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.
Create three subplots and link their x-axes and y-axes so that they are always in sync.
First, create a figure with three subplots. Plot data in each subplot.
figure ax1 = subplot(2,2,1); x1 = linspace(0,6); y1 = sin(x1); plot(x1,y1) ax2 = subplot(2,2,2); x2 = linspace(0,10); y2 = sin(2*x2); plot(x2,y2) ax3 = subplot(2,2,[3,4]); x3 = linspace(0,16); y3 = sin(6*x3); plot(x3,y3)
Now link the axes for the three subplots using the linkaxes function. Pass the string 'xy' as an input argument to the function to link both the x-axes and y-axes. The linkaxes function uses the limits from the first axes passed to it.
Panning or zooming into one of the subplots displays the same range of data in the other two subplots. To remove the linking, use linkaxes([ax1,ax2,ax3]),'off'.
Load the count.dat data set which returns a three-column array named count. Create a new figure with three subplots and return the axes handles. In each subplot, create a bar graph.
load count.dat figure ax1 = subplot(3,1,1); bar(count(:,1),'g'); ax2 = subplot(3,1,2); bar(count(:,2),'b'); ax3 = subplot(3,1,3); bar(count(:,3),'m');
Link the x-axes and y-axes limits using linkaxes with the argument 'xy'. Base the limits on the third subplot by passing its axes handle, ax3, as the first input to linkaxes.
If you zoom in on one subplot, then the other two subplots behave in the same manner. To enable interactive zooming, use zoom on. To disable zooming, use zoom off.
Load the count.dat data set which returns a three-column array named count. Create a new figure with two subplots and return the axes handles. In each subplot, create a bar graph.
load count.dat figure ax1 = subplot(2,1,1); bar(count(:,1),'g'); ax2 = subplot(2,1,2); bar(count(:,2),'b');
Link the x-axes for the two subplots.
Set the x-axis limits for the second subplot. Setting the limits overrides the limits initially set by linkaxes. Changing the x-axis limits effects both subplots.
ax2.XLim = [4.5,9.5];
If you pan either subplot, then both subplots pan uniformly in the x-direction, but only one subplot moves in the y-direction. To enable interactive panning, use pan on. To disable panning, use pan off.
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.