linkaxes with different x scales

10 vues (au cours des 30 derniers jours)
Mech Princess
Mech Princess le 13 Fév 2015
Commenté : Adam Danz le 17 Juin 2025
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)

Réponses (3)

dario
dario le 12 Mai 2015
Find attached a simple demo :)
  1 commentaire
Adam Danz
Adam Danz le 15 Avr 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:

Connectez-vous pour commenter.


Peter Kövesdi
Peter Kövesdi le 2 Avr 2019
Modifié(e) : Peter Kövesdi le 2 Avr 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.

Adam
Adam le 13 Fév 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 commentaires
Alessandro Podesta'
Alessandro Podesta' le 17 Juin 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
Adam Danz le 17 Juin 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Exploration dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by