xlim

Set or query x-axis limits

Syntax

``xlim(limits)``
``xl = xlim``
``xlim(limitmethod)``
``xlim(limitmode)``
``m = xlim('mode')``
``___ = xlim(target,___)``

Description

Specific Limits

example

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

example

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

Automatic or Manual Limit Selection

example

````xlim(limitmethod)` specifies the limit method MATLAB® uses for automatic limit selection. Specify the limit method as `'tickaligned'`, `'tight'`, or `'padded'`. MATLAB sets the `XLimitMethod` property of the axes to the value you specify. The limit method is not supported for standalone visualizations.You can specify the `limitmethod` argument without parentheses. For example, `xlim tight` enables tight x-axis limits.```

example

````xlim(limitmode)` specifies automatic or manual limit selection. The `limitmode` can have either of two values: `'auto'` — Enable automatic limit selection. MATLAB selects the limits based on the range of your data and the value of the `XLimitMethod` property of the axes. If you plot into the axes multiple times, the limits update to encompass all the data.`'manual'` — Freeze the x-axis limits at their current value. You can specify the `limitmode` argument without parentheses. For example, `xlim auto` enables automatic limit selection.```
````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.```

Target Axes or Chart

example

````___ = xlim(target,___)` uses the axes or standalone visualization specified by `target` instead of the current axes. Specify `target` 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 the mode inputs, for example, `xlim(target,'auto')`.```

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 surface plot and show only x values greater than 0. Specify the minimum x-axis limit as 0 and let MATLAB choose the maximum limit.

```[X,Y,Z] = peaks; surf(X,Y,Z) 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); stem(t,y,'filled') tstart = datetime(2014,06,1); tend = datetime(2014,06,5); xlim([tstart tend])```

If you want the x-axis to automatically adjust to match the range of your x-coordinates, use the `'tight'` limit method.

Create a line plot.

`plot([1 2 3 4 5 6.3],[0 0.3 0.1 0.6 0.4 1])`

Change the limit method to `'tight'`.

`xlim tight`

Add another plot to the axes. The x-axis limits adjust to encompass the span of the new data.

```hold on plot([1 2 3 4 5 8.3],[0.1 0.2 0.3 0.4 0.5 0.83]) hold off```

Starting in R2019b, you can display a tiling of plots using the `tiledlayout` and `nexttile` functions. Call the `tiledlayout` function to create a 2-by-1 tiled chart layout. Call the `nexttile` function to create the axes objects `ax1` and `ax2`. Plot data into each axes. Then set the x-axis limits for the bottom plot by specifying `ax2` as the first input argument to `xlim`.

```tiledlayout(2,1) x = linspace(0,5,1000); y = sin(100*x)./exp(x); ax1 = nexttile; plot(ax1,x,y) ax2 = nexttile; plot(ax2,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 = 1×2 -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. MATLAB uses the `'tight'` limit method to calculate the corresponding limit.

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`

Limit selection method, specified as a value from the table.

The examples in the table show the approximate appearance for each method. Your results might differ depending on your data, the size of the axes, and the type of plot you create.

ValueDescriptionExample
`'tickaligned'`

In general, align the edges of the axes box with the tick marks that are closest to your data without excluding any data. The appearance might vary depending on the type of data you plot and the type of chart you create.

`'tight'`

Fit the axes box tightly around the data by setting the axis limits to the data range.

`'padded'`

Fit the axes box around the data with a thin margin of padding on each side. The width of the margin is approximately 7% of the data range.

Note

• The limit method has no effect when the `XLimMode` property of the axes is set to `'manual'`.

• Specifying the limit method is not supported for standalone visualizations such as `heatmap` or `stackedplot`.

Limit mode, specified as one of the following values:

• `'auto'` — Enable automatic limit selection, which is based on the total span of the data and the value of the `XLimitMethod` property of the axes. If you plot into the axes multiple times, the limits update to encompass all the data. You can use this option if you change the limits and want to set them back to the default values.

• `'manual'` — Freeze 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.

When you specify this argument, MATLAB sets the `XLimMode` property of the axes to the value you specify. However, the `XLimMode` property changes to `'manual'` whenever you set the x-axis limits explicitly, either by calling `xlim(limits)`, or by setting the value of the `XLim` property on the axes.

Target axes or chart, specified as one of the following:

If you do not specify this argument, then `xlim` sets the limits on the graphics object returned by the `gca` command.

Output Arguments

collapse all

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

Querying the limits returns the `XLim` or `XLimits` property value for corresponding `Axes` or graphics 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 the `XLimMode` property value for the corresponding `Axes` object.

Algorithms

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'`.

• `XLimitMethod` — Property that controls how the x-axis limits are calculated when the `XLimMode` property is set to `'auto'`.

Introduced before R2006a