diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 25138be1471c..c835eefc28b8 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1354,8 +1354,13 @@ def __clear(self): xaxis_visible = self.xaxis.get_visible() yaxis_visible = self.yaxis.get_visible() - for axis in self._axis_map.values(): + for name, axis in self._axis_map.items(): axis.clear() # Also resets the scale to linear. + # need to do any shared axis scales as well + for other in axis._get_shared_axes(): + if other is self.axes: + continue + other._axis_map[name]._set_scale("linear") for spine in self.spines.values(): spine._clear() # Use _clear to not clear Axis again diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 9e27e9ab0a16..5713c371a341 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -37,6 +37,7 @@ from matplotlib.projections.geo import HammerAxes from matplotlib.projections.polar import PolarAxes import matplotlib.pyplot as plt +import matplotlib.scale as mscales import matplotlib.text as mtext import matplotlib.ticker as mticker import matplotlib.transforms as mtransforms @@ -9264,6 +9265,24 @@ def test_shared_axes_clear(fig_test, fig_ref): ax.plot(x, y) +def test_shared_axes_clear_scale(recwarn): + _, axs = plt.subplots(1, 2, sharey=True) + x = range(1, 10) + axs[0].loglog(x, x) + axs[1].loglog(x, x) + axs[0].clear() + + assert len(recwarn) == 0 + + # the cleared axes has linear on both axis + for axis in axs[0]._axis_map.values(): + assert isinstance(axis._scale, mscales.LinearScale) + + # the linked axes becomes linear on the shared y-axis + assert isinstance(axs[1].xaxis._scale, mscales.LogScale) + assert isinstance(axs[1].yaxis._scale, mscales.LinearScale) + + def test_shared_axes_retick(): fig, axs = plt.subplots(2, 2, sharex='all', sharey='all')