8000 Merge pull request #19472 from anntzer/sharedlabels · matplotlib/matplotlib@b681abc · GitHub
[go: up one dir, main page]

Skip to content

Commit b681abc

Browse files
authored
Merge pull request #19472 from anntzer/sharedlabels
Fix default label visibility for top-or-left-labeled shared subplots().
2 parents aa413a8 + 6a9e339 commit b681abc

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Shared-axes ``subplots`` tick label visibility is now correct for top or left labels
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
When calling ``subplots(..., sharex=True, sharey=True)``, Matplotlib
4+
automatically hides x tick labels for axes not in the first column and y tick
5+
labels for axes not in the last row. This behavior is incorrect if rcParams
6+
specify that axes should be labeled on the top (``rcParams["xtick.labeltop"] =
7+
True``) or on the right (``rcParams["ytick.labelright"] = True``).
8+
9+
Such cases are now handled correctly (adjusting visibility as needed on the
10+
first row and last column of axes).

lib/matplotlib/gridspec.py

Lines changed: 16 additions & 10 deletions
< 8000 /tr>
Original file line numberDiff line numberDiff line change
@@ -309,17 +309,23 @@ def subplots(self, *, sharex=False, sharey=False, squeeze=True,
309309

310310
# turn off redundant tick labeling
311311
if sharex in ["col", "all"]:
312-
# turn off all but the bottom row
313-
for ax in axarr[:-1, :].flat:
314-
ax.xaxis.set_tick_params(which='both',
315-
labelbottom=False, labeltop=False)
316-
ax.xaxis.offsetText.set_visible(False)
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)
317320
if sharey in ["row", "all"]:
318-
# turn off all but the first column
319-
for ax in axarr[:, 1:].flat:
320-
ax.yaxis.set_tick_params(which='both',
321-
labelleft=False, labelright=False)
322-
ax.yaxis.offsetText.set_visible(False)
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)
323329

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

lib/matplotlib/tests/test_subplots.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,28 @@ def test_subplots_offsettext():
160160
axs[1, 1].plot(y, x)
161161

162162

163+
@pytest.mark.parametrize("top", [True, False])
164+
@pytest.mark.parametrize("bottom", [True, False])
165+
@pytest.mark.parametrize("left", [True, False])
166+
@pytest.mark.parametrize("right", [True, False])
167+
def test_subplots_hide_labels(top, bottom, left, right):
168+
# Ide 8000 ally, we would also test offset-text visibility (and remove
169+
# test_subplots_offsettext), but currently, setting rcParams fails to move
170+
# the offset texts as well.
171+
with plt.rc_context({"xtick.labeltop": top, "xtick.labelbottom": bottom,
172+
"ytick.labelleft": left, "ytick.labelright": right}):
173+
axs = plt.figure().subplots(3, 3, sharex=True, sharey=True)
174+
for (i, j), ax in np.ndenumerate(axs):
175+
xtop = ax.xaxis._major_tick_kw["label2On"]
176+
xbottom = ax.xaxis._major_tick_kw["label1On"]
177+
yleft = ax.yaxis._major_tick_kw["label1On"]
178+
yright = ax.yaxis._major_tick_kw["label2On"]
179+
assert xtop == (top and i == 0)
180+
assert xbottom == (bottom and i == 2)
181+
assert yleft == (left and j == 0)
182+
assert yright == (right and j == 2)
183+
184+
163185
def test_get_gridspec():
164186
# ahem, pretty trivial, but...
165187
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)
0