Documentation

我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

axis

Set axis limits and appearance

Syntax

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

Description

example

axis(limits) sets the x-axis and y-axis limits for the current axes. Specify limits as a four-element of the form [xmin xmax ymin ymax]. To additionally set the z-axis limits, specify a six-element vector. To additionally set the color limits, specify an eight-element vector.

example

axis style uses a predefined style to set the limits and scaling. For example, specify the style as equal to use equal data unit lengths along each axis.

example

axis mode sets whether the axes automatically chooses the limits or not. Specify the mode as manual, auto, or one of the semiautomatic options, for example, 'auto x'.

example

axis ydirection controls the placement of the coordinate system origin and the direction of increasing y values. Set ydirection to ij to place the origin at the upper left corner of the axes. The y values increase from top to bottom. Set ydirection to xy to place the origin at the lower left corner. The y values increase from bottom to top. This is the default value.

example

axis visibility controls the visibility of the axes background. Set visibility to off to turn off the display of the axes background. Plots in the axes still display. Set visibility to on to display the axes background. This is the default value.

example

lim = axis returns the x-axis and y-axis limits for the current axes. For 3-D axes, it additionally returns the z-axis limits.

[m,v,d] = axis('state') returns the current settings for the axis limit selection, the axes visibility, and the y-axis direction.

example

___ = axis(ax,___) uses the axes specified by ax instead of the current axes. You can specify an axes with any of the input or output arguments in the previous syntaxes. When you specify an axes, use single quotes around input arguments that are character strings, for example, axis(ax,'equal').

Examples

collapse all

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 x-axis ranges from $0$ to $2\pi$ and the y-axis ranges from -1.5 to 1.5.

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

Use Semiautomatic Axis Limits

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 for Multiple Axes

Create a figure with two subplots. Plot a sine wave in each subplot. Then, set the axis limits for the subplots to the same values.

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

x2 = linspace(0,5,100);
y2 = sin(x2);
ax2 = subplot(2,1,2);
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.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

collapse all

limits — Axis limitsfour-element vector | six-element vector | eight-element vector

Axis limits, specified as a vector of four, six, or eight elements 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] — Additionally set the z-axis limits to range from zmin to zmax.

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

    Note:   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.

If you specify the limits, then the XLim, YLim, ZLim, and CLim properties for the axes change to the specified values. Additionally, the associated mode properties change to 'manual'.

Example: [0 1 0 1]

Example: [0 1 0 1 0 1]

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

mode — Manual, automatic, or semiautomatic selection of axis limitsmanual | 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.

ValueDescriptionAxes Properties That Change
manual Freeze all axis limits at their current values. Sets XLimMode, YLimMode, and ZLimMode to 'manual'.
autoThe axes automatically chooses all axis limits. Sets XLimMode, YLimMode, and ZLimMode to 'auto'.
'auto x'The axes automatically chooses the x-axis limits. Sets XLimMode to 'auto'.
'auto y'The axes automatically chooses the y-axis limits. Sets YLimMode to 'auto'.
'auto z'The axes automatically chooses the z-axis limits. Sets ZLimMode to 'auto'.
'auto xy'The axes automatically chooses the x-axis and y-axis limits. Sets XLimMode and YLimMode to 'auto'.
'auto xz'The axes automatically chooses the x-axis and z-axis limits. Sets XLimMode and ZLimMode to 'auto'.
'auto yz'The axes automatically chooses the y-axis and z-axis limits. Sets YLimMode and ZLimMode to 'auto'.

style — Axis limits and scalingtight | fill | equal | image | square | vis3d | normal

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

ValueDescriptionAxes Properties That Change
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'. The limits automatically update to incorporate new data added to the axes. To keep the limits from changing when using hold on, use axis tight manual.
equalUse the same length for the data units along each axis. Sets DataAspectRatio to [1 1 1], sets PlotBoxAspectRatio to [3 4 4], and sets the associated mode properties to manual. Disables the "stretch-to-fill" behavior.
imageUse the same length for the data units along each axis and fit the axes box tightly around the data. Sets DataAspectRatio to [1 1 1]and sets the associated mode property to manual. Disables the "stretch-to-fill" behavior.
squareUse 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. Disables the "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 the plot box aspect ratio mode and data aspect ratio mode properties to auto.
vis3dFreeze the aspect ratio properties.Sets the plot box aspect ratio mode and data aspect ratio mode properties to manual.
normalRestore the default behavior.Sets the plot box aspect ratio mode and data aspect ratio mode properties to auto.

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

You can combine style options. The options are evaluated from left to right, so subsequent options can overwrite properties set by prior ones.

ydirection — y-axis directionxy (default) | ij

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

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

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

visibility — Axes lines and background visibilityon (default) | off

Axes lines and background visibility, specified as either on or off. Specifying the visibility sets the Visible property of the axes to the same value.

ax — Axes objectsscalar | vector

Axes objects, specified as a scalar or a vector. If you do not specify an axes, then axis sets the limits for the current axes (gca).

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

Example: axis(ax,'tight')

Example: axis(ax,limits)

Example: axis(ax,'manual')

Output Arguments

collapse all

lim — Current limit valuesfour-element vector | six-element vector

Current limit values, returned as a four-element or six-element vector. For axes with a 2-D view, lim is of the form [xmin xmax ymin ymax]. For axes with a 3-D view, lim is of the form [xmin xmax ymin ymax zmin zmax].

The XLim, YLim, and ZLim properties for the axes contain the limit values.

More About

collapse all

Tips

  • If an axes does not exist, the axis function creates one.

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

See Also

Functions

Properties

Introduced before R2006a

Was this topic helpful?