xlim

Gets or sets the limits of the x-axis.

Category: Graphics > Formatting and Annotation

Syntax

xlim([xmin xmax])

Sets the limits of the current axes.

xl = xlim

Returns the current limits as a two-element vector.

xlim('auto')

Lets the axes choose limits that fit the data again.

xlim(ax, ___)

Acts on the axes ax.

Description

The limits decide which part of the data is visible. Data outside the range is clipped, not removed. Setting limits turns off automatic fitting for that axis until xlim('auto') is called.

The limits are stored in the XLim property of the axes. To reverse the direction of the axis, set ax.XDir = 'reverse' rather than swapping the limits.

Input arguments

[xmin xmax]
Two numbers with xmin < xmax. -Inf and Inf are allowed and mean "fit the data on that side".

Output arguments

xl
Current limits, [xmin xmax].

Example

Zoom into part of a curve

x = linspace(0, 10, 200);
plot(x, sin(x))
xlim([2 6])
xl = xlim
Output
xl = 1×2

     2     6

See also

ylim axis xticks xscale zlim