Documentation

xlim

Set or queryx-axis limits

Syntax

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

Description

example

xlim(limits)sets thex-axis limits for the current axes. Specifylimitsas a two-element vector of the form[xmin xmax], wherexmaxis greater thanxmin.

example

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

xlim autosets an automatic mode, enabling the axes to determine thex-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 theXLimModeproperty for the axes to'auto'.

example

xlim manualsets 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 thehold oncommand. This command sets theXLimModeproperty for the axes to“手动”.

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

example

___= xlim(ax,___)uses the axes specified byaxinstead of the current axes. Specifyaxas 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').

Examples

collapse all

Plot a line and set thex-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 onlyxvalues greater than 0. Specify the minumumx-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 thex-axis. Set thex-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])

Create a figure with two subplots and assign theAxesobjects to the variablesax1andax2. Plot the same data in each subplot. Set thex-axis limits for the bottom subplot by specifyingax2as the first input argument toxlim.

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 thex-axis limits mode to manual so that the limits do not change. Usehold onto add a second plot to the axes.

xlimmanualholdonplot(2*x,2*y) holdoff

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

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

xlimauto

Create a scatter plot of random data. Return the values of thex-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], wherexmaxis greater thanxmin. 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 thex-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-inforinf, 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|分类|datetime|duration

Axesobject. If you do not specify anAxesobject, thenxlimsets 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 theXLimproperty value for the correspondingAxesobject.

Current limits mode, returned as one of these values:

  • 'auto'— Automatically determine the limits.

  • “手动”— Use manually specified limits that do not update to reflect changes in the data.

Querying thex-axis limits mode returnsXLimModeproperty value for the correspondingAxesobject.

Algorithms

Thexlimfunction sets and queries several axes properties related to thex-axis limits.

  • XLim— Property that stores thex-axis limits.

  • XLimMode— Property that stores thex-axis limits mode. When you set thex-axis limits, this property changes to“手动”.

Introduced before R2006a

Was this topic helpful?