Skip to Main Content Skip to Search
Product Documentation

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:

See Defining the View 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).

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS