| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
When you create a MATLAB graph, the axis limits and tick-mark spacing are automatically selected based on the data plotted. However, you can also specify your own values for axis limits and tick marks with the following functions:
axis — Sets values that affect the current axes object (the most recently created or the last clicked on).
axes — (Not axis) creates a new axes object with the specified characteristics.
get and set — Enable you to query and set a wide variety of properties of existing axes.
gca — Returns the handle (identifier) of the current axes. If there are multiple axes in the figure window, the current axes is the last graph created or the last graph you clicked on with the mouse. The following two sections provide more information and examples:
See Defining the View in the 3-D Visualization documentation for more extensive information on manipulating 3-D views.
By default, axis limits are chosen to encompass the range of the plotted data. You can specify the limits manually using the axis function. Call axis with the new limits defined as a four-element vector.
axis([xmin,xmax,ymin,ymax])
The minimum values must be less than the maximum values.
If you want to autoscale only one of a min/max set of axis limits, but you want to specify the other, use the MATLAB variable Inf or -Inf for the autoscaled limit. For example, this graph uses default scaling.

Compare the default limits to the following graph, which sets the maximum limit of the x-axis, but autoscales the minimum limit.
axis([-Inf 5 2 2.5])

The tick-mark locations are based on the range of data so as to produce equally spaced ticks (for linear graphs). You can specify different tick marks by setting the axes XTick and YTick properties. Define tick marks as a vector of increasing values. The values do not need to be equally spaced.
For example, setting the y-axis tick marks for the graph from the preceding example,
set(gca,'ytick',[2 2.1 2.2 2.3 2.4 2.5])
produces a graph with only the specified ticks on the y-axis.

If you specify tick-mark values that are outside the axis limits, they are not displayed (that is, specifying tick marks cannot cause axis limits to change).
You can adjust the axis tick-mark locations and the labels appearing at each tick mark. For example, this plot of the sine function relabels the x-axis with more meaningful values.
x = -pi:.1:pi;
y = sin(x);
plot(x,y)
set(gca,'XTick',-pi:pi/2:pi)
set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})
These functions (xlabel, ylabel, title, text) add axis labels and draw an arrow that points to the location on the graph where y = sin(-pi/4).
xlabel('-\pi \leq \Theta \leq \pi')
ylabel('sin(\Theta)')
title('Plot of sin(\Theta)')
text(-pi/4,sin(-pi/4),'\leftarrow sin(-\pi\div4)',...
'HorizontalAlignment','left')
Change the line color to purple by first finding the handle of the line object created by plot and then setting its Color property. Use findobj and the fact that the MATLAB line color default is a blue line (RGB value [0 0 1]). In the same statement, set the LineWidth property to 2 points.
set(findobj(gca,'Type','line','Color',[0 0 1]),...
'Color',[0.5,0,0.5],'LineWidth',2)

The Greek symbols are created using TeX character sequences.
By default, graphs display in a rectangular axes that has the same aspect ratio as the figure window. This makes optimum use of space available for plotting. You exercise control over the aspect ratio with the axis function.
For example,
t = 0:pi/20:2*pi; plot(sin(t),2*cos(t)) grid on
produces a graph with the default aspect ratio. The command
axis square
makes the x- and y-axes equal in length.

The square axes has one data unit in x to equal two data units in y. If you want the x- and y-data units to be equal, use the command
axis equal
This produces an axes that is rectangular in shape, but has equal scaling along each axis.

If you want the axes shape to conform to the plotted data, use the tight option in conjunction with equal.
axis equal tight
Note In order to format aspect ratio using axis, axes must exist and contain a plot. That is, you cannot pre-format an axes that has no actual x-, y-, or z-limits. To overcome this, you can preformat the axes with axis and issue the hold on command before plotting data. |
![]() | Plotting with Two Y-Axes | Creating Specialized Plots | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |