Skip to content

Commit a63dfaf

Browse files
authored
Added get_xmargin(), get_ymargin() and get_zmargin() and tests. (#26293)
Co-authored-by: Evgenii Radchenko <ge53fah@mytum.de>
1 parent 1cb514d commit a63dfaf

File tree

8 files changed

+74
-0
lines changed

8 files changed

+74
-0
lines changed

doc/api/axes_api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ Autoscaling and margins
335335
Axes.use_sticky_edges
336336

337337
Axes.margins
338+
Axes.get_xmargin
339+
Axes.get_ymargin
338340
Axes.set_xmargin
339341
Axes.set_ymargin
340342

doc/api/toolkits/mplot3d/axes3d.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Autoscaling and margins
137137
:template: autosummary.rst
138138
:nosignatures:
139139

140+
get_zmargin
140141
set_zmargin
141142
margins
142143
autoscale
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Getters for xmargin, ymargin and zmargin
2+
----------------------------------------
3+
``.Axes.get_xmargin()``, ``.Axes.get_ymargin()`` and ``.Axes3D.get_zmargin()`` methods have been added to return
4+
the margin values set by ``.Axes.set_xmargin()``, ``.Axes.set_ymargin()`` and ``.Axes3D.set_zmargin()``, respectively.

lib/matplotlib/axes/_base.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,38 @@ def use_sticky_edges(self, b):
26172617
self._use_sticky_edges = bool(b)
26182618
# No effect until next autoscaling, which will mark the Axes as stale.
26192619

2620+
def get_xmargin(self):
2621+
"""
2622+
Retrieve autoscaling margin of the x-axis.
2623+
2624+
.. versionadded:: 3.9
2625+
2626+
Returns
2627+
-------
2628+
xmargin : float
2629+
2630+
See Also
2631+
--------
2632+
matplotlib.axes.Axes.set_xmargin
2633+
"""
2634+
return self._xmargin
2635+
2636+
def get_ymargin(self):
2637+
"""
2638+
Retrieve autoscaling margin of the y-axis.
2639+
2640+
.. versionadded:: 3.9
2641+
2642+
Returns
2643+
-------
2644+
ymargin : float
2645+
2646+
See Also
2647+
--------
2648+
matplotlib.axes.Axes.set_ymargin
2649+
"""
2650+
return self._ymargin
2651+
26202652
def set_xmargin(self, m):
26212653
"""
26222654
Set padding of X data limits prior to autoscaling.

lib/matplotlib/axes/_base.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ class _AxesBase(martist.Artist):
242242
def use_sticky_edges(self) -> bool: ...
243243
@use_sticky_edges.setter
244244
def use_sticky_edges(self, b: bool) -> None: ...
245+
def get_xmargin(self) -> float: ...
246+
def get_ymargin(self) -> float: ...
245247
def set_xmargin(self, m: float) -> None: ...
246248
def set_ymargin(self, m: float) -> None: ...
247249

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6153,6 +6153,14 @@ def test_margins():
61536153
ymax + (ymax - ymin) * 0.5)
61546154

61556155

6156+
def test_margin_getters():
6157+
fig = plt.figure()
6158+
ax = fig.add_subplot()
6159+
ax.margins(0.2, 0.3)
6160+
assert ax.get_xmargin() == 0.2
6161+
assert ax.get_ymargin() == 0.3
6162+
6163+
61566164
def test_set_margin_updates_limits():
61576165
mpl.style.use("default")
61586166
fig, ax = plt.subplots()

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,22 @@ def update_datalim(self, xys, **kwargs):
513513
get_autoscalez_on = _axis_method_wrapper("zaxis", "_get_autoscale_on")
514514
set_autoscalez_on = _axis_method_wrapper("zaxis", "_set_autoscale_on")
515515

516+
def get_zmargin(self):
517+
"""
518+
Retrieve autoscaling margin of the z-axis.
519+
520+
.. versionadded:: 3.9
521+
522+
Returns
523+
-------
524+
zmargin : float
525+
526+
See Also
527+
--------
528+
mpl_toolkits.mplot3d.axes3d.Axes3D.set_zmargin
529+
"""
530+
return self._zmargin
531+
516532
def set_zmargin(self, m):
517533
"""
518534
Set padding of Z data limits prior to autoscaling.

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,15 @@ def test_margins():
19941994
assert ax.margins() == (0, 0.1, 0)
19951995

19961996

1997+
def test_margin_getters():
1998+
fig = plt.figure()
1999+
ax = fig.add_subplot(projection='3d')
2000+
ax.margins(0.1, 0.2, 0.3)
2001+
assert ax.get_xmargin() == 0.1
2002+
assert ax.get_ymargin() == 0.2
2003+
assert ax.get_zmargin() == 0.3
2004+
2005+
19972006
@pytest.mark.parametrize('err, args, kwargs, match', (
19982007
(ValueError, (-1,), {}, r'margin must be greater than -0\.5'),
19992008
(ValueError, (1, -1, 1), {}, r'margin must be greater than -0\.5'),

0 commit comments

Comments
 (0)