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?

回答(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