8000 Merge branch 'agg-pybind11' into pybind11-cleanup · matplotlib/matplotlib@6635712 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6635712

Browse files
committed
Merge branch 'agg-pybind11' into pybind11-cleanup
2 parents 81c2717 + 3fde41c commit 6635712

File tree

4 files changed

+93
-14
lines changed

4 files changed

+93
-14
lines changed

doc/_embedded_plots/axes_margins.py

Lines 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

Lines changed: 20 additions & 13 deletions
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):

lib/mpl_toolkits/axes_grid1/axes_size.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class (or others) to determine the size of each Axes. The unit
77
Note that this class is nothing more than a simple tuple of two
88
floats. Take a look at the Divider class to see how these two
99
values are used.
10+
11+
Once created, the unit classes can be modified by simple arithmetic
12+
operations: addition /subtraction with another unit type or a real number and scaling
13+
(multiplication or division) by a real number.
1014
"""
1115

1216
from numbers import Real
@@ -17,14 +21,33 @@ class (or others) to determine the size of each Axes. The unit
1721

1822
class _Base:
1923
def __rmul__(self, other):
24+
return self * other
25+
26+
def __mul__(self, other):
27+
if not isinstance(other, Real):
28+
return NotImplemented
2029
return Fraction(other, self)
2130

31+
def __div__(self, other):
32+
return (1 / other) * self
33+
2234
def __add__(self, other):
2335
if isinstance(other, _Base):
2436
return Add(self, other)
2537
else:
2638
return Add(self, Fixed(other))
2739

40+
def __neg__(self):
41+
return -1 * self
42+
43+
def __radd__(self, other):
44+
# other cannot be a _Base instance, because A + B would trigger
45+
# A.__add__(B) first.
46+
return Add(self, Fixed(other))
47+
48+
def __sub__(self, other):
49+
return self + (-other)
50+
2851
def get_size(self, renderer):
2952
"""
3053
Return two-float tuple with relative and absolute sizes.

src/_backend_agg_wrapper.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,14 @@ PyRendererAgg_draw_gouraud_triangles(RendererAgg *self,
188188

189189
PYBIND11_MODULE(_backend_agg, m)
190190
{
191-
_import_array();
191+
auto ia = [m]() -> const void* {
192+
import_array();
193+
return &m;
194+
};
195+
if (ia() == NULL) {
196+
throw py::error_already_set();
197+
}
198+
192199
py::class_<RendererAgg>(m, "RendererAgg", py::buffer_protocol())
193200
.def(py::init<unsigned int, unsigned int, double>(),
194201
"width"_a, "height"_a, "dpi"_a)

0 commit comments

Comments
 (0)
0