Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 06bba7e

Browse filesBrowse files
committed
DOC: Add a plot to margins() to visualize the effect
1 parent 36af98e commit 06bba7e
Copy full SHA for 06bba7e

File tree

Expand file treeCollapse file tree

2 files changed

+56
-38
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+56
-38
lines changed

‎doc/_embedded_plots/axes_margins.py

Copy file name to clipboard
+42Lines changed: 42 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+14-38Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,47 +2736,19 @@ 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
2746-
margins will remain unchanged and simply be returned.
2747-
2748-
.. plot::
2749-
2750-
import numpy as np
2751-
import matplotlib.pyplot as plt
2752-
2753-
x, y = np.meshgrid(np.linspace(0, 1, 10), np.linspace(0, 1, 10))
2754-
fig, ax = plt.subplots()
2755-
ax.plot(x, y, 'o', color='lightblue')
2756-
ax.margins(1.5, 0.5)
2757-
ax.set_title("margins(x=1.5, y=0.5)")
2758-
2759-
def arrow(p1, p2, **props):
2760-
ax.annotate("", p1, p2,
2761-
arrowprops=dict(arrowstyle="<->", shrinkA=0, shrinkB=0, **props))
2741+
Autoscaling determines the axis limits by adding *margin* times the
2742+
data interval as padding around the data. See the following illustration:
27622743
2763-
arrow((-1.5, 0), (0, 0), color="orange")
2764-
arrow((0, 0), (1, 0), color="sienna")
2765-
arrow((1, 0), (2.5, 0), color="orange")
2766-
ax.text(-0.75, -0.1, "x margin * x data range", ha="center",
2767-
color="orange")
2768-
ax.text(0.5, -0.1, "x data range", ha="center", color="sienna")
2744+
.. plot:: _embedded_plots/axes_margins.py
27692745
2770-
arrow((1, -0.5), (1, 0), color="tab:green")
2771-
arrow((1, 0), (1, 1), color="darkgreen")
2772-
arrow((1, 1), (1, 1.5), color="tab:green")
2773-
ax.text(1.1, 1.25, "y margin * y data range", color="tab:green")
2774-
ax.text(1.1, 0.5, "y data range", color="darkgreen")
2746+
All input parameters must be floats greater than -0.5. Passing both
2747+
positional and keyword arguments is invalid and will raise a TypeError.
2748+
If no arguments (positional or otherwise) are provided, the current
2749+
margins will remain unchanged and simply be returned.
27752750
2776-
Specifying any margin changes only the autoscaling; for example,
2777-
if *xmargin* is not None, then *xmargin* times the X data
2778-
interval will be added to each end of that interval before
2779-
it is used in autoscaling.
2751+
The default margins are :rc:`axes.xmargin` and :rc:`axes.ymargin`.
27802752
27812753
Parameters
27822754
----------
@@ -2809,9 +2781,13 @@ def arrow(p1, p2, **props):
28092781
-----
28102782
If a previously used Axes method such as :meth:`pcolor` has set
28112783
:attr:`use_sticky_edges` to `True`, only the limits not set by
2812-
the "sticky artists" will be modified. To force all of the
2784+
the "sticky artists" will be modified. To force all
28132785
margins to be set, set :attr:`use_sticky_edges` to `False`
28142786
before calling :meth:`margins`.
2787+
2788+
See Also
2789+
--------
2790+
set_xmargin, set_ymargin
28152791
"""
28162792

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.