Documentation

This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

xlim

Set or query x-axis limits

Description

example

xlim(limits) specifies the x-axis limits for the current axes. Specify limits as a two-element vector of the form [xmin xmax], where xmax is greater than xmin.

xlim auto lets the axes choose the x-axis limits. The axes chooses limits that span the range of the plotted data. This command sets the XLimMode property for the axes to 'auto'.

example

xlim manual freezes the limits at the current values. Use this option if you want to retain the current limits when adding new data to the axes using the hold on command. This command sets the XLimMode property for the axes to 'manual'.

example

xl = xlim returns a two-element vector containing the current limits.

m = xlim('mode') returns the current value of the limits mode, which is either 'auto' or 'manual'. By default, the mode is automatic unless you specify limits or set the mode to manual.

example

___ = xlim(ax,___) uses the axes specified by ax instead of the current axes. The ax input argument can precede any of the input arguments in the previous syntaxes. Use single quotes around input arguments that are character vectors, for example, xlim(ax,'auto') and xlim(ax,'manual'). You can include any of the output arguments in the preceding syntaxes if the original syntax supports an output argument.

Examples

collapse all

Plot a line and set the x-axis limits to range from 0 to 5.

x = linspace(0,10);
y = sin(x);
plot(x,y)
xlim([0 5])

Create a figure with two subplots and plot the same data in each subplot. Set the x-axis limits for the bottom subplot.

x = linspace(0,5,1000);
y = sin(100*x)./exp(x);
ax1 = subplot(2,1,1);
plot(x,y)

ax2 = subplot(2,1,2);
plot(x,y)
xlim(ax2,[0 1])

Use manual mode to maintain the current x-axis limits when you add more plots to the axes.

First, plot a line.

x = linspace(0,10);
y = sin(x);
plot(x,y);

Set the x-axis limits mode to manual so that the limits do not change. Use hold on to add a second plot to the axes.

xlim manual
hold on
plot(2*x,2*y)
hold off

The x-axis limits do not update to incorporate the new plot.

Switch back to automatically updated limits by resetting the mode to automatic.

xlim auto

Create a scatter plot of random data. Return the values of the x-axis limits.

x = randn(50,1);
y = randn(50,1);
scatter(x,y)

xl = xlim
xl =

-3     4

Input Arguments

collapse all

Minimum and maximum limits, specified as a two-element vector of the form [xmin xmax], where xmax is greater than xmin. You can specify the limits as numeric, datetime, or duration values. However, the type of values that you specify must match the type of values along the x-axis.

You can specify both limits, or specify one limit and let MATLAB® automatically calculate the other.

• To automatically set the minimum limit to the minimum data value, specify the first element as -inf, for example, xlim([-inf 0]).

• To automatically set the maximum limit to the maximum data value, specify the second element as inf, for example, xlim([0 inf]).

When you specify the limits, the XLim property for the axes object updates to the specified values, and the XLimMode property changes to 'manual'. Changing the x-axis limits can cause other limits to change, unless their corresponding modes are manual.

Example: [0 1]

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | datetime | duration

Axes object. If you do not specify an axes object, then xlim sets the limits for the current axes (gca).

Output Arguments

collapse all

Current limits, returned as a two-element vector of the form [xmin xmax].

Querying the limits returns the XLim property value for the corresponding axes.

Current limits mode, returned as one of these values:

• 'auto' — The limits automatically update to reflect changes in the data.

• 'manual' — The limits do not update to reflect changes in the data.

Querying the x-axis limits mode returns XLimMode property value for the corresponding axes.