Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d45060c

Browse files
authored
Merge pull request #19540 from anntzer/labelo
Share subplots() label visibility handling with label_outer().
2 parents 8b02ed1 + 7e1443d commit d45060c

File tree

3 files changed

+65
-31
lines changed

3 files changed

+65
-31
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.yaxis.get_label_position()
146+
if not ss.is_first_col(): # Remove left label/ticklabels/offsettext.
147+
if label_position == "left":
148+
self.set_ylabel("")
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_ylabel("")
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

lib/matplotlib/tests/test_subplots.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ def check_visible(axs, x_visible, y_visible):
3333
for l in ax.get_yticklabels() + [ax.yaxis.offsetText]:
3434
assert l.get_visible() == vy, \
3535
f"Visibility of y axis #{i} is incorrectly {vy}"
36+
# axis label "visibility" is toggled by label_outer by resetting the
37+
# label to empty, but it can also be empty to start with.
38+
if not vx:
39+
assert ax.get_xlabel() == ""
40+
if not vy:
41+
assert ax.get_ylabel() == ""
3642

3743

3844
def test_shared():
@@ -92,6 +98,7 @@ def test_shared():
9298
f, ((a1, a2), (a3, a4)) = plt.subplots(2, 2, sharex=True, sharey=True)
9399
axs = [a1, a2, a3, a4]
94100
for ax in axs:
101+
ax.set(xlabel="foo", ylabel="bar")
95102
ax.label_outer()
96103
check_visible(axs, [False, False, True, True], [True, False, True, False])
97104

@@ -164,7 +171,7 @@ def test_subplots_offsettext():
164171
@pytest.mark.parametrize("bottom", [True, False])
165172
@pytest.mark.parametrize("left", [True, False])
166173
@pytest.mark.parametrize("right", [True, False])
167-
def test_subplots_hide_labels(top, bottom, left, right):
174+
def test_subplots_hide_ticklabels(top, bottom, left, right):
168175
# Ideally, we would also test offset-text visibility (and remove
169176
# test_subplots_offsettext), but currently, setting rcParams fails to move
170177
# the offset texts as well.
@@ -182,6 +189,23 @@ def test_subplots_hide_labels(top, bottom, left, right):
182189
assert yright == (right and j == 2)
183190

184191

192+
@pytest.mark.parametrize("xlabel_position", ["bottom", "top"])
193+
@pytest.mark.parametrize("ylabel_position", ["left", "right"])
194+
def test_subplots_hide_axislabels(xlabel_position, ylabel_position):
195+
axs = plt.figure().subplots(3, 3, sharex=True, sharey=True)
196+
for (i, j), ax in np.ndenumerate(axs):
197+
ax.set(xlabel="foo", ylabel="bar")
198+
ax.xaxis.set_label_position(xlabel_position)
199+
ax.yaxis.set_label_position(ylabel_position)
200+
ax.label_outer()
201+
assert bool(ax.get_xlabel()) == (
202+
xlabel_position == "bottom" and i == 2
203+
or xlabel_position == "top" and i == 0)
204+
assert bool(ax.get_ylabel()) == (
205+
ylabel_position == "left" and j == 0
206+
or ylabel_position == "right" and j == 2)
207+
208+
185209
def test_get_gridspec():
186210
# ahem, pretty trivial, but...
187211
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)