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!