How do I add a listener for an implicit change of limits on an axis
조회 수: 7 (최근 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?
댓글 수: 0
답변 (2개)
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
댓글 수: 0
참고 항목
카테고리
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!