From c22299849589e9a0ca6a6a2f95a84376f932281e Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 11 Aug 2021 02:17:38 -0400 Subject: [PATCH] Fix clear of Axes that are shared. This reverts a portion of a15bc47816e13ff7383c308441cbea7498ac94b0, which cleared information that was only known by Subplots or GridSpec at initialization time, and stored on the ticks. Probably the storage of this information and initialization needs to be thoroughly reviewed so that it is stored in the right place. Fixes #20721. --- lib/matplotlib/axis.py | 9 +++++++-- lib/matplotlib/tests/test_axes.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 887cf97ef140..7feca94bd9d8 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -806,8 +806,13 @@ def clear(self): # Clear the callback registry for this axis, or it may "leak" self.callbacks = cbook.CallbackRegistry() - self._reset_major_tick_kw() - self._reset_minor_tick_kw() + # whether the grids are on + self._major_tick_kw['gridOn'] = ( + mpl.rcParams['axes.grid'] and + mpl.rcParams['axes.grid.which'] in ('both', 'major')) + self._minor_tick_kw['gridOn'] = ( + mpl.rcParams['axes.grid'] and + mpl.rcParams['axes.grid.which'] in ('both', 'minor')) self.reset_ticks() self.converter = None diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index db7050f732f3..aad699d35594 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6961,6 +6961,21 @@ 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_shared_axes_clear(fig_test, fig_ref): + x = np.arange(0.0, 2*np.pi, 0.01) + y = np.sin(x) + + axs = fig_ref.subplots(2, 2, sharex=True, sharey=True) + for ax in axs.flat: + ax.plot(x, y) + + axs = fig_test.subplots(2, 2, sharex=True, sharey=True) + for ax in axs.flat: + ax.clear() + ax.plot(x, y) + + def test_shared_axes_retick(): fig, axs = plt.subplots(2, 2, sharex='all', sharey='all')