linkaxes with different x scales

19 次查看(过去 30 天)
Mech Princess
Mech Princess 2015-2-13
评论: Adam Danz 2021-4-15
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)

回答(3 个)

dario
dario 2015-5-12
Find attached a simple demo :)
  1 个评论
Adam Danz
Adam Danz 2021-4-15
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
Peter Kövesdi 2019-4-2
编辑:Peter Kövesdi 2019-4-2
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 2015-2-13
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 个评论
Mech Princess
Mech Princess 2015-2-13
Thanks for the reply. I modified the code as following. Zooming together works but the x-axis is wrong; both axis have 1 to 10. first plot should be 1 to 10 and second 10 to 100. when I zoom 1 in first plot it should zoom to 10 in second plot.
clear; close all; clc;
y=rand(10,1);
x1=1:10;
x2=10:10:100;
ax(1)=subplot(211); plot(y);
ax(2)=subplot(212); plot(y);
linkaxes(ax,'xy')
xLimListener = addlistener( ax, 'XLim', 'PostSet', @(src,evt) disp( 'X Changed' ) )
yLimListener = addlistener( ax, 'YLim', 'PostSet', @(src,evt) disp( 'Y Changed' ) )
Adam
Adam 2015-2-14
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by