From 2292f7be07310a004bbc0fea87a4f32a766e4791 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 27 Jan 2020 10:24:08 +0100 Subject: [PATCH] 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). --- lib/matplotlib/axes/_base.py | 8 ++++---- lib/matplotlib/tests/test_axes.py | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 8f60cfeddf3a..00804b8ecf2e 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -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): @@ -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): @@ -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 diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index e2b1461ee9c4..ae256aafe2f3 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -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)