How do I add a listener for an implicit change of limits on an axis

조회 수: 7 (최근 30일)
Troy Daniels
Troy Daniels 2016년 2월 10일
답변: Serge 2022년 3월 30일
I have a plot with two sets of axes. One set shows latitude/longitude, and the other shows a local X-Y coordinate system. When the bounds of the plot change, I want to update the limits of the second (lat/lon) axes to match the first pair of axes.
I have added this line:
addlistener(xyAxes, {'XLim', 'YLim'}, 'PostSet', @(hAxes, eventData) syncAxes());
The plotting code (which I inherited) is similar to this:
function testListener
close(77);
figure(77)
xyAxes = gca;
addlistener(xyAxes, {'XLim', 'YLim'}, 'PostSet', @(hAxes, eventData) ...
fprintf('Listener called\n'));
XY.plots.p = plot(xyAxes, 0,0, 'x', 'LineWidth', 2, 'MarkerSize', 20);
XY.plots.x = 2;
XY.plots.y = 3;
set(XY.plots.p, 'XData', XY.plots.x, 'YData', XY.plots.y);
fprintf('Set data %f %f\n', get(xyAxes, 'XLim'));
pause;
XY.plots.x = [ XY.plots.x 5 ];
XY.plots.y = [ XY.plots.y 4 ];
set(XY.plots.p, 'XData',XY.plots.x, 'YData',XY.plots.y);
fprintf('Set data, plot is updated %f %f\n', get(xyAxes, 'XLim'));
pause;
set(xyAxes, 'XLim', get(xyAxes, 'XLim'));
fprintf('Manually set XLim property %f %f\n', get(xyAxes, 'XLim'));
pause;
xlim(xyAxes, get(xyAxes, 'XLim'));
fprintf('Called XLim %f %f\n', get(xyAxes, 'XLim'));
When I run the above code, I get this output:
>> testListener
Set data 1.000000 3.000000
Set data, plot is updated 2.000000 5.000000
Listener called
Manually set XLim property 2.000000 5.000000
Listener called
Called XLim 2.000000 5.000000
When I explicitly set the bounds on the axis, then the listener is called. However, when I change the data, which implicitly changes the bounds, the listener is not called.
What do I need to do to have the listener be called for the implicit change?

답변 (2개)

Walter Roberson
Walter Roberson 2016년 2월 10일
Use linkaxes()
  댓글 수: 1
Troy Daniels
Troy Daniels 2016년 2월 10일
I have one plot with two axes, showing different scales. Unless I have missed something, linkaxes is for two plots with the same scales.

댓글을 달려면 로그인하십시오.


Serge
Serge 2022년 3월 30일
Here is a simple example that updates the time axis on two linked plots every time you zoom/pan:
ax(1)=subplot(211); plot(rand(5));
ax(2)=subplot(212); plot(rand(5));
linkaxes(ax,'x') %link axis extents
addlistener(ax,'XLim','PostSet',@(~,e)datetick(e.AffectedObject,'x','keeplimits')); %self updating time axis

카테고리

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