From 92cab09a9bb1aed777406680708b5315c0a0999b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Mon, 5 Sep 2022 22:00:45 +0200 Subject: [PATCH 1/3] BUG: fix a regression where tick direction wasn't conserved by Axis.clear() --- lib/matplotlib/axis.py | 3 ++- lib/matplotlib/tests/test_axes.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 1ee97dc9078b..ad0af8ad6360 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -855,7 +855,8 @@ def _reset_major_tick_kw(self, keep_tick_and_label_visibility=False): *keep_tick_and_label_visibility*. """ backup = {name: value for name, value in self._major_tick_kw.items() - if name in ['tick1On', 'tick2On', 'label1On', 'label2On']} + if name in ['tick1On', 'tick2On', 'tickdir', + 'label1On', 'label2On']} self._major_tick_kw.clear() if keep_tick_and_label_visibility: self._major_tick_kw.update(backup) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index a230af2ac1e0..090c2ed8f68c 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -7665,6 +7665,17 @@ def test_2dcolor_plot(fig_test, fig_ref): axs[4].bar(np.arange(10), np.arange(10), color=color.reshape((1, -1))) +@check_figures_equal(extensions=['png']) +def test_tickdir_axes_clear(fig_test, fig_ref): + # see https://github.com/matplotlib/matplotlib/issues/23806 + ax = fig_ref.subplots(1, 1) + ax.tick_params(which='both', axis='both', direction='in') + + ax = fig_test.subplots(1, 1) + ax.tick_params(which='both', axis='both', direction='in') + ax.clear() + + @check_figures_equal(extensions=['png']) def test_shared_axes_clear(fig_test, fig_ref): x = np.arange(0.0, 2*np.pi, 0.01) From e590c5ef06c71a73f24038210cd100b6ac857b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Wed, 7 Sep 2022 10:56:31 +0200 Subject: [PATCH 2/3] ENH: preserve more style parameters in Axis._reset_major_tick_kw (resp minor) --- lib/matplotlib/axis.py | 12 +++++++++--- lib/matplotlib/tests/test_axes.py | 29 ++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index ad0af8ad6360..48006865631c 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -846,6 +846,13 @@ def get_children(self): return [self.label, self.offsetText, *self.get_major_ticks(), *self.get_minor_ticks()] + # style parameters that should be preserved by reset operations + _style_tick_params = [ + 'tick1On', 'tick2On', 'tickdir', + 'label1On', 'label2On', 'labelsize', 'labelcolor', 'labelrotation', + 'size', 'width', 'color', 'pad' + ] + def _reset_major_tick_kw(self, keep_tick_and_label_visibility=False): """ Reset major tick params to defaults. @@ -855,8 +862,7 @@ def _reset_major_tick_kw(self, keep_tick_and_label_visibility=False): *keep_tick_and_label_visibility*. """ backup = {name: value for name, value in self._major_tick_kw.items() - if name in ['tick1On', 'tick2On', 'tickdir', - 'label1On', 'label2On']} + if name in self.__class__._style_tick_params} self._major_tick_kw.clear() if keep_tick_and_label_visibility: self._major_tick_kw.update(backup) @@ -873,7 +879,7 @@ def _reset_minor_tick_kw(self, keep_tick_and_label_visibility=False): *keep_tick_and_label_visibility*. """ backup = {name: value for name, value in self._minor_tick_kw.items() - if name in ['tick1On', 'tick2On', 'label1On', 'label2On']} + if name in self.__class__._style_tick_params} self._minor_tick_kw.clear() if keep_tick_and_label_visibility: self._minor_tick_kw.update(backup) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 090c2ed8f68c..7b5acdbe176f 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -7665,15 +7665,38 @@ def test_2dcolor_plot(fig_test, fig_ref): axs[4].bar(np.arange(10), np.arange(10), color=color.reshape((1, -1))) +@pytest.mark.parametrize( + # separate colliding parameters into 2 sets + 'params', + [ + dict( + which='both', axis='both', + direction='in', length=2, width=5, color="red", pad=0.4, + bottom=False, top=True, left=False, right=True, + labelrotation=90, labelsize=20, + ), + dict( + which='both', axis='both', + colors="red", + bottom=True, top=True, left=True, right=True, + labelbottom=False, labeltop=True, labelleft=False, labelright=True, + ) + ] +) @check_figures_equal(extensions=['png']) -def test_tickdir_axes_clear(fig_test, fig_ref): +def test_persistent_style_axes_clear(fig_test, fig_ref, params): # see https://github.com/matplotlib/matplotlib/issues/23806 + prng = np.random.RandomState(0) + img = prng.random_sample((8, 8)) + ax = fig_ref.subplots(1, 1) - ax.tick_params(which='both', axis='both', direction='in') + ax.tick_params(**params) + ax.imshow(img) ax = fig_test.subplots(1, 1) - ax.tick_params(which='both', axis='both', direction='in') + ax.tick_params(**params) ax.clear() + ax.imshow(img) @check_figures_equal(extensions=['png']) From 48a5382722b218fb7fcb92a5e2fb6c3926075918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Thu, 8 Sep 2022 10:22:44 +0200 Subject: [PATCH 3/3] TST: simplify and comment test --- lib/matplotlib/tests/test_axes.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 7b5acdbe176f..05b55d504d74 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -7666,17 +7666,16 @@ def test_2dcolor_plot(fig_test, fig_ref): @pytest.mark.parametrize( - # separate colliding parameters into 2 sets + # separate tested parameters into two collision-free sets + # (e.g. avoid supplying *color* and *colors* at the same time) 'params', [ dict( - which='both', axis='both', direction='in', length=2, width=5, color="red", pad=0.4, bottom=False, top=True, left=False, right=True, labelrotation=90, labelsize=20, ), dict( - which='both', axis='both', colors="red", bottom=True, top=True, left=True, right=True, labelbottom=False, labeltop=True, labelleft=False, labelright=True, @@ -7686,17 +7685,12 @@ def test_2dcolor_plot(fig_test, fig_ref): @check_figures_equal(extensions=['png']) def test_persistent_style_axes_clear(fig_test, fig_ref, params): # see https://github.com/matplotlib/matplotlib/issues/23806 - prng = np.random.RandomState(0) - img = prng.random_sample((8, 8)) - ax = fig_ref.subplots(1, 1) - ax.tick_params(**params) - ax.imshow(img) + ax.tick_params(which='both', axis='both', **params) ax = fig_test.subplots(1, 1) - ax.tick_params(**params) + ax.tick_params(which='both', axis='both', **params) ax.clear() - ax.imshow(img) @check_figures_equal(extensions=['png'])