axis
Set axis limits and aspect ratios
Syntax
Description
axis
uses a
predefined style to set the limits and scaling. For example, specify
the style as style
equal
to use equal data unit lengths
along each axis.
axis
sets whether MATLAB^{®} automatically
chooses the limits or not. Specify the mode as mode
manual
, auto
,
or one of the semiautomatic options, such as 'auto x'
.
axis
, where ydirection
ydirection
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
is xy
,
which places the origin at the lower left corner. The y values
increase from bottom to top.
axis
, where visibility
visibility
is off
,
turns off the display of the axes background. Plots in the axes still
display. The default for visibility
is on
,
which displays the axes background.
returns
the xaxis and yaxis limits
for the current axes. For 3D axes, it also returns the zaxis
limits. For polar axes, it returns the thetaaxis
and raxis limits.lim
= axis
Examples
Set Axis Limits
Plot the sine function.
x = linspace(0,2*pi);
y = sin(x);
plot(x,y,'o')
Change the axis limits so that the xaxis ranges from $$0$$ to $$2\pi $$ and the yaxis ranges from 1.5 to 1.5.
axis([0 2*pi 1.5 1.5])
Add Padding Around Stairstep Plot
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
Create a plot. Set the limits for the xaxis and set the minimum yaxis limit. Use an automatically calculated value for the maximum yaxis limit.
x = linspace(10,10,200); y = sin(4*x)./exp(.1*x); plot(x,y) axis([10 10 0 inf])
Set Axis Limits for Multiple Axes
Starting in R2019b, you can display a tiling of plots using the tiledlayout
and nexttile
functions. Call the tiledlayout
function to create a 2by1 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])
Display Plot Without Axes Background
Plot a surface without displaying the axes lines and background.
surf(peaks)
axis off
Use Tight Axis Limits and Return Values
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
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
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
Reverse the coordinate system so that the y values increase from top to bottom.
axis ij
Retain Current Axis Limits When Adding New Plots
Plot a sine wave.
x = linspace(0,10); y = sin(x); plot(x,y)
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
If you want the axes to choose the appropriate limits, set the limits mode back to automatic.
axis auto
Input Arguments
limits
— Axis limits
fourelement vector  sixelement vector  eightelement 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 xaxis limits to range fromxmin
toxmax
. Set the yaxis limits to range fromymin
toymax
.[xmin xmax ymin ymax zmin zmax]
— Also set the zaxis limits to range fromzmin
tozmax
.[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 thetaaxis limits to range fromthetamin
tothetamax
. Set the raxis limits to range fromrmin
tormax
.
The ThetaLim
and
RLim
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 xaxis
limit and maximum yaxis limit. It uses the specified
values for the maximum xaxis limit and minimum yaxis
limit.
Note
If the xaxis, yaxis,
or zaxis 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])
mode
— Manual, 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.
Value  Description  Axes 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' . 
auto  Automatically 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 xaxis limits.  Sets XLimMode to
'auto' . 
'auto y'  Automatically choose the yaxis limits.  Sets YLimMode to
'auto' . 
'auto z'  Automatically choose the zaxis limits.  Sets ZLimMode to
'auto' . 
'auto xy'  Automatically choose the xaxis and yaxis limits.  Sets XLimMode and
YLimMode to
'auto' . 
'auto xz'  Automatically choose the xaxis and zaxis limits.  Sets XLimMode and
ZLimMode to
'auto' . 
'auto yz'  Automatically choose the yaxis and zaxis limits.  Sets YLimMode and
ZLimMode to
'auto' . 
Note
You cannot use these options with polar axes.
style
— Axis limits and scaling
tight
 padded
 fill
 equal
 image
 square
 vis3d
 normal
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. 

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

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. 

equal  Use the same length for the data units along each axis.  Sets This style disables the default “stretchtofill” behavior. 
image  Use the same length for the data units along each axis and fit the axes box tightly around the data.  Sets This style disables the default “stretchtofill” behavior. 
square  Use axis lines with equal lengths. Adjust the increments between data units accordingly.  Sets This style disables the default “stretchtofill” behavior. 
fill  Enable the “stretchtofill” behavior (the default). The lengths of each axis
line fill the position rectangle defined in the
Position property of the axes.  Sets 
vis3d  Freeze the aspect ratio properties.  Sets 
normal  Restore the default behavior.  Sets 
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.
ydirection
— yaxis direction
xy
(default)  ij
yaxis direction, specified as one of these values:
xy
— Default direction. For axes in a 2D view, the yaxis is vertical with values increasing from bottom to top.ij
— Reverse direction. For axes in a 2D view, the yaxis is vertical with values increasing from top to bottom.
Note
You cannot use these options with polar axes.
visibility
— Axes lines and background visibility
on
(default)  off
Axes lines and background visibility, specified as either on
or
off
. Specifying the visibility sets the Visible
property of the Axes
object or PolarAxes
object to the specified value.
ax
— Target axes
one or more axes
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
lim
— Current limit values
fourelement vector  sixelement vector
Current limit values, returned as a fourelement or sixelement vector.
For Cartesian axes in a 2D view,
lim
is of the form[xmin xmax ymin ymax]
. For axes in a 3D view,lim
is of the form[xmin xmax ymin ymax zmin zmax]
. TheXLim
,YLim
, andZLim
properties for theAxes
object store the limit values.For polar axes,
lim
is of the form[thetamin thetamax rmin rmax]
. TheThetaLim
andRLim
properties for thePolarAxes
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
function creates them.Use
hold on
to keep plotting functions from overriding preset axis limits.
Version History
See Also
Functions
Properties
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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.
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:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
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
 United Kingdom (English)