The limits on an axis can be set manually (e.g.
or Matplotlib can set them automatically based on the data already on the axes.
There are a number of options to this autoscaling behaviour, discussed below.
We will start with a simple line plot showing that autoscaling extends the axis limits 5% beyond the data limits (-2π, 2π).
The default margin around the data limits is 5%:
The margins can be made larger using
In general, margins can be in the range (-0.5, ∞), where negative margins set
the axes limits to a subrange of the data range, i.e. they clip data.
Using a single number for margins affects both axes, a single margin can be
customized using keyword arguments
y, but positional and keyword
interface cannot be combined.
This override of margins is determined by "sticky edges", a
Artist class that can suppress adding margins to axis
limits. The effect of sticky edges can be disabled on an Axes by changing
Artists have a property
Artist.sticky_edges, and the values of
sticky edges can be changed by writing to
The following example shows how overriding works and when it is needed.
fig, ax = plt.subplots(ncols=3, figsize=(16, 10)) ax.imshow(zz) ax.margins(0.2) ax.set_title("default use_sticky_edges\nmargins(0.2)") ax.imshow(zz) ax.margins(0.2) ax.use_sticky_edges = False ax.set_title("use_sticky_edges=False\nmargins(0.2)") ax.imshow(zz) ax.margins(-0.2) ax.set_title("default use_sticky_edges\nmargins(-0.2)")
We can see that setting
use_sticky_edges to False renders the image
with requested margins.
While sticky edges don't increase the axis limits through extra margins, negative margins are still taken into account. This can be seen in the reduced limits of the third image.
By default, the limits are recalculated every time you add a new curve to the plot:
However, there are cases when you don't want to automatically adjust the viewport to new data.
One way to disable autoscaling is to manually set the
axis limit. Let's say that we want to see only a part of the data in
greater detail. Setting the
xlim persists even if we add more curves to
the data. To recalculate the new limits calling
toggle the functionality manually.
fig, ax = plt.subplots(ncols=2, figsize=(12, 8)) ax.plot(x, y) ax.set_xlim(left=-1, right=1) ax.plot(x + np.pi * 0.5, y) ax.set_title("set_xlim(left=-1, right=1)\n") ax.plot(x, y) ax.set_xlim(left=-1, right=1) ax.plot(x + np.pi * 0.5, y) ax.autoscale() ax.set_title("set_xlim(left=-1, right=1)\nautoscale()")
We can check that the first plot has autoscale disabled and that the second
plot has it enabled again by using
print(ax.get_autoscale_on()) # False means disabled print(ax.get_autoscale_on()) # True means enabled -> recalculated
Arguments of the autoscale function give us precise control over the process
of autoscaling. A combination of arguments
axis sets the
autoscaling feature for the selected axis (or both). The argument
sets the margin of the selected axis to zero. To preserve settings of either
tight you can set the opposite one to None, that way
it should not be modified. However, setting
enable to None and tight
to True affects both axes regardless of the
Working with collections#
Autoscale works out of the box for all lines, patches, and images added to
the axes. One of the artists that it won't work with is a
After adding a collection to the axes, one has to manually trigger the
autoscale_view() to recalculate
fig, ax = plt.subplots() collection = mpl.collections.StarPolygonCollection( 5, rotation=0, sizes=(250,), # five point star, zero angle, size 250px offsets=np.column_stack([x, y]), # Set the positions offset_transform=ax.transData, # Propagate transformations of the Axes ) ax.add_collection(collection) ax.autoscale_view()
Total running time of the script: ( 0 minutes 8.987 seconds)