Skip to content

Make set_x/ymargin() update axes limits, just like margins(). #16340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,7 @@ def set_xmargin(self, m):
if m <= -0.5:
raise ValueError("margin must be greater than -0.5")
self._xmargin = m
self._request_autoscale_view(scalex=True, scaley=False)
self.stale = True

def set_ymargin(self, m):
Expand All @@ -2316,6 +2317,7 @@ def set_ymargin(self, m):
if m <= -0.5:
raise ValueError("margin must be greater than -0.5")
self._ymargin = m
self._request_autoscale_view(scalex=False, scaley=True)
self.stale = True

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

if tight is not None:
self._tight = tight
if x is not None:
self.set_xmargin(x)
if y is not None:
self.set_ymargin(y)

self._request_autoscale_view(
tight=tight, scalex=(x is not None), scaley=(y is not None)
)

def set_rasterization_zorder(self, z):
"""
Parameters
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4582,6 +4582,14 @@ def test_margins():
ymax + (ymax - ymin) * 0.5)


def test_set_margin_updates_limits():
mpl.style.use("default")
fig, ax = plt.subplots()
ax.plot([1, 2], [1, 2])
ax.set(xscale="log", xmargin=0)
assert ax.get_xlim() == (1, 2)


def test_length_one_hist():
fig, ax = plt.subplots()
ax.hist(1)
Expand Down