Info

This question is closed. Reopen it to edit or answer.

How to keep plot window area of interest without drawing to the same plot?

1 view (last 30 days)
plot(Y, 'r'); hold on;
plot(X);
That's how I plot.
When I zoom into a very specific region, I'd like to be able to re-run the code (generating X and Y) and draw a fresh plot, but keep the 'window', i.e. the region of interest where I zoomed in.
How to achieve that in a simple fashion?

Answers (1)

Geoff Hayes
Geoff Hayes on 6 Nov 2014
Jens - consider using linkaxes which will allow the axes on your different figures to have the same limits specified. And as you zoom in or out, or pan within one axes, the same behaviour affects the other one - so the axes limits for both figures are kept identical. Try the following
% generate some data
X = randi(255,42,1);
Y = randi(128,52,1);
% plot the data to first figure
hFig1 = figure;
plot(Y,'r');
hold on;
plot(X);
% get the axes on the first figure (we assume only one)
hAxes1 = findobj('Parent',hFig1,'Type','axes');
Now zoom in or pan within the axes so that the limits change. Now regenerate your data and repeat for the second figure
% re-generate the data
X = randi(255,42,1);
Y = randi(128,52,1);
% plot the data to second figure
hFig2 = figure;
plot(Y,'r');
hold on;
plot(X);
% get the axes on the first figure (we assume only one)
hAxes2 = findobj('Parent',hFig2,'Type','axes');
The axes limits for the second figure are not that for the first, so link the two
linkaxes([hAxes1 hAxes2])
and both axes should have the same limits. From the above link, the first axes you supply to linkaxes determines the x- and y-limits for all linked axes, but manipulating the limits in either axes affects both.
Try the above and see what happens!
Note some of the above code can be simplified. For example,
hAxes1 = findobj('Parent',hFig1,'Type','axes');
could be replaced with
hAxes1 = gca; % get current axes
but I prefer using findobj because it makes it a little more clear in grabbing a specific axes from a specific figure.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!