Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

## Setting Axis Parameters

### Axis Scaling and Ticks

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 View Overview in the 3-D Visualization documentation for more extensive information on manipulating 3-D views.

### Axis Limits and Ticks

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.

#### Semiautomatic Limits

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])


#### Axis Tick Marks

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).

### Example — Specifying Ticks and Tick Labels

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')


#### Setting Line Properties on an Existing Plot

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.

### Setting Aspect Ratio

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 same as axis normal).

• axis square — makes the current axes region square

• axis equal — sets the aspect ratio so that the data units are the same in every direction

• axis equal tight — sets the aspect ratio so that the data units are the same in every direction and then sets the axis limits to the minimum and maximum values of the data.

 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.