# ylim

Set or query y-axis limits

## Syntax

• `ylim(limits)`
• `ylim auto`
• `ylim manual`
• `yl = ylim`
• `m = ylim('mode')`
• `___ = ylim(ax,___)`

## Description

````ylim(limits)` sets the y-axis limits for the current axes. Specify `limits` as a two-element vector of the form `[ymin ymax]`, where `ymax` is a numeric value greater than `ymin`.```
````ylim auto` lets the axes choose the y-axis limits. The axes chooses limits that span the range of the plotted data. This command sets the `YLimMode` property for the axes to `'auto'`.```
````ylim 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 `YLimMode` property for the axes to `'manual'`.```
````yl = ylim` returns a two-element vector containing the current limits.```
````m = ylim('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.```
````___ = ylim(ax,___)` uses the axes specified by `ax` instead of the current axes. Specify an axes with any of the input or output arguments in previous syntaxes. Use single quotes around input arguments that are character strings, for example, `ylim(ax,'auto')` and `ylim(ax,'manual')`.```

## Examples

collapse all

### Set y-Axis Limits

Plot a line and set the y-axis limits to range from -2 to 2.

```x = linspace(0,10); y = sin(x); plot(x,y) ylim([-2 2]) ```

### Set y-Axis Limits for Specific Axes

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

```x = linspace(0,10,1000); y = sin(10*x).*exp(.5*x); ax1 = subplot(2,1,1); plot(x,y) ax2 = subplot(2,1,2); plot(x,y) ylim(ax2,[-10 10]) ```

### Maintain Current y-Axis Limits

Use manual mode to maintain the current y-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 y-axis limits mode to manual so that the limits to not change. Use `hold on` to add a second plot to the axes.

```ylim manual hold on y2 = 2*sin(x); plot(x,y2) hold off ```

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

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

```ylim auto ```

### Return y-Axis Limits

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

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

```yl = ylim ```
```yl = -2 3 ```

## Input Arguments

collapse all

### `limits` — Minimum and maximum limitstwo-element vector

Minimum and maximum limits, specified as a two-element vector of the form `[ymin ymax]`. Specifying the y-axis limits sets the y-axis limits mode to manual. The `YLim` and `YLimMode` properties for the corresponding axes store the y-axis limits and the limits mode, respectively.

Changing the y-axis limits can cause other limits to change, unless their corresponding mode properties are set to manual.

Example: `[0 1]`

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

### `ax` — Axes objectaxes object

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

## Output Arguments

collapse all

### `yl` — Current limitstwo-element vector

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

Querying the limits returns the `YLim` property value for the corresponding axes.

### `m` — Current limits mode`'auto'` | `'manual'`

Current limits mode, returned as one of these values:

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

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

Querying the y-axis limits mode returns `YLimMode` property value for the corresponding axes.