Skip to content

Commit b64ab85

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR matplotlib#31609: DOC: Improve autoscaling and margin docs
1 parent f980031 commit b64ab85

3 files changed

Lines changed: 71 additions & 22 deletions

File tree

galleries/users_explain/axes/autoscale.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,41 @@
66
Axis autoscaling
77
================
88
9-
The limits on an axis can be set manually (e.g. ``ax.set_xlim(xmin, xmax)``)
10-
or Matplotlib can set them automatically based on the data already on the Axes.
11-
There are a number of options to this autoscaling behaviour, discussed below.
12-
"""
9+
Basic concept
10+
-------------
1311
14-
# %%
15-
# We will start with a simple line plot showing that autoscaling
16-
# extends the axis limits 5% beyond the data limits (-2π, 2π).
12+
Autoscaling ensures that data is visible within the Axes by automatically adjusting
13+
the axis limits. When you plot data, Matplotlib's autoscaling mechanism updates the
14+
axis limits accordingly.
15+
"""
1716

1817
import matplotlib.pyplot as plt
1918
import numpy as np
2019

2120

22-
x = np.linspace(-2 * np.pi, 2 * np.pi, 100)
21+
x = np.linspace(-6, 6, 201)
2322
y = np.sinc(x)
2423

2524
fig, ax = plt.subplots()
2625
ax.plot(x, y)
2726

2827
# %%
28+
#
29+
# .. _autoscale_margins:
30+
#
2931
# Margins
3032
# -------
31-
# The default margin around the data limits is 5%, which is based on the
32-
# default configuration setting of :rc:`axes.xmargin`, :rc:`axes.ymargin`,
33-
# and :rc:`axes.zmargin`:
33+
# To ensure that the data is not at the very edge of the plot, Matplotlib adds a
34+
# margin around the data limits. Note that the *x* data range in the above plot is
35+
# [-6, 6], but the x-axis limits are slightly wider due to the margin.
36+
#
37+
# The default margin is 5%, defined via
38+
#
39+
# - :rc:`axes.xmargin`
40+
# - :rc:`axes.ymargin`
41+
# - :rc:`axes.zmargin`
3442

35-
print(ax.margins())
43+
print(ax.get_xmargin(), ax.get_ymargin())
3644

3745
# %%
3846
# The margin size can be overridden to make them smaller or larger using
@@ -116,14 +124,14 @@
116124
ax[1].set_title("Two curves")
117125

118126
# %%
119-
# However, there are cases when you don't want to automatically adjust the
120-
# viewport to new data.
127+
# If you don't want automatic updates of the axis limits, either deactivate
128+
# autoscaling with `~.axes.Axes.autoscale` or set the limits
129+
# manually with `~.axes.Axes.set_xlim` / `~.axes.Axes.set_ylim`.
121130
#
122-
# One way to disable autoscaling is to manually set the
123-
# axis limit. Let's say that we want to see only a part of the data in
131+
# Let's say that we want to see only a part of the data in
124132
# greater detail. Setting the ``xlim`` persists even if we add more curves to
125-
# the data. To recalculate the new limits calling `.Axes.autoscale` will
126-
# toggle the functionality manually.
133+
# the data. Calling `.Axes.autoscale` will re-enable the autoscaling and
134+
# recalculate the limits to fit all the data.
127135

128136
fig, ax = plt.subplots(ncols=2, figsize=(12, 8))
129137
ax[0].plot(x, y)

