linkaxes with different x scales
17 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (3 件)
dario
2015 年 5 月 12 日
Find attached a simple demo :)
1 件のコメント
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
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.
0 件のコメント
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 件のコメント
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 Center および File Exchange で Graphics Object Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!