matplotlib.axes.Axes.set_xlim#

Axes.set_xlim(left=None, right=None, *, emit=True, auto=False, xmin=None, xmax=None)[source]#

Set the x-axis view limits.

Parameters:
leftfloat, optional

The left xlim in data coordinates. Passing None leaves the limit unchanged.

The left and right xlims may also be passed as the tuple (left, right) as the first positional argument (or as the left keyword argument).

rightfloat, optional

The right xlim in data coordinates. Passing None leaves the limit unchanged.

emitbool, default: True

Whether to notify observers of limit change.

autobool or None, default: False

Whether to turn on autoscaling of the x-axis. True turns on, False turns off, None leaves unchanged.

xmin, xmaxfloat, optional

They are equivalent to left and right respectively, and it is an error to pass both xmin and left or xmax and right.

Returns:
left, right(float, float)

The new x-axis limits in data coordinates.

Notes

The left value may be greater than the right value, in which case the x-axis values will decrease from left to right.

Examples

>>> set_xlim(left, right)
>>> set_xlim((left, right))
>>> left, right = set_xlim(left, right)

One limit may be left unchanged.

>>> set_xlim(right=right_lim)

Limits may be passed in reverse order to flip the direction of the x-axis. For example, suppose x represents the number of years before present. The x-axis limits might be set like the following so 5000 years ago is on the left of the plot and the present is on the right.

>>> set_xlim(5000, 0)

Examples using matplotlib.axes.Axes.set_xlim#

Bar chart with labels

Bar chart with labels

Broken horizontal bars

Broken horizontal bars

Cross spectral density (CSD)

Cross spectral density (CSD)

EventCollection Demo

EventCollection Demo

Markevery Demo

Markevery Demo

Multicolored lines

Multicolored lines

Contouring the solution space of optimizations

Contouring the solution space of optimizations

Image resampling

Image resampling

Image nonuniform

Image nonuniform

pcolormesh grids and shading

pcolormesh grids and shading

Spectrogram

Spectrogram

Boxplots

Boxplots

Violin plot customization

Violin plot customization

Including upper and lower limits in error bars

Including upper and lower limits in error bars

Bar of pie

Bar of pie

Annotate transform

Annotate transform

AnnotationBbox demo

AnnotationBbox demo

Using a text as a Path

Using a text as a Path

Text rotation angle in data coordinates

Text rotation angle in data coordinates

Mmh Donuts!!!

Mmh Donuts!!!

Plot multiple lines using a LineCollection

Plot multiple lines using a LineCollection

Simple axis pad

Simple axis pad

Anatomy of a figure

Anatomy of a figure

Stock prices over 32 years

Stock prices over 32 years

XKCD

XKCD

Decay

Decay

Rain simulation

Rain simulation

Animated scatter saved as GIF

Animated scatter saved as GIF

Path editor

Path editor

Polygon editor

Polygon editor

Resampling Data

Resampling Data

Zoom modifies other Axes

Zoom modifies other Axes

Custom projection

Custom projection

Apply SVG filter to a line

Apply SVG filter to a line

TickedStroke patheffect

TickedStroke patheffect

Asinh scale

Asinh scale

Loglog aspect

Loglog aspect

Ishikawa Diagram

Ishikawa Diagram

SkewT-logP diagram: using transforms and custom projections

SkewT-logP diagram: using transforms and custom projections

Format date ticks using ConciseDateFormatter

Format date ticks using ConciseDateFormatter

Date Demo Convert

Date Demo Convert

Date tick locators and formatters

Date tick locators and formatters

Multilevel (nested) ticks

Multilevel (nested) ticks

Annotation with units

Annotation with units

Artist tests

Artist tests

Annotated cursor

Annotated cursor

Cursor

Cursor

Span Selector

Span Selector

Path Tutorial

Path Tutorial

Transformations Tutorial

Transformations Tutorial

Specifying colors

Specifying colors

Choosing Colormaps in Matplotlib

Choosing Colormaps in Matplotlib