This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Set or query x-axis limits


xl = xlim
xlim auto
xlim manual
m = xlim('mode')
___ = xlim(ax,___)



xlim(limits) sets 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.


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

xlim auto sets an automatic mode, enabling the axes to determine the x-axis limits. The limits span the range of the plotted data. Use this option if you change the limits and then want to set them back to the default values. This command sets the XLimMode property for the axes to 'auto'.


xlim manual sets a manual mode, freezing 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'.

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


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


collapse all

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

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

Create a surface plot and show only x values greater than 0. Specify the minumum x-axis limit as 0 and let MATLAB choose the maximum limit.

[X,Y,Z] = peaks;
xlim([0 inf])

Create a stem chart with dates along the x-axis. Set the x-axis limits to range from June 1, 2014 to June 5, 2014.

t = datetime(2014,06,1) + caldays(0:10);
y = rand(11,1);

tstart = datetime(2014,06,1);
tend = datetime(2014,06,5);
xlim([tstart tend])

Create a figure with two subplots and assign the Axes objects to the variables ax1 and ax2. Plot the same data in each subplot. Set the x-axis limits for the bottom subplot by specifying ax2 as the first input argument to xlim.

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

ax2 = subplot(2,1,2);
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);

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

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, categorical, 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. For an automatically calculated minimum or maximum limit, use -inf or inf, respectively.

Example: xlim([0 1])

Example: xlim([-inf 1])

Example: xlim([0 inf])

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | 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 object.

Current limits mode, returned as one of these values:

  • 'auto' — Automatically determine the limits.

  • 'manual' — Use manually specified limits that do not update to reflect changes in the data.

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


The xlim function sets and queries several axes properties related to the x-axis limits.

  • XLim — Property that stores the x-axis limits.

  • XLimMode — Property that stores the x-axis limits mode. When you set the x-axis limits, this property changes to 'manual'.

Introduced before R2006a

Was this topic helpful?