8000 Make set_x/ymargin() update axes limits, just like margins(). · matplotlib/matplotlib@2292f7b · GitHub
[go: up one dir, main page]

Skip to content

Commit 2292f7b

Browse files
committed
Make set_x/ymargin() update axes limits, just like margins().
Previously, setting the margins via set_x/ymargin() would not update axes limits, unlike calling margins(). This appears to be a simple oversight. The change in collections.py is necessary because the difference in timing at which autoscale is executed now exposed a floating point inaccuracy (which gets amplified by the old round_numbers autolimits mode).
1 parent a57ea3d commit 2292f7b

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
@@ -4582,6 +4582,14 @@ def test_margins():
45824582
ymax + (ymax - ymin) * 0.5)
45834583

45844584

4585+
def test_set_margin_updates_limits():
4586+
mpl.style.use("default")
4587+
fig, ax = plt.subplots()
4588+
ax.plot([1, 2], [1, 2])
4589+
ax.set(xscale="log", xmargin=0)
4590+
assert ax.get_xlim() == (1, 2)
4591+
4592+
45854593
def test_length_one_hist():
45864594
fig, ax = plt.subplots()
45874595
ax.hist(1)

0 commit comments

Comments
 (0)
0