Synchronize limits of specified 2-D axes
Use linkaxes to synchronize the individual axis limits across several figures or subplots within a figure. Calling linkaxes makes all input axes have identical limits. Linking axes is best when you want to zoom or pan in one subplot and display the same range of data in another subplot.
linkaxes(axes_handles) links the x- and y-axis limits of the axes specified in the vector axes_handles. You can link any number of existing plots or subplots. The axes_handles input should be a vector of the handles for each plot or subplot. Entering an array of values results in an error message.
Link x-axis only.
Link y-axis only.
Link x-axis and y-axis.
See the linkprop function for more advanced capabilities that allow you to link object properties on any graphics object.
You can use interactive zooming or panning (selected from the figure toolbar) to see the effect of axes linking. For example, pan in one graph and notice how the x-axis also changes in the other. The axes responds in the same way to zoom and pan directives you type in the Command Window.
This example loads three vectors of data and creates a subplot for each vector. After a three-second pause, it then calls linkaxes with the argument 'xy' to synchronize both the x and y axes limits. By specifying the third subplot first to linkaxes, you establish its y-limit for all the subplots:
load count.dat % Contains a 3-column vector named count figure ax(1) = subplot(3,1,1); bar(ax(1),count(:,1),'g'); ax(2) = subplot(3,1,2); bar(ax(2),count(:,2),'b'); ax(3) = subplot(3,1,3); bar(ax(3),count(:,3),'m');
Base the y-axis limits on the bottom subplot.
% Wait three seconds and then link the axes pause(3) linkaxes([ax(3) ax(2) ax(1)],'xy');
Click the Zoom in icon to enter Zoom mode, and click the mouse near the middle of the top axes. You observe all three axes respond the same. If you pan in any of the axes, all of them also respond the same.
Create two subplots containing related bar graphs. Call linkaxes to link only the x-axis limits of the two axes. Unlike in the previous example, the y-limits of the graphs remain unchanged. The example shows the effect of restricting the x-values to a range of 5 units and then manually panning either subplot:
load count.dat figure ax(1) = subplot(2,1,1); bar(ax(1),count(:,1),'g'); ax(2) = subplot(2,1,2); bar(ax(2),count(:,2),'b'); linkaxes(ax,'x'); % Restrict either axis to show 5 values set(ax(1),'XLim',[4.5 9.5])
Choose the Pan tool (Tools > Pan) (or type pan on). Drag the top axes. Both axes pan uniformly in x, but only the one you pan moves in the y direction.
Create two subplots containing data having different ranges. The first axes handle passed to linkaxes determines the data range for all other linked axes. In this example, calling set for the lower axes overrides the x-limits established by the call to linkaxes:
a1 = subplot(2,1,1); % Plot 10 numbers on top plot(randn(10,1)); a2 = subplot(2,1,2); % Plot 100 numbers below plot(a2,randn(100,1)) % Link the axes; subplot 2 now out of range linkaxes([a1 a2], 'x');
In order to display the full range of x-values, override the axes limits that linkaxes established.
Now both x-axes run from 1 to 100. You could also use set(a2,'xlim',[1 100]).
The first axes you supply to linkaxes determines the x- and y-limits for all linked axes. This can cause plots to partly or entirely disappear if their limits or scaling are very different. To override this behavior, after calling linkaxes, specify the limits of the axes that you want to control with the set command, as the third example illustrates.