lib/matplotlib/axes/_base.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,6 +2787,13 @@ def set_autoscale_on(self, b):
27872787
Parameters
27882788
----------
27892789
b : bool
2790+
2791+
See Also
2792+
--------
2793+
:ref:`autoscale`
2794+
matplotlib.axes.Axes.autoscale
2795+
matplotlib.axes.Axes.set_autoscalex_on
2796+
matplotlib.axes.Axes.set_autoscaley_on
27902797
"""
27912798
for axis in self._axis_map.values():
27922799
axis._set_autoscale_on(b)
@@ -2825,6 +2832,7 @@ def get_xmargin(self):
28252832
28262833
See Also
28272834
--------
2835+
:ref:`autoscale_margins`
28282836
matplotlib.axes.Axes.set_xmargin
28292837
"""
28302838
return self._xmargin
@@ -2841,6 +2849,7 @@ def get_ymargin(self):
28412849
28422850
See Also
28432851
--------
2852+
:ref:`autoscale_margins`
28442853
matplotlib.axes.Axes.set_ymargin
28452854
"""
28462855
return self._ymargin
@@ -2860,6 +2869,13 @@ def set_xmargin(self, m):
28602869
Parameters
28612870
----------
28622871
m : float greater than -0.5
2872+
2873+
See Also
2874+
--------
2875+
:ref:`autoscale_margins`
2876+
matplotlib.axes.Axes.margins
2877+
matplotlib.axes.Axes.get_xmargin
2878+
28632879
"""
28642880
if m <= -0.5:
28652881
raise ValueError("margin must be greater than -0.5")
@@ -2882,6 +2898,12 @@ def set_ymargin(self, m):
28822898
Parameters
28832899
----------
28842900
m : float greater than -0.5
2901+
2902+
See Also
2903+
--------
2904+
:ref:`autoscale_margins`
2905+
matplotlib.axes.Axes.margins
2906+
matplotlib.axes.Axes.get_ymargin
28852907
"""
28862908
if m <= -0.5:
28872909
raise ValueError("margin must be greater than -0.5")
@@ -2945,6 +2967,7 @@ def margins(self, *margins, x=None, y=None, tight=True):
29452967
29462968
See Also
29472969
--------
2970+
:ref:`autoscale_margins`
29482971
.Axes.set_xmargin, .Axes.set_ymargin
29492972
"""
29502973

@@ -2999,10 +3022,12 @@ def autoscale(self, enable=True, axis='both', tight=None):
29993022
"""
30003023
Autoscale the axis view to the data (toggle).
30013024
3002-
Convenience method for simple axis view autoscaling.
3003-
It turns autoscaling on or off, and then,
3004-
if autoscaling for either axis is on, it performs
3005-
the autoscaling on the specified axis or Axes.
3025+
Convenience method for simple axis view autoscaling. This:
3026+
3027+
- Turns autoscaling on or off (`~.axes.Axes.set_autoscalex_on` /
3028+
`~.axes.Axes.set_autoscaley_on`).
3029+
- Ensures that view limits will get updated when needed. - As view limits
3030+
are lazy-updated, this technically marks the view limits as stale.
30063031
30073032
Parameters
30083033
----------
@@ -3016,6 +3041,13 @@ def autoscale(self, enable=True, axis='both', tight=None):
30163041
If True, first set the margins to zero. Then, this argument is
30173042
forwarded to `~.axes.Axes.autoscale_view` (regardless of
30183043
its value); see the description of its behavior there.
3044+
3045+
See Also
3046+
--------
3047+
:ref:`autoscale`
3048+
matplotlib.axes.Axes.set_autoscale_on
3049+
matplotlib.axes.Axes.set_autoscalex_on
3050+
matplotlib.axes.Axes.set_autoscaley_on
30193051
"""
30203052
if enable is None:
30213053
scalex = True

lib/matplotlib/axis.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,15 @@ def _set_autoscale_on(self, b):
838838
Parameters
839839
----------
840840
b : bool
841+
842+
See Also
843+
--------
844+
matplotlib.axes.Axes.autoscale
845+
matplotlib.axes.Axes.set_autoscale_on
846+
matplotlib.axes.Axes.get_autoscalex_on
847+
matplotlib.axes.Axes.set_autoscalex_on
848+
matplotlib.axes.Axes.get_autoscaley_on
849+
matplotlib.axes.Axes.set_autoscaley_on
841850
"""
842851
if b is not None:
843852
self._autoscale_on = b

0 commit comments

Comments
 (0)