8000 Merge pull request #6619 from anntzer/labelouter-hide-labels · matplotlib/matplotlib@4786a0b · GitHub
[go: up one dir, main page]

Skip to content

Commit 4786a0b

Browse files
authored
Merge pull request #6619 from anntzer/labelouter-hide-labels
ENH: Hide "inner" {x,y}labels in label_outer too.
2 parents baf812c + f5c1ab7 commit 4786a0b

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

examples/pylab_examples/subplots_demo.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,32 @@
2222

2323
# Two subplots, the axes array is 1-d
2424
f, axarr = plt.subplots(2, sharex=True)
25+
f.suptitle('Sharing X axis')
2526
axarr[0].plot(x, y)
26-
axarr[0].set_title('Sharing X axis')
2727
axarr[1].scatter(x, y)
2828

2929
# Two subplots, unpack the axes array immediately
3030
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
31+
f.suptitle('Sharing Y axis')
3132
ax1.plot(x, y)
32-
ax1.set_title('Sharing Y axis')
3333
ax2.scatter(x, y)
3434

3535
# Three subplots sharing both x/y axes
36-
f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
37-
ax1.plot(x, y)
38-
ax1.set_title('Sharing both axes')
39-
ax2.scatter(x, y)
40-
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
41-
# Fine-tune figure; make subplots close to each other and hide x ticks for
42-
# all but bottom plot.
36+
f, axarr = plt.subplots(3, sharex=True, sharey=True)
37+
f.suptitle('Sharing both axes')
38+
axarr[0].plot(x, y)
39+
axarr[1].scatter(x, y)
40+
axarr[2].scatter(x, 2 * y ** 2 - 1, color='r')
41+
# Bring subplots close to each other.
4342
f.subplots_adjust(hspace=0)
44-
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
43+
# Hide x labels and tick labels for all but bottom plot.
44+
for ax in axarr:
45+
ax.label_outer()
4546

4647
# row and column sharing
4748
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row')
49+
f.suptitle('Sharing x per column, y per row')
4850
ax1.plot(x, y)
49-
ax1.set_title('Sharing x per column, y per row')
5051
ax2.scatter(x, y)
5152
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
5253
ax4.plot(x, 2 * y ** 2 - 1, color='r')
@@ -61,9 +62,11 @@
6162
axarr[1, 0].set_title('Axis [1,0]')
6263
axarr[1, 1].scatter(x, y ** 2)
6364
axarr[1, 1].set_title('Axis [1,1]')
64-
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
65-
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
66-
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)
65+
for ax in axarr.flat:
66+
ax.set(xlabel='x-label', ylabel='y-label')
67+
# Hide x labels and tick labels for top plots and y ticks for right plots.
68+
for ax in axarr.flat:
69+
ax.label_outer()
6770

6871
# Four polar axes
6972
f, axarr = plt.subplots(2, 2, subplot_kw=dict(projection='polar'))

lib/matplotlib/axes/_subplots.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,25 @@ def is_last_row(self):
125125
def is_last_col(self):
126126
return self.colNum == self.numCols - 1
127127

128-
# COVERAGE NOTE: Never used internally or from examples
128+
# COVERAGE NOTE: Never used internally.
129129
def label_outer(self):
130-
"""
131-
set the visible property on ticklabels so xticklabels are
132-
visible only if the subplot is in the last row and yticklabels
133-
are visible only if the subplot is in the first column
130+
"""Only show "outer" labels and tick labels.
131+
132+
x-labels are only kept for subplots on the last row; y-labels only for
133+
subplots on the first column.
134134
"""
135135
lastrow = self.is_last_row()
136136
firstcol = self.is_first_col()
137137
for label in self.get_xticklabels():
138138
label.set_visible(lastrow)
139139
self.get_xaxis().get_offset_text().set_visible(lastrow)
140+
if not lastrow:
141+
self.set_xlabel("")
140142
for label in self.get_yticklabels():
141143
label.set_visible(firstcol)
142144
self.get_yaxis().get_offset_text().set_visible(firstcol)
145+
if not firstcol:
146+
self.set_ylabel("")
143147

144148
def _make_twin_axes(self, *kl, **kwargs):
145149
"""

0 commit comments

Comments
 (0)
0