From 5c7fb5b7c927f674cdd74f1841bab886d072c2b7 Mon Sep 17 00:00:00 2001 From: Benjamin Root Date: Thu, 28 May 2020 14:09:40 -0400 Subject: [PATCH] Backport PR #17408: FIX: cancel pending autoscale on manually setting limits --- lib/matplotlib/axes/_base.py | 6 ++++++ lib/matplotlib/tests/test_axes.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 694945d57389..cc0c364ee05e 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3220,6 +3220,9 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, left, right = sorted([left, right], reverse=bool(reverse)) self._viewLim.intervalx = (left, right) + # Mark viewlims as no longer stale without triggering an autoscale. + for ax in self._shared_x_axes.get_siblings(self): + ax._stale_viewlim_x = False if auto is not None: self._autoscaleXon = bool(auto) @@ -3611,6 +3614,9 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, bottom, top = sorted([bottom, top], reverse=bool(reverse)) self._viewLim.intervaly = (bottom, top) + # Mark viewlims as no longer stale without triggering an autoscale. + for ax in self._shared_y_axes.get_siblings(self): + ax._stale_viewlim_y = False if auto is not None: self._autoscaleYon = bool(auto) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 69b460a7e5f0..75636301d064 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6694,3 +6694,33 @@ def test_invisible_axes(): assert fig.canvas.inaxes((200, 200)) is not None ax.set_visible(False) assert fig.canvas.inaxes((200, 200)) is None + + +@pytest.mark.parametrize('auto', (True, False, None)) +def test_unautoscaley(auto): + fig, ax = plt.subplots() + x = np.arange(100) + y = np.linspace(-.1, .1, 100) + ax.scatter(x, y) + + post_auto = ax.get_autoscaley_on() if auto is None else auto + + ax.set_ylim((-.5, .5), auto=auto) + assert post_auto == ax.get_autoscaley_on() + fig.canvas.draw() + assert_array_equal(ax.get_ylim(), (-.5, .5)) + + +@pytest.mark.parametrize('auto', (True, False, None)) +def test_unautoscalex(auto): + fig, ax = plt.subplots() + x = np.arange(100) + y = np.linspace(-.1, .1, 100) + ax.scatter(y, x) + + post_auto = ax.get_autoscalex_on() if auto is None else auto + + ax.set_xlim((-.5, .5), auto=auto) + assert post_auto == ax.get_autoscalex_on() + fig.canvas.draw() + assert_array_equal(ax.get_xlim(), (-.5, .5))