linkaxes

Synchronize limits of multiple axes

Description

example

linkaxes(ax) synchronizes the limits of the specified vector of axes. Synchronizing limits allows you to zoom or pan in one plot or figure and display the same range of data in another plot or figure. When you first call linkaxes, the function chooses new limits that incorporate the current limits of all the specified axes.

example

linkaxes(ax,dimension) synchronizes the axes limits for the specified axis dimension. For example, linkaxes(ax,'x') synchronizes the limits for the x-axis only.

Examples

collapse all

Starting in R2019b, you can display a tiling of plots using the tiledlayout and nexttile functions. Call the tiledlayout function to create a 3-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1, ax2, and ax3. Then plot into each axes.

tiledlayout(3,1)

% First plot
ax1 = nexttile;
x1 = linspace(0,6);
y1 = sin(x1);
plot(x1,y1)

% Second plot
ax2 = nexttile;
x2 = linspace(0,10);
y2 = 2*sin(2*x2);
plot(x2,y2)

% Third plot
ax3 = nexttile;
x3 = linspace(0,12,200);
y3 = 4*sin(6*x3);
plot(x3,y3)

Synchronize the x-axis and y-axis limits of each plot. Note that the new axes limits incorporate the old limits.

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

Set the x-axis limits for the first plot. All of axes are linked, so the x-axis limits in the second and third plots also change.

ax1.XLim = [0 4.5];

Panning or zooming into one of the plots displays the same range of data in the other two plots.

To remove the linking, use linkaxes([ax1 ax2 ax3],'off').

Create two plots in a 2-by-1 tiled chart layout. Synchronize the x-axis limits by calling the linkaxes function.

x1 = linspace(0,20,100);
y1 = sin(x1);
x2 = 3:17;
y2 = rand(1,15);

% Create plots.
t = tiledlayout(2,1);
ax1 = nexttile;
plot(ax1,x1,y1)
ax2 = nexttile;
stem(ax2,x2,y2)

% Link the axes. Add title and labels.
linkaxes([ax1,ax2],'x');

Create shared x- and y-axis labels by passing t to the xlabel and ylabel functions. Remove the x-axis tick labels from the top plot by calling the xticklabels function. Then minimize the space between the plots by setting the TileSpacing property of t to 'compact'.

xlabel(t,'Input')
ylabel(t,'Output')
xticklabels(ax1,{})
t.TileSpacing = 'compact';

Input Arguments

collapse all

Target axes, specified as a vector of Axes objects. The linkaxes function supports 2-D Cartesian axes only.

You can link any number of Axes objects. For example, linkaxes([ax1 ax2 ax3]) links ax1, ax2, and ax3. Separate calls to linkaxes([ax1 ax2]) and linkaxes([ax2 ax3]) cancels the link between ax1 and ax2.

Limits to synchronize, specified as one of these values:

  • 'xy' — Synchronize both the x-axis and y-axis limits.

  • 'x' — Synchronize only the x-axis limits.

  • 'y' — Synchronize only the y-axis limits.

  • 'off' — Turn off synchronization.

Introduced before R2006a