Making inset figures in Matlab
Originally posted in June 2004, this was one of the most read posts; the search term “inset” generated the largest number of hits to my site, more than either “Niket” or “Kaisare”. Here is the post, restored thanks to Google cache.
Some time back, I wanted to create inset figures in Matlab(TM). I tried asking friends and help desk, but got no help. So, I tried experimenting and finally found a way to do it. Its really neat!
% Generating inset plots in Matlab
h1 = figure(1);
% h1 now has "handle" to the figureplot(cumsum(randn(100,1)))
h2 = get(h1,'CurrentAxes');
% h2 now has "handle" to the curh3 = axes('pos',[.5 .2 .35 .35]);
% We specify an INSET axis.
% This axis has its origin at RELATIVE location (0.5, 0.2)
% The X- and Y-axes lengths are both 0.35 (i.e. 35% of main figure)
% h3 has the handle to this INSET figure
plot([0:0.1:10],sin([0:0.1:10]))
% Plot on the inset
set(h1,'CurrentAxes',h2)
hold on; plot(cumsum(randn(100,1)),'r')
% In order to plot on the main figure, we need to select it
% This is done using the axis handle h2, which is passed as
% the Current Axis for the figure handle h1
% Next, we plot another plot on main area
set(h1,'CurrentAxes',h3)
hold on; plot([0:0.1:10],cos([0:0.1:10]),'r')
% To plot another figure in inset, we need to select it first.
% Again follow the same procedure as above
plot([0:10],[0:0.1:1],'k')
% Where will this get plotted?
% Remember that currently selected axis is the inset axis
% Hence it will get plotted on the inset plot
was looking for this for a while! thanks
Thanks for your script. I just want to say that If figure(1) contains no axes, get(h1,’CurrentAxes’) returns the empty matrix and h2 = [ ].
Mucho gracias. Exactly what was needed.
Excellent!
There’s a nice little m-file in matlab central called figcopy.m. It lets you make inset figure interactively and you can even fiddle around with them after you’ve put them in. This code works well too, though.
Thanks Daniel.
The figcopy.m file can be found at this link:
http://www.mathworks.com/matlabcentral/fileexchange/17768
Also read the comment by Thierry Dalon to make figcopy work well.
Thanks,
Simple and useful