ylim - Set or query y-axis limits - MATLAB (original) (raw)

Set or query _y_-axis limits

Syntax

Description

Specify Limits

ylim([limits](#buob47u-limits)) sets the _y_-axis limits for the current axes or chart. Specify limits as a two-element vector of the form [ymin ymax], whereymax is greater than ymin.

example

ylim([limitmethod](#mw%5F0d6a2783-49e7-4c9e-abec-6ec410a6fab5)) specifies the limit method MATLAB® uses for automatic limit selection. Specify the limit method as"tickaligned", "tight", or"padded". MATLAB sets the YLimitMethod 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, ylim tight enables tight_y_-axis limits.

example

ylim([limitmode](#mw%5F4f5c8d45-b98a-4ee4-85e0-3f7b9e4e2121)) specifies automatic or manual limit selection. The limitmode can have either of two values:

You can specify the limitmode argument without parentheses. For example, ylim auto enables automatic limit selection.

example

Query Limits

[yl](#buob47u-yl) = ylim returns the current limits as a two-element vector.

example

[limmethod](#mw%5F89cb6e36-4ee6-4f3d-b75a-8e06ce214d07) = ylim("method") returns the current_y_-axis limits method, which can be'tickaligned', 'tight', or'padded'.

[limmode](#buob47u-m) = ylim("mode") returns the current _y_-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.

Specify Target Axes or Chart

___ = ylim([target](#d126e2047539),___) uses the axes or standalone visualization specified bytarget instead of the current axes. Specifytarget 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 quotes around the mode inputs, for example,ylim(target,"auto").

example

Examples

collapse all

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

Figure contains an axes object. The axes object contains an object of type line.

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

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

Figure contains an axes object. The axes object contains an object of type surface.

Create a horizontal bar chart with dates along the y-axis. Set the y-axis limits to range from June 1, 2014 to June 10, 2014.

t = datetime(2014,06,1) + caldays(0:20); y = rand(21,1); barh(t,y)

tstart = datetime(2014,06,1); tend = datetime(2014,06,10); ylim([tstart tend])

Figure contains an axes object. The axes object contains an object of type bar.

Create a line plot.

x = 1:5; y = [-2.7 2 -1 4.2 0]; plot(x,y)

Figure contains an axes object. The axes object contains an object of type line.

Change the limit method to 'tight'.

Figure contains an axes object. The axes object contains an object of type line.

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 _y_-axis limits for the bottom plot by specifying ax2 as the first input argument to ylim.

tiledlayout(2,1) x = linspace(0,10,1000); y = sin(10*x).exp(.5x); ax1 = nexttile; plot(ax1,x,y)

ax2 = nexttile; plot(ax2,x,y) ylim(ax2,[-10 10])

Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line.

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)

Figure contains an axes object. The axes object contains an object of type line.

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

Figure contains an axes object. The axes object contains 2 objects of type line.

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

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

Figure contains an axes object. The axes object contains 2 objects of type line.

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)

Figure contains an axes object. The axes object contains an object of type scatter.

Input Arguments

collapse all

Minimum and maximum limits, specified as a two-element vector of the form [ymin ymax], where ymax is greater than ymin. 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 _y_-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 orinf, respectively. MATLAB uses the 'tight' limit method to calculate the corresponding limit.

Example: ylim([0 1])

Example: ylim([-inf 1])

Example: ylim([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.

Value Description Example
"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. Line plot with "tickaligned" limit method.
"tight" Fit the axes box tightly around the data by setting the axis limits to the data range. Line plot with "tight" limit method.
"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. Line plot with "padded" limit method.

Note

Limit mode, specified as one of the following values:

When you specify this argument, MATLAB sets the YLimMode property of the axes to the value you specify. However, the YLimMode property changes to "manual" whenever you set the_y_-axis limits explicitly, either by callingylim(limits), or by setting the value of theYLim property on the axes.

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

If you do not specify this argument, then ylim 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 [ymin ymax].

Querying the limits returns the YLim or YLimits property value for the corresponding Axes or graphics object.

Current limits method, returned as one of these values:

Querying the _y_-axis limits method returns theYLimitMethod property value for the corresponding Axes object.

Current limits mode, returned as one of these values:

Querying the _y_-axis limits mode returns the YLimMode property value for the corresponding Axes object.

Algorithms

The ylim function sets and queries several axes properties related to the _y_-axis limits.

Version History

Introduced before R2006a

expand all

Query the current axis limit method using the "method" input argument.

Set the method that MATLAB uses for automatic limit selection by specifying thelimitmethod argument. This argument can have a value of'tickaligned', 'tight', or'padded'.