From 7f65cad75e08fa7e19934fbf23398737bf00c1df Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 8 Oct 2021 11:40:07 +0200 Subject: [PATCH] Move label hiding rectilinear-only check into _label_outer_{x,y}axis. This allows having it both in subplots() (which had that check before) and in subplot_mosaic (which didn't, see test). (All Axes returned by subplots() have the same projection, so `all(ax.name == "rectilinear")` can be replaced by independently checking each Axes.) Also improve the rectilinearity check by testing the shape of the axes patch instead (so that e.g. skewT also participates in outer-only-labels), and add an escape hatch so that Axes.label_outer always does "what it says on, the tin". --- lib/matplotlib/axes/_subplots.py | 15 +++++++++++---- lib/matplotlib/figure.py | 4 ++-- lib/matplotlib/gridspec.py | 13 ++++++------- lib/matplotlib/tests/test_polar.py | 7 ++++++- 4 files changed, 25 insertions(+), 14 deletions(-) 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 ffbb486a139d..06253828c6d5 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1910,10 +1910,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 5ab3888000ec..a10e9c10f0e7 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():