Skip to content

Commit 8440f86

Browse files
committed
Share subplots() label visibility handling with label_outer().
This allows label_outer() to also benefit from handling of top-or-right labeled axes (not only for the tick labels, but also for the axis labels themselves).
1 parent b681abc commit 8440f86

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

lib/matplotlib/axes/_subplots.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,44 @@ def label_outer(self):
117117
"""
118118
Only show "outer" labels and tick labels.
119119
120-
x-labels are only kept for subplots on the last row; y-labels only for
121-
subplots on the first column.
120+
x-labels are only kept for subplots on the last row (or first row, if
121+
labels are on the top side); y-labels only for subplots on the first
122+
column (or last column, if labels are on the right side).
122123
"""
124+
self._label_outer_xaxis()
125+
self._label_outer_yaxis()
126+
127+
def _label_outer_xaxis(self):
128+
ss = self.get_subplotspec()
129+
label_position = self.xaxis.get_label_position()
130+
if not ss.is_first_row(): # Remove top label/ticklabels/offsettext.
131+
if label_position == "top":
132+
self.set_xlabel("")
133+
self.xaxis.set_tick_params(which="both", labeltop=False)
134+
if self.xaxis.offsetText.get_position()[1] == 1:
135+
self.xaxis.offsetText.set_visible(False)
136+
if not ss.is_last_row(): # Remove bottom label/ticklabels/offsettext.
137+
if label_position == "bottom":
138+
self.set_xlabel("")
139+
self.xaxis.set_tick_params(which="both", labelbottom=False)
140+
if self.xaxis.offsetText.get_position()[1] == 0:
141+
self.xaxis.offsetText.set_visible(False)
142+
143+
def _label_outer_yaxis(self):
123144
ss = self.get_subplotspec()
124-
lastrow = ss.is_last_row()
125-
firstcol = ss.is_first_col()
126-
if not lastrow:
127-
for label in self.get_xticklabels(which="both"):
128-
label.set_visible(False)
129-
self.xaxis.get_offset_text().set_visible(False)
130-
self.set_xlabel("")
131-
if not firstcol:
132-
for label in self.get_yticklabels(which="both"):
133-
label.set_visible(False)
134-
self.yaxis.get_offset_text().set_visible(False)
135-
self.set_ylabel("")
145+
label_position = self.xaxis.get_label_position()
146+
if not ss.is_first_col(): # Remove left label/ticklabels/offsettext.
147+
if label_position == "left":
148+
self.set_xlabel("")
149+
self.yaxis.set_tick_params(which="both", labelleft=False)
150+
if self.yaxis.offsetText.get_position()[0] == 0:
151+
self.yaxis.offsetText.set_visible(False)
152+
if not ss.is_last_col(): # Remove right label/ticklabels/offsettext.
153+
if label_position == "right":
154+
self.set_xlabel("")
155+
self.yaxis.set_tick_params(which="both", labelright=False)
156+
if self.yaxis.offsetText.get_position()[0] == 1:
157+
self.yaxis.offsetText.set_visible(False)
136158

137159
def _make_twin_axes(self, *args, **kwargs):
138160
"""Make a twinx axes of self. This is used for twinx and twiny."""

lib/matplotlib/gridspec.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -309,23 +309,11 @@ def subplots(self, *, sharex=False, sharey=False, squeeze=True,
309309

310310
# turn off redundant tick labeling
311311
if sharex in ["col", "all"]:
312-
for ax in axarr[:-1, :].flat: # Remove bottom labels/offsettexts.
313-
ax.xaxis.set_tick_params(which="both", labelbottom=False)
314-
if ax.xaxis.offsetText.get_position()[1] == 0:
315-
ax.xaxis.offsetText.set_visible(False)
316-
for ax in axarr[1:, :].flat: # Remove top labels/offsettexts.
317-
ax.xaxis.set_tick_params(which="both", labeltop=False)
318-
if ax.xaxis.offsetText.get_position()[1] == 1:
319-
ax.xaxis.offsetText.set_visible(False)
312+
for ax in axarr.flat:
313+
ax._label_outer_xaxis()
320314
if sharey in ["row", "all"]:
321-
for ax in axarr[:, 1:].flat: # Remove left labels/offsettexts.
322-
ax.yaxis.set_tick_params(which="both", labelleft=False)
323-
if ax.yaxis.offsetText.get_position()[0] == 0:
324-
ax.yaxis.offsetText.set_visible(False)
325-
for ax in axarr[:, :-1].flat: # Remove right labels/offsettexts.
326-
ax.yaxis.set_tick_params(which="both", labelright=False)
327-
if ax.yaxis.offsetText.get_position()[0] == 1:
328-
ax.yaxis.offsetText.set_visible(False)
315+
for ax in axarr.flat:
316+
ax._label_outer_yaxis()
329317

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

0 commit comments

Comments
 (0)