axis - Set axis limits and aspect ratios - MATLAB (original) (raw)

Set axis limits and aspect ratios

Syntax

Description

Set Limits and Aspect Ratio

axis([limits](#buk989s-1-limits)) specifies the limits for the current axes. Specify the limits as vector of four, six, or eight elements.

example

axis [style](#buk989s-1-style) uses a predefined style to set the limits and scaling. For example, specify the style asequal to use equal data unit lengths along each axis.

example

axis [mode](#buk989s-1-mode) sets whether MATLAB® automatically chooses the limits or not. Specify the mode asmanual, auto, or one of the semiautomatic options, such as 'auto x'.

example

axis [ydirection](#buk989s-1-ydirection), whereydirection is ij, places the origin at the upper left corner of the axes. The y values increase from top to bottom. The default for ydirection isxy, which places the origin at the lower left corner. The_y_ values increase from bottom to top.

example

axis [visibility](#buk989s-1-visibility), wherevisibility is off, turns off the display of the axes background. Plots in the axes still display. The default forvisibility is on, which displays the axes background.

example

Get Limits

[lim](#buk989s-1-lim) = axis returns the_x_-axis and _y_-axis limits for the current axes. For 3-D axes, it also returns the _z_-axis limits. For polar axes, it returns the _theta_-axis and_r_-axis limits.

example

[lim](#buk989s-1-lim) = axis([ax](#buk989s-1-ax)) returns the limits of the specified by ax instead of the current axes.

Examples

collapse all

Plot the sine function.

x = linspace(0,2*pi); y = sin(x); plot(x,y,'-o')

Figure contains an axes object. The axes object contains an object of type line.

Change the axis limits so that the _x_-axis ranges from 0 to 2π and the _y_-axis ranges from -1.5 to 1.5.

Figure contains an axes object. The axes object contains an object of type line.

Create a stairstep plot, and use the axis padded command to add a margin of padding between the plot and the plot box.

x = 0:12; y = sin(x); stairs(x,y) axis padded

Figure contains an axes object. The axes object contains an object of type stair.

Create a plot. Set the limits for the _x_-axis and set the minimum _y_-axis limit. Use an automatically calculated value for the maximum _y_-axis limit.

x = linspace(-10,10,200); y = sin(4x)./exp(.1x); plot(x,y) axis([-10 10 0 inf])

Figure contains an axes object. The axes object contains an object of type line.

Call the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2. Plot data in each axes. Then set the axis limits for both axes to the same values.

tiledlayout(2,1) x1 = linspace(0,10,100); y1 = sin(x1); ax1 = nexttile; plot(ax1,x1,y1)

x2 = linspace(0,5,100); y2 = sin(x2); ax2 = nexttile; plot(ax2,x2,y2)

axis([ax1 ax2],[0 10 -1 1])

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

Plot a surface without displaying the axes lines and background.

Figure contains an axes object. The hidden axes object contains an object of type surface.

Plot a surface. Set the axis limits to equal the range of the data so that the plot extends to the edges of the axes.

Figure contains an axes object. The axes object contains an object of type surface.

Return the values of the current axis limits.

l = 1×6

1.0000   49.0000    1.0000   49.0000   -6.5466    8.0752

Create a checkerboard plot and change the direction of the coordinate system.

First, create the plot using the summer colormap. By default, the x values increase from left to right and the y values increase from bottom to top.

C = eye(10); pcolor(C) colormap summer

Figure contains an axes object. The axes object contains an object of type surface.

Reverse the coordinate system so that the y values increase from top to bottom.

Figure contains an axes object. The axes object contains an object of type surface.

Plot a sine wave.

x = linspace(0,10); y = sin(x); plot(x,y)

Figure contains an axes object. The axes object contains an object of type line.

Add another sine wave to the axes using hold on. Keep the current axis limits by setting the limits mode to manual.

y2 = 2*sin(x); hold on axis manual plot(x,y2) hold off

Figure contains an axes object. The axes object contains 2 objects of type line.

If you want the axes to choose the appropriate limits, set the limits mode back to automatic.

Figure contains an axes object. The axes object contains 2 objects of type line.

Input Arguments

collapse all

Axis limits, specified as a vector of four, six, or eight elements.

For Cartesian axes, specify the limits in one of these forms:

The XLim, YLim, ZLim, andCLim properties for theAxes object store the limit values.

For polar axes, specify the limits in this form:

The ThetaLim andRLim properties for the PolarAxes object store the limit values.

For partially automatic limits, use inf or -inf for the limits you want the axes to choose automatically. For example, axis([-inf 10 0 inf]) lets the axes choose the appropriate minimum _x_-axis limit and maximum _y_-axis limit. It uses the specified values for the maximum _x_-axis limit and minimum _y_-axis limit.

Note

If the _x_-axis, _y_-axis, or _z_-axis displays categorical, datetime, or duration values, then use the xlim, ylim, and zlim functions to set the limits instead.

Example: axis([0 1 0 1])

Example: axis([0 1 0 1 0 1])

Example: axis([0 inf 0 inf])

Manual, automatic, or semiautomatic selection of axis limits, specified as one of the values in this table. All of the auto mode values use thetickaligned style to calculate the limits for the particular axis or set of axes you specify.

Value Description Axes Properties That Change
manual Freeze all axis limits at their current values. Sets XLimMode,YLimMode, andZLimMode to'manual'. If you are working with polar axes, then this option setsThetaLimMode andRLimMode to'manual'.
auto Automatically choose all axis limits. Sets XLimMode,YLimMode, andZLimMode to'auto'. If you are working with polar axes, then this option setsThetaLimMode andRLimMode to'auto'.
'auto x' Automatically choose the _x_-axis limits. Sets XLimMode to'auto'.
'auto y' Automatically choose the _y_-axis limits. Sets YLimMode to'auto'.
'auto z' Automatically choose the _z_-axis limits. Sets ZLimMode to'auto'.
'auto xy' Automatically choose the _x_-axis and _y_-axis limits. Sets XLimMode andYLimMode to'auto'.
'auto xz' Automatically choose the _x_-axis and _z_-axis limits. Sets XLimMode andZLimMode to'auto'.
'auto yz' Automatically choose the _y_-axis and _z_-axis limits. Sets YLimMode andZLimMode to'auto'.

Note

You cannot use these options with polar axes.

Axis limits and scaling, specified as one of these values.

Value Description Axes Properties That Change
tickaligned In general, align the edges of the axes box with the tick marks that are closest to your data without excluding any data. The appearance might vary depending on the type of data you plot and the type of chart you create. XLimMode, YLimMode, andZLimMode change to'auto'.XLimitMethod,YLimitMethod, andZLimitMethod change to'tickaligned'.XLim,YLim, andZLim automatically update to incorporate new data added to the axes. To keep the limits from changing when using hold on, use axis tickaligned manual.
tight Fit the axes box tightly around the data by setting the axis limits equal to the range of the data. XLimMode,YLimMode, andZLimMode change to'auto'. If you are working with polar axes, then ThetaLimMode and RLimMode change.XLimitMethod,YLimitMethod, andZLimitMethod change to'tight'.XLim,YLim, andZLim automatically update to incorporate new data added to the axes. To keep the limits from changing when using hold on, use axis tight manual.
padded Fit the axes box around the data with a thin margin of padding on all sides. The width of the margin is approximately 7% of your data range. XLimMode,YLimMode, andZLimMode change to'auto'.XLimitMethod,YLimitMethod, andZLimitMethod change to'padded'.XLim,YLim, andZLim automatically update to incorporate new data added to the axes. To keep the limits from changing when using hold on, use axis padded manual.
equal Use the same length for the data units along each axis.equal is not supported by plots created withyyaxis. Sets DataAspectRatio to [1 1 1] and setsDataAspectRatioMode andPlotBoxAspectRatioMode to'manual'. For 2-D views, it also sets the XLimMode andYLimMode appropriately so that the axes fills its allotted space within the parent figure or other container. For 3-D Views,XLimMode,YLimMode, andZLimMode are set to'auto' andXLimitMethod,YLimitMethod, andZLimitMethod are set to'tight'.This style disables the default “stretch-to-fill” behavior.
image Use the same length for the data units along each axis and fit the axes box tightly around the data.image is not supported by plots created withyyaxis. Sets DataAspectRatio to [1 1 1],DataAspectRatioMode to'manual', andPlotBoxAspectRatioMode to'auto'. It also setsXLimMode,YLimMode, andZLimMode to'auto' andXLimitMethod,YLimitMethod, andZLimitMethod to'tight'.This style disables the default “stretch-to-fill” behavior.
square Use axis lines with equal lengths. Adjust the increments between data units accordingly. Sets PlotBoxAspectRatio to [1 1 1] and sets the associated mode property to manual.This style disables the default “stretch-to-fill” behavior.
fill Enable the “stretch-to-fill” behavior (the default). The lengths of each axis line fill the position rectangle defined in thePosition property of the axes. Sets DataAspectRatioMode andPlotBoxAspectRatioMode to'auto'.
vis3d Freeze the aspect ratio properties.vis3d is not supported by plots created withyyaxis. Sets DataAspectRatioMode,PlotBoxAspectRatioMode, andCameraViewAngleMode to'manual'.
normal Restore the default behavior. Sets DataAspectRatioMode andPlotBoxAspectRatioMode to'auto'.

For more information on the plot box aspect ratio and the data aspect ratio, see the PlotBoxAspectRatio and DataAspectRatio properties.

Note

You cannot use these options with polar axes, except for the axis tight and axis normal commands.

_y_-axis direction, specified as one of these values:

Note

You cannot use these options with polar axes.

Axes lines and background visibility, specified as any of these values:

Note

Use parentheses to specify 1, 0,true, false, or anOnOffSwitchState value. For example,axis(0) and axis(false) hide the axes.

Parentheses are optional for the values "on" and"off". For example, axis off hides the axes.

Specifying the visibility sets the Visible property of the Axes object or PolarAxes object to the specified value.

Target axes, specified as one or more axes. You can specify Axes objects or PolarAxes objects. If you do not specify the axes, then axis sets the limits for the current axes (gca).

When you specify the axes, use single quotes around other input arguments that are character vectors.

Example: axis(ax,'tight')

Example: axis(ax,limits)

Example: axis(ax,'manual')

Output Arguments

collapse all

Current limit values, returned as a four-element or six-element vector.

Tips

Version History

Introduced before R2006a

expand all

This syntax, which returns the axis limit selection mode (m), visibility (v), and _y_-axis direction (d) is no longer supported and returns an error.

[m,v,d] = axis('state')

You can get the same information by querying these Axes properties.

This change was announced in R2015a.

Display or hide the axes by specifying the visibility input argument as a logical value or as a matlab.lang.OnOffSwitchState value. The values 1 and true are equivalent to"on", and 0 and false are equivalent to "off".

The values "on" and "off" are still supported.