![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176724/image.jpeg)
linkaxes with different x scales
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (3)
dario
el 12 de Mayo de 2015
Find attached a simple demo :)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176724/image.jpeg)
1 comentario
Adam Danz
el 15 de Abr. de 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
el 2 de Abr. de 2019
Editada: Peter Kövesdi
el 2 de Abr. de 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 comentarios
Adam
el 13 de Feb. de 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.
2 comentarios
Adam
el 14 de Feb. de 2015
You can't use linkaxes. You have to program your own callback to change the XLim and YLim properties of the second axes, something like:
function updateAxes( axes1, axes2 )
xLim1 = get( axes1, 'XLim' );
yLim1 = get( axes1, 'YLim' );
set( axes2, 'XLim', 10 * xLim1 );
set( axes2, 'YLim', 10 * yLim1 );
end
then plug that function into those addlistener calls in place of my disp function.
I can never remember the syntax for callbacks so I always just try it out, but something like:
xLimListener = addlistener( ax(1), 'XLim', 'PostSet', @(src,evt) updateAxes( ax(1), ax(2) );
You can probably use the src as the first axes since that is what it should point to, but I haven't tested out that syntax so you'll have to work out exactly what it needs to be.
Ver también
Categorías
Más información sobre Graphics Object Programming en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!