Skip to content

Commit 1851427

Browse files
committed
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".
1 parent be3608b commit 1851427

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

lib/matplotlib/axes/_subplots.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import matplotlib as mpl
12
from matplotlib import _api, cbook
23
from matplotlib.axes._axes import Axes
34
from matplotlib.gridspec import GridSpec, SubplotSpec
@@ -109,10 +110,13 @@ def label_outer(self):
109110
labels are on the top side); y-labels only for subplots on the first
110111
column (or last column, if labels are on the right side).
111112
"""
112-
self._label_outer_xaxis()
113-
self._label_outer_yaxis()
113+
self._label_outer_xaxis(check_patch=False)
114+
self._label_outer_yaxis(check_patch=False)
114115

115-
def _label_outer_xaxis(self):
116+
def _label_outer_xaxis(self, *, check_patch):
117+
# see documentation in label_outer.
118+
if check_patch and not isinstance(self.patch, mpl.patches.Rectangle):
119+
return
116120
ss = self.get_subplotspec()
117121
label_position = self.xaxis.get_label_position()
118122
if not ss.is_first_row(): # Remove top label/ticklabels/offsettext.
@@ -128,7 +132,10 @@ def _label_outer_xaxis(self):
128132
if self.xaxis.offsetText.get_position()[1] == 0:
129133
self.xaxis.offsetText.set_visible(False)
130134

131-
def _label_outer_yaxis(self):
135+
def _label_outer_yaxis(self, *, check_patch):
136+
# see documentation in label_outer.
137+
if check_patch and not isinstance(self.patch, mpl.patches.Rectangle):
138+
return
132139
ss = self.get_subplotspec()
133140
label_position = self.yaxis.get_label_position()
134141
if not ss.is_first_col(): # Remove left label/ticklabels/offsettext.

lib/matplotlib/figure.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1912,10 +1912,10 @@ def _do_layout(gs, mosaic, unique_ids, nested):
19121912
for ax in ret.values():
19131913
if sharex:
19141914
ax.sharex(ax0)
1915-
ax._label_outer_xaxis()
1915+
ax._label_outer_xaxis(check_patch=True)
19161916
if sharey:
19171917
ax.sharey(ax0)
1918-
ax._label_outer_yaxis()
1918+
ax._label_outer_yaxis(check_patch=True)
19191919
for k, ax in ret.items():
19201920
if isinstance(k, str):
19211921
ax.set_label(k)

lib/matplotlib/gridspec.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,12 @@ def subplots(self, *, sharex=False, sharey=False, squeeze=True,
306306
self[row, col], **subplot_kw)
307307

308308
# turn off redundant tick labeling
309-
if all(ax.name == "rectilinear" for ax in axarr.flat):
310-
if sharex in ["col", "all"]:
311-
for ax in axarr.flat:
312-
ax._label_outer_xaxis()
313-
if sharey in ["row", "all"]:
314-
for ax in axarr.flat:
315-
ax._label_outer_yaxis()
309+
if sharex in ["col", "all"]:
310+
for ax in axarr.flat:
311+
ax._label_outer_xaxis(check_patch=True)
312+
if sharey in ["row", "all"]:
313+
for ax in axarr.flat:
314+
ax._label_outer_yaxis(check_patch=True)
316315

317316
if squeeze:
318317
# Discarding unneeded dimensions that equal 1. If we only have one

lib/matplotlib/tests/test_polar.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,15 @@ def test_remove_shared_polar(fig_ref, fig_test):
396396

397397
def test_shared_polar_keeps_ticklabels():
398398
fig, axs = plt.subplots(
399-
2, 2, subplot_kw=dict(projection="polar"), sharex=True, sharey=True)
399+
2, 2, subplot_kw={"projection": "polar"}, sharex=True, sharey=True)
400400
fig.canvas.draw()
401401
assert axs[0, 1].xaxis.majorTicks[0].get_visible()
402402
assert axs[0, 1].yaxis.majorTicks[0].get_visible()
403+
fig, axs = plt.subplot_mosaic(
404+
"ab\ncd", subplot_kw={"projection": "polar"}, sharex=True, sharey=True)
405+
fig.canvas.draw()
406+
assert axs["b"].xaxis.majorTicks[0].get_visible()
407+
assert axs["b"].yaxis.majorTicks[0].get_visible()
403408

404409

405410
def test_axvline_axvspan_do_not_modify_rlims():

0 commit comments

Comments
 (0)