Skip to content

Commit 42b88d0

Browse files
authored
Merge pull request matplotlib#28808 from timhoffm/doc-margins
DOC: Add a plot to margins() to visualize the effect
2 parents 9365c97 + 8ea86fe commit 42b88d0

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

doc/_embedded_plots/axes_margins.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
fig, ax = plt.subplots(figsize=(6.5, 4))
5+
x = np.linspace(0, 1, 33)
6+
y = -np.sin(x * 2*np.pi)
7+
ax.plot(x, y, 'o')
8+
ax.margins(0.5, 0.2)
9+
ax.set_title("margins(x=0.5, y=0.2)")
10+
11+
# fix the Axes limits so that the following helper drawings
12+
# cannot change them further.
13+
ax.set(xlim=ax.get_xlim(), ylim=ax.get_ylim())
14+
15+
16+
def arrow(p1, p2, **props):
17+
ax.annotate("", p1, p2,
18+
arrowprops=dict(arrowstyle="<->", shrinkA=0, shrinkB=0, **props))
19+
20+
21+
axmin, axmax = ax.get_xlim()
22+
aymin, aymax = ax.get_ylim()
23+
xmin, xmax = x.min(), x.max()
24+
ymin, ymax = y.min(), y.max()
25+
26+
y0 = -0.8
27+
ax.axvspan(axmin, xmin, color=("orange", 0.1))
28+
ax.axvspan(xmax, axmax, color=("orange", 0.1))
29+
arrow((xmin, y0), (xmax, y0), color="sienna")
30+
arrow((xmax, y0), (axmax, y0), color="orange")
31+
ax.text((xmax + axmax)/2, y0+0.05, "x margin\n* x data range",
32+
ha="center", va="bottom", color="orange")
33+
ax.text(0.55, y0+0.1, "x data range", va="bottom", color="sienna")
34+
35+
x0 = 0.1
36+
ax.axhspan(aymin, ymin, color=("tab:green", 0.1))
37+
ax.axhspan(ymax, aymax, color=("tab:green", 0.1))
38+
arrow((x0, ymin), (x0, ymax), color="darkgreen")
39+
arrow((x0, ymax), (x0, aymax), color="tab:green")
40+
ax.text(x0, (ymax + aymax) / 2, " y margin * y data range",
41+
va="center", color="tab:green")
42+
ax.text(x0, 0.5, " y data range", color="darkgreen")

lib/matplotlib/axes/_base.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -2736,19 +2736,22 @@ def set_ymargin(self, m):
27362736

27372737
def margins(self, *margins, x=None, y=None, tight=True):
27382738
"""
2739-
Set or retrieve autoscaling margins.
2739+
Set or retrieve margins around the data for autoscaling axis limits.
27402740
2741-
The padding added to each limit of the Axes is the *margin*
2742-
times the data interval. All input parameters must be floats
2743-
greater than -0.5. Passing both positional and keyword
2744-
arguments is invalid and will raise a TypeError. If no
2745-
arguments (positional or otherwise) are provided, the current
2741+
This allows to configure the padding around the data without having to
2742+
set explicit limits using `~.Axes.set_xlim` / `~.Axes.set_ylim`.
2743+
2744+
Autoscaling determines the axis limits by adding *margin* times the
2745+
data interval as padding around the data. See the following illustration:
2746+
2747+
.. plot:: _embedded_plots/axes_margins.py
2748+
2749+
All input parameters must be floats greater than -0.5. Passing both
2750+
positional and keyword arguments is invalid and will raise a TypeError.
2751+
If no arguments (positional or otherwise) are provided, the current
27462752
margins will remain unchanged and simply be returned.
27472753
2748-
Specifying any margin changes only the autoscaling; for example,
2749-
if *xmargin* is not None, then *xmargin* times the X data
2750-
interval will be added to each end of that interval before
2751-
it is used in autoscaling.
2754+
The default margins are :rc:`axes.xmargin` and :rc:`axes.ymargin`.
27522755
27532756
Parameters
27542757
----------
@@ -2780,10 +2783,14 @@ def margins(self, *margins, x=None, y=None, tight=True):
27802783
Notes
27812784
-----
27822785
If a previously used Axes method such as :meth:`pcolor` has set
2783-
:attr:`use_sticky_edges` to `True`, only the limits not set by
2784-
the "sticky artists" will be modified. To force all of the
2785-
margins to be set, set :attr:`use_sticky_edges` to `False`
2786+
`~.Axes.use_sticky_edges` to `True`, only the limits not set by
2787+
the "sticky artists" will be modified. To force all
2788+
margins to be set, set `~.Axes.use_sticky_edges` to `False`
27862789
before calling :meth:`margins`.
2790+
2791+
See Also
2792+
--------
2793+
.Axes.set_xmargin, .Axes.set_ymargin
27872794
"""
27882795

27892796
if margins and (x is not None or y is not None):

0 commit comments

Comments
 (0)