linkaxes

Synchronize limits of specified 2-D axes

Syntax

linkaxes(axes_handles)
linkaxes(axes_handles,'option')

Description

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.

    Note:   Use linkaxes for axes that remain in 2-D views.

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.

linkaxes(axes_handles,'option') links the axes' axes_handles according to the specified option. The option argument can be one of the following strings:

x

Link x-axis only.

y

Link y-axis only.

xy

Link x-axis and y-axis.

off

Remove linking.

See the linkprop function for more advanced capabilities that allow you to link object properties on any graphics object.

Examples

expand all

Create Subplots and Link Their Axes

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.

linkaxes([ax1,ax2,ax3],'xy')

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'.

Use Specific Subplot to Determine Limits for All Subplots

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.

linkaxes([ax3,ax2,ax1],'xy');

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.

Link x-Axes And Change Axis Limits

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.

linkaxes([ax1,ax2],'x');

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.

More About

expand all

Tips

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.

    Note:   linkaxes is not designed to be transitive across multiple invocations. If you have three axes, ax1, ax2, and ax3 and want to link them together, call linkaxes with [ax1, ax2, ax3] as the first argument. Linking ax1 to ax2, then ax2 to ax3, "unbinds" the ax1-ax2 linkage.

See Also

| | |

Was this topic helpful?