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

7 visualizzazioni (ultimi 30 giorni)
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?

Risposte (2)

Walter Roberson
Walter Roberson il 10 Feb 2016
Use linkaxes()
  1 Commento
Troy Daniels
Troy Daniels il 10 Feb 2016
I have one plot with two axes, showing different scales. Unless I have missed something, linkaxes is for two plots with the same scales.

Accedi per commentare.


Serge
Serge il 30 Mar 2022
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

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by