8000 Merge pull request #16340 from anntzer/set_xmargin-autoscale · matplotlib/matplotlib@42b2dff · GitHub
[go: up one dir, main page]

Skip to content

Commit 42b2dff

Browse files
authored
Merge pull request #16340 from anntzer/set_xmargin-autoscale
Make set_x/ymargin() update axes limits, just like margins().
2 parents 5698325 + 2292f7b commit 42b2dff

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,7 @@ def set_xmargin(self, m):
22942294
if m <= -0.5:
22952295
raise ValueError("margin must be greater than -0.5")
22962296
self._xmargin = m
2297+
self._request_autoscale_view(scalex=True, scaley=False)
22972298
self.stale = True
22982299

22992300
def set_ymargin(self, m):
@@ -2316,6 +2317,7 @@ def set_ymargin(self, m):
23162317
if m <= -0.5:
23172318
raise ValueError("margin must be greater than -0.5")
23182319
self._ymargin = m
2320+
self._request_autoscale_view(scalex=False, scaley=True)
23192321
self.stale = True
23202322

23212323
def margins(self, *margins, x=None, y=None, tight=True):
@@ -2386,15 +2388,13 @@ def margins(self, *margins, x=None, y=None, tight=True):
23862388
cbook._warn_external(f'ignoring tight={tight!r} in get mode')
23872389
return self._xmargin, self._ymargin
23882390

2391+
if tight is not None:
2392+
self._tight = tight
23892393
if x is not None:
23902394
self.set_xmargin(x)
23912395
if y is not None:
23922396
self.set_ymargin(y)
23932397

2394-
self._request_autoscale_view(
2395-
tight=tight, scalex=(x is not None), scaley=(y is not None)
2396-
)
2397-
23982398
def set_rasterization_zorder(self, z):
23992399
"""
24002400
Parameters

lib/matplotlib/tests/test_axes.py

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

46044604

4605+
def test_set_margin_updates_limits():
4606+
mpl.style.use("default")
4607+
fig, ax = plt.subplots()
4608+
ax.plot([1, 2], [1, 2])
4609+
ax.set(xscale="log", xmargin=0)
4610+
assert ax.get_xlim() == (1, 2)
4611+
4612+
46054613
def test_length_one_hist():
46064614
fig, ax = plt.subplots()
46074615
ax.hist(1)

0 commit comments

Comments
 (0)
0