Set axis limits and aspect ratios (2024)

Set axis limits and aspect ratios

collapse all in page

Syntax

axis(limits)

axis style

axis mode

axis ydirection

axis visibility

lim = axis

___ = axis(ax,___)

Description

example

axis(limits) specifiesthe limits for the current axes. Specify the limits as vector of four,six, or eight elements.

example

axis style uses apredefined style to set the limits and scaling. For example, specifythe style as equal to use equal data unit lengthsalong each axis.

example

axis mode sets whether MATLAB® automaticallychooses the limits or not. Specify the mode as manual, auto,or one of the semiautomatic options, such as 'auto x'.

example

axis ydirection, where ydirection is ij,places the origin at the upper left corner of the axes. The y valuesincrease from top to bottom. The default for ydirection is xy,which places the origin at the lower left corner. The y valuesincrease from bottom to top.

example

axis visibility, where visibility is off,turns off the display of the axes background. Plots in the axes stilldisplay. The default for visibility is on,which displays the axes background.

example

lim = axis returnsthe x-axis and y-axis limitsfor the current axes. For 3-D axes, it also returns the z-axislimits. For polar axes, it returns the theta-axisand r-axis limits.

example

___ = axis(ax,___) usesthe axes or polar axes specified by ax insteadof the current axes. Specify ax as the first inputargument for any of the previous syntaxes. Use single quotes aroundinput arguments that are character vectors, such as axis(ax,'equal').

Examples

collapse all

Set Axis Limits

Open Live Script

Plot the sine function.

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

Set axis limits and aspect ratios (1)

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.

axis([0 2*pi -1.5 1.5])

Set axis limits and aspect ratios (2)

Add Padding Around Stairstep Plot

Open Live Script

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

Use Semiautomatic Axis Limits

Open Live Script

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(4*x)./exp(.1*x);plot(x,y)axis([-10 10 0 inf])

Set axis limits and aspect ratios (4)

Set Axis Limits for Multiple Axes

Open Live Script

Starting in R2019b, you can display a tiling of plots using the tiledlayout and nexttile functions. 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])

Set axis limits and aspect ratios (5)

Display Plot Without Axes Background

Open Live Script

Plot a surface without displaying the axes lines and background.

surf(peaks)axis off

Set axis limits and aspect ratios (6)

Use Tight Axis Limits and Return Values

Open Live Script

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.

surf(peaks)axis tight

Set axis limits and aspect ratios (7)

Return the values of the current axis limits.

l = axis
l = 1×6 1.0000 49.0000 1.0000 49.0000 -6.5466 8.0752

Change Direction of Coordinate System

Open Live Script

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

Set axis limits and aspect ratios (8)

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

axis ij

Set axis limits and aspect ratios (9)

Retain Current Axis Limits When Adding New Plots

Open Live Script

Plot a sine wave.

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

Set axis limits and aspect ratios (10)

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 onaxis manualplot(x,y2)hold off

Set axis limits and aspect ratios (11)

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

axis auto

Set axis limits and aspect ratios (12)

Input Arguments

collapse all

limitsAxis limits
four-element vector | six-element vector | eight-element vector

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

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

  • [xmin xmax ymin ymax] — Set the x-axis limits to range from xmin to xmax. Set the y-axis limits to range from ymin to ymax.

  • [xmin xmax ymin ymax zmin zmax] — Also set the z-axis limits to range from zmin to zmax.

  • [xmin xmax ymin ymax zmin zmax cmin cmax] — Also set the color limits. cmin is the data value that corresponds to the first color in the colormap. cmax is the data value that corresponds to the last color in the colormap.

The XLim, YLim, ZLim, and CLim properties for the Axes object store the limit values.

For polar axes, specify the limits in this form:

  • [thetamin thetamax rmin rmax] — Set the theta-axis limits to range from thetamin to thetamax. Set the r-axis limits to range from rmin to rmax.

The ThetaLim and RLim properties for the PolarAxes object store the limit values.

For partially automatic limits, use inf or -inf forthe limits you want the axes to choose automatically. For example, axis([-inf10 0 inf]) lets the axes choose the appropriate minimum x-axislimit and maximum y-axis limit. It uses the specifiedvalues for the maximum x-axis limit and minimum y-axislimit.

Note

If the x-axis, y-axis,or z-axis displays categorical, datetime, orduration values, then use the xlim, ylim, and zlim functionsto set the limits instead.

Example: axis([0 1 0 1])

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

Example: axis([0 inf 0 inf])

modeManual, automatic, or semiautomatic selection of axis limits
manual | auto | 'auto x' | 'auto y' | 'auto z' | 'auto xy' | 'auto xz' | 'auto yz'

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

ValueDescriptionAxes Properties That Change
manual Freeze all axis limits at their current values. Sets XLimMode, YLimMode, and ZLimMode to 'manual'. If you are working with polar axes, then this option sets ThetaLimMode and RLimMode to 'manual'.
autoAutomatically choose all axis limits. Sets XLimMode, YLimMode, and ZLimMode to 'auto'. If you are working with polar axes, then this option sets ThetaLimMode and RLimMode 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 and YLimMode to 'auto'.
'auto xz'Automatically choose the x-axis and z-axis limits. Sets XLimMode and ZLimMode to 'auto'.
'auto yz'Automatically choose the y-axis and z-axis limits. Sets YLimMode and ZLimMode to 'auto'.

