Main Content

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 calllinkaxes, 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 thex-axis only.

Examples

collapse all

Display a tiling of plots using thetiledlayoutandnexttilefunctions.

Create a 3-by-1 tiled chart layout by using thetiledlayoutfunction. Then, create the axes objectsax1,ax2, andax3by using thenexttilefunction and plot into each axes.

tiledlayout(3,1)% First plotax1 = nexttile; x1 = linspace(0,6); y1 = sin(x1); plot(x1,y1)% Second plotax2 = nexttile; x2 = linspace(0,10); y2 = 2*sin(2*x2); plot(x2,y2)% Third plotax3 = nexttile; x3 = linspace(0,12,200); y3 = 4*sin(6*x3); plot(x3,y3)

Figure contains 3 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line.

Synchronize thex-axis andy-axis limits of each plot. Note that the new axes limits incorporate the old limits.

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

Figure contains 3 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line.

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

ax1.XLim = [0 4.5];

Figure contains 3 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line.

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

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

Synchronize and modify thex-axis limits of multiple axes objects by using thelinkaxesfunction.

Display a tiling of plots using thetiledlayoutandnexttilefunctions. Create a 2-by-1 tiled chart layout by using thetiledlayoutfunction. Then, create the axes objectsax1andax2by using thenexttilefunction and plot into each axes.

t = tiledlayout(2,1);% First plotax1 = nexttile; p1 = peaks; surf(ax1,p1); view(3)% Second plotax2 = nexttile; p2 = peaks(25); mesh(ax2,p2)

图包含2轴对象。坐标轴对象1续ains an object of type surface. Axes object 2 contains an object of type surface.

Synchronize thex-axis limits for the two axes objects. Thex-axis limits for the newly linked axes objects will update to encompass all the data.

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

Update thex-axis limits for all linked axes objects by setting thex-axis limits for just one of the axes objects.

ax1.XLim = [0 15];

图包含2轴对象。坐标轴对象1续ains an object of type surface. Axes object 2 contains an object of type surface.

ax2.XLim
ans =1×20 15

Remove the linking by turning off synchronization.

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

Input Arguments

collapse all

Target axes, specified as a vector ofAxesobjects.

You can link any number ofAxesobjects. For example,linkaxes([ax1 ax2 ax3])linksax1,ax2, andax3. Separate calls tolinkaxes([ax1 ax2])andlinkaxes([ax2 ax3])cancels the link betweenax1andax2.

Axis limits to synchronize, specified as one of these values:

  • 'xyz'— Synchronize thex-axis,y-axis, andz-axis limits.

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

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

  • 'z'— Synchronize only thez-axis limits.

  • 'xy'— Synchronize only thex-axis andy-axis limits.

  • 'xz'— Synchronize only thex-axis andz-axis limits.

  • 'yz'— Synchronize only they-axis andz-axis limits.

  • 'off'— Turn off synchronization.

Version History

Introduced before R2006a

expand all