Change Axis Limits of Graph

This example shows how to change the axis limits of a graph. By default, MATLAB® chooses axis limits to encompass the data plotted.

Create Simple Line Plot

Define x as 200 linearly spaced values between -10 and 10. Define y as the sine of x with an exponentially decreasing amplitude. Create a line plot of the data.

x = linspace(-10,10,200);
y = sin(4*x)./exp(x);

figure
plot(x,y)

Change Axis Limits

Change the axis limits by passing to the axis function a four-element vector of the form [xmin,xmax,ymin,ymax], where xmin and xmax set the scaling for the x-axis, and ymin and ymax set the scaling for the y-axis.

You also can change the axis limits using the xlim, ylim, and zlim functions. The commands xlim([xmin,xmax]) and ylim([ymin,ymax]) produce the same result as axis([xmin,xmax,ymin,ymax]).

Change the x-axis scaling to range from 0 to 10. Change the y-axis scaling to range from -1 to 1.

axis([0,10,-1,1])

Use Semiautomatic Axis Limits

Use an automatically calculated minimum x-axis limit by setting its value to -inf. MATLAB® calculates the limit based on the data. Set the maximum x-axis limit to 10, the minimum y-axis limit to -1, and the maximum y-axis limit to 1.

axis([-inf,10,-1,1])

MATLAB calculates the minimum limit for the x-axis based on the data. To use an automatically calculated maximum limit, set the value to inf.

See Also

| | | |

Related Examples

Was this topic helpful?