Note

You cannot use these options with polar axes.

styleAxis limits and scaling
tight | padded | fill | equal | image | square | vis3d | normal

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

ValueDescriptionAxes Properties That Change
tickalignedIn 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, and ZLimMode change to 'auto'.

XLimitMethod, YLimitMethod, and ZLimitMethod change to 'tickaligned'.

XLim, YLim, and ZLim automatically update to incorporate new data added to the axes. To keep the limits from changing when using hold on, use axis tickaligned manual.

tightFit the axes box tightly around the data by setting the axis limits equal to the range of the data.

XLimMode, YLimMode, and ZLimMode change to 'auto'. If you are working with polar axes, then ThetaLimMode and RLimMode change.

XLimitMethod, YLimitMethod, and ZLimitMethod change to 'tight'.

XLim, YLim, and ZLim 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, and ZLimMode change to 'auto'.

XLimitMethod, YLimitMethod, and ZLimitMethod change to 'padded'.

XLim, YLim, and ZLim automatically update to incorporate new data added to the axes. To keep the limits from changing when using hold on, use axis padded manual.

equalUse the same length for the data units along each axis.

Sets DataAspectRatio to [1 1 1] and sets DataAspectRatioMode and PlotBoxAspectRatioMode to 'manual'. For 2-D views, it also sets the XLimMode and YLimMode appropriately so that the axes fills its allotted space within the parent figure or other container. For 3-D Views, XLimMode, YLimMode, and ZLimMode are set to 'auto' and XLimitMethod, YlimitMethod, and ZLimitMethod are set to 'tight'.

This style disables the default “stretch-to-fill” behavior.

imageUse the same length for the data units along each axis andfit the axes box tightly around the data.

Sets DataAspectRatio to [1 1 1], DataAspectRatioMode to 'manual', and PlotBoxAspectRatioMode to 'auto'. It also sets XLimMode, YLimMode, and ZLimMode to 'auto' and XLimitMethod, YlimitMethod, and ZLimitMethod to 'tight'.

This style disables the default “stretch-to-fill” behavior.

squareUse axis lines with equal lengths. Adjust the increments betweendata 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.

fillEnable the “stretch-to-fill” behavior (the default). The lengths of each axis line fill the position rectangle defined in the Position property of the axes.

Sets DataAspectRatioMode and PlotBoxAspectRatioMode to 'auto'.

vis3dFreeze the aspect ratio properties.

Sets DataAspectRatioMode, PlotBoxAspectRatioMode, and CameraViewAngleMode to 'manual'.

normalRestore the default behavior.

Sets DataAspectRatioMode and PlotBoxAspectRatioMode to 'auto'.

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

Note

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

ydirectiony-axis direction
xy (default) | ij

y-axis direction, specified as one of thesevalues:

  • xy — Default direction.For axes in a 2-D view, the y-axis is verticalwith values increasing from bottom to top.

  • ij — Reverse direction.For axes in a 2-D view, the y-axis is verticalwith values increasing from top to bottom.

Note

You cannot use these options with polar axes.

visibilityAxes lines and background visibility
"on" | "off" | true or 1 | false or 0 | OnOffSwitchState value

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

  • "on" or "off" — A value of "on" displays the axes lines and background, and "off" hides them. You can also specify the character vectors 'on' or 'off'.

  • Numeric or logical 1 (true) or 0 (false) — A value of 1 or true displays the axes lines and background, and 0 or false hides them. (since R2024a)

  • A matlab.lang.OnOffSwitchState value — A value of matlab.lang.OnOffSwitchState.on displays the axes lines and background, and matlab.lang.OnOffSwitchState.off hides them. (since R2024a)

Note

Use parentheses to specify 1, 0, true, false, or an OnOffSwitchState 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.

axTarget axes
one or more axes

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

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

Example: axis(ax,'tight')

Example: axis(ax,limits)

Example: axis(ax,'manual')

Output Arguments

collapse all

lim — Current limit values
four-element vector | six-element vector

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

  • For Cartesian axes in a 2-D view, lim is of the form [xmin xmax ymin ymax]. For axes in a 3-D view, lim is of the form [xmin xmax ymin ymax zmin zmax]. The XLim, YLim, and ZLim properties for the Axes object store the limit values.

  • For polar axes, lim is of the form [thetamin thetamax rmin rmax]. The ThetaLim and RLim properties for the PolarAxes object store the limit values.

Tips

  • You can combine multiple input arguments together, for example, axis image ij. The options are evaluated from left to right. Subsequent options can overwrite properties set by prior ones.

  • If axes do not exist, the axis functioncreates them.

  • Use hold on to keep plotting functionsfrom overriding preset axis limits.

Version History

Introduced before R2006a

expand all

See Also

Functions

  • xlim | ylim | zlim | tiledlayout | nexttile | title | grid

Properties

  • Axes Properties | PolarAxes Properties

Topics

  • Specify Axis Limits
  • Control Ratio of Axis Lengths and Data Unit Lengths

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Set axis limits and aspect ratios (13)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Set axis limits and aspect ratios (2024)
Top Articles
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 5626

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.