diff --git a/lib/matplotlib/axes/_subplots.py b/lib/matplotlib/axes/_subplots.py index f92d08440ea5..18faf2e9d086 100644 --- a/lib/matplotlib/axes/_subplots.py +++ b/lib/matplotlib/axes/_subplots.py @@ -1,3 +1,4 @@ +import matplotlib as mpl from matplotlib import _api, cbook from matplotlib.axes._axes import Axes from matplotlib.gridspec import GridSpec, SubplotSpec @@ -109,10 +110,13 @@ def label_outer(self): labels are on the top side); y-labels only for subplots on the first column (or last column, if labels are on the right side). """ - self._label_outer_xaxis() - self._label_outer_yaxis() + self._label_outer_xaxis(check_patch=False) + self._label_outer_yaxis(check_patch=False) - def _label_outer_xaxis(self): + def _label_outer_xaxis(self, *, check_patch): + # see documentation in label_outer. + if check_patch and not isinstance(self.patch, mpl.patches.Rectangle): + return ss = self.get_subplotspec() label_position = self.xaxis.get_label_position() if not ss.is_first_row(): # Remove top label/ticklabels/offsettext. @@ -128,7 +132,10 @@ def _label_outer_xaxis(self): if self.xaxis.offsetText.get_position()[1] == 0: self.xaxis.offsetText.set_visible(False) - def _label_outer_yaxis(self): + def _label_outer_yaxis(self, *, check_patch): + # see documentation in label_outer. + if check_patch and not isinstance(self.patch, mpl.patches.Rectangle): + return ss = self.get_subplotspec() label_position = self.yaxis.get_label_position() if not ss.is_first_col(): # Remove left label/ticklabels/offsettext. diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 2454776370d0..61ef30c8fcb1 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1909,10 +1909,10 @@ def _do_layout(gs, mosaic, unique_ids, nested): for ax in ret.values(): if sharex: ax.sharex(ax0) - ax._label_outer_xaxis() + ax._label_outer_xaxis(check_patch=True) if sharey: ax.sharey(ax0) - ax._label_outer_yaxis() + ax._label_outer_yaxis(check_patch=True) for k, ax in ret.items(): if isinstance(k, str): ax.set_label(k) diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 74b35d41797d..be004fcf9868 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -306,13 +306,12 @@ def subplots(self, *, sharex=False, sharey=False, squeeze=True, self[row, col], **subplot_kw) # turn off redundant tick labeling - if all(ax.name == "rectilinear" for ax in axarr.flat): - if sharex in ["col", "all"]: - for ax in axarr.flat: - ax._label_outer_xaxis() - if sharey in ["row", "all"]: - for ax in axarr.flat: - ax._label_outer_yaxis() + if sharex in ["col", "all"]: + for ax in axarr.flat: + ax._label_outer_xaxis(check_patch=True) + if sharey in ["row", "all"]: + for ax in axarr.flat: + ax._label_outer_yaxis(check_patch=True) if squeeze: # Discarding unneeded dimensions that equal 1. If we only have one diff --git a/lib/matplotlib/tests/test_polar.py b/lib/matplotlib/tests/test_polar.py index ea3cdfa12917..85aece5fce1a 100644 --- a/lib/matplotlib/tests/test_polar.py +++ b/lib/matplotlib/tests/test_polar.py @@ -396,10 +396,15 @@ def test_remove_shared_polar(fig_ref, fig_test): def test_shared_polar_keeps_ticklabels(): fig, axs = plt.subplots( - 2, 2, subplot_kw=dict(projection="polar"), sharex=True, sharey=True) + 2, 2, subplot_kw={"projection": "polar"}, sharex=True, sharey=True) fig.canvas.draw() assert axs[0, 1].xaxis.majorTicks[0].get_visible() assert axs[0, 1].yaxis.majorTicks[0].get_visible() + fig, axs = plt.subplot_mosaic( + "ab\ncd", subplot_kw={"projection": "polar"}, sharex=True, sharey=True) + fig.canvas.draw() + assert axs["b"].xaxis.majorTicks[0].get_visible() + assert axs["b"].yaxis.majorTicks[0].get_visible() def test_axvline_axvspan_do_not_modify_rlims():