
linkaxes with different x scales
10 views (last 30 days)
Show older comments
Hi, I have 2 plots with same y axis but different x axis. But the two x axis are propotional. How do I link x=1 of plot 1 to x=10 of plot 2 and x=10 to x=100. and also for both plots to zoom in and out together like linkaxes. See below. Thanks
y = rand(10,1);
x1 = 1:10;
x2 = 10:10:100;
ax(1) = subplot(211); plot(x1,y)
ax(2) = subplot(212); plot(x2,y)
0 Comments
Answers (3)
dario
on 12 May 2015
Find attached a simple demo :)

1 Comment
Adam Danz
on 15 Apr 2021
Nice demo, dario. It works well when panning and applying other translations of the axes but it does not work when the user zooms in/out of the axes. The solution does not maintain the relative aspect ratios between the two pairs of axes which results in the misalignment between the green curve and the underlying red curve.
I recently addressed this same question in another thread. This solution correctly yokes the two pairs of axis limits:
Peter Kövesdi
on 2 Apr 2019
Edited: Peter Kövesdi
on 2 Apr 2019
Adams solution works well, but fails on 'Restore View' from the context menu in zoom or pan mode.
To get the axes synchronized on 'Restore View' as well, add the same callback function also to the zoom and the pan objects:
h = zoom;
h.ActionPostCallback = @(src,evt) updateAxes( ax(1), ax(2) );
h = pan;
h.ActionPostCallback = @(src,evt) updateAxes( ax(1), ax(2) );
But then, if only added there, it would synchronize only when releasing the mouse button, not while dragging, so both is recommended, addlistener 'PostSet' and ActionPostCallback.
0 Comments
Adam
on 13 Feb 2015
xLimListener = addlistener( hAxes1, 'XLim', 'PostSet', @(src,evt) disp( 'X Changed' ) )
yLimListener = addlistener( hAxes1, 'YLim', 'PostSet', @(src,evt) disp( 'Y Changed' ) )
will respond whenever the limits of an axes change.
So you can just create your own callback to go in-place of that one which will change the XLim and YLim properties of the second axes in a manner of your choosing relative to the XLim and YLim of the first axes.
Then every time the first axes are zoomed, the second axes will also be zoomed.
4 Comments
Alessandro Podesta'
on 17 Jun 2025
Hi,
thank you for your help.
I wonder whether at present (2025) this pieces of codes have been integrated in some plotting function, or the implementation is still left to the user .
Alessandro
Adam Danz
on 17 Jun 2025
This type of axes-linking with non-identical limits is not available in MATLAB as of R2025a.
The only addition since these solutions were added is the LimitsChangedFcn (R2021a) which is a callback property on the axes rulers. See this community highlight. It behaves similarly to the listeners in Adam's answer.
See Also
Categories
Find more on Data Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!