How Can I Zoom with Multiple Axes on a Plot

28 views (last 30 days)
Paul
Paul on 29 Sep 2020
Commented: Paul on 6 Oct 2020
I create a plot and add a secondary x-axis. The code for the secondary x-axis came from dbp in a post that for the life of me I can't find.
xm = 0:10;
xcm = xm*100;
plot(xm,5*xm,'-x');
xlabel('meters');
h1 = get(gcf,'Children');
pos1 = h1.Position;
pos2 = pos1;
pos1(2) = pos1(2) + pos2(1);
pos1(4) = pos1(4) - pos2(1);
h1.Position = pos1;
h2 = axes('Position',pos2,'color','none');
h2.XColor = 'r';
h2.YColor = 'none';
set(gcf,'CurrentAxes',h1);
This gives me the plot I want. But when I zoom, the plot zooms only the second axes (the red one) and doesn't change the plot.
So the first question is: what controls which axes is affected by the zoom? I thought it would be the current axes, but apparently that's not the case.
More importantly, what needs to be done so that zooming on the plot does what's expected for the plot and both sets of axes, as is is the case when zooming after using plotyy? I looked at plotyy and saw some code about axes pointing to each other and using some ListenerManager. Is that what keeps the axes synched when zooming?

Answers (1)

Nitin Kapgate
Nitin Kapgate on 5 Oct 2020
You can use “linkaxes” function to synchronize the limits of the specified vector of axes. Synchronizing the axes limits for the specified axis dimension allows you to display the same range data between multiple axes in a figure.
For example, the following code synchronizes the limits for the x-axis only.
linkaxes([ax1, ax2, ax3],'x')
To open a demo example for using “linkaxes” function, you can enter the following in MATLAB command window:
openExample('graphics/LinkaxesSynchronizeLimitsExample')
  1 Comment
Paul
Paul on 6 Oct 2020
linkaxes doesn't work for this use case as far as I can tell. The first axes zoom fine, bu the XTickLabels on the second axes get garbled. If you think linkaxes works, can you post the code and pictures of the plot before and after zooming?

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!