From 27d669c5a7d0b111ee826a397a4fb14a3a822294 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 20 Jun 2016 18:51:21 -0700 Subject: [PATCH 1/2] Hide "inner" {x,y}labels in label_outer too. `ax.label_outer` used to hide only "inner" ticklabels; make it hide axes labels too. (I couldn't find a way to just switch label visibility, so I just set the labels to the empty string instead.) --- lib/matplotlib/axes/_subplots.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_subplots.py b/lib/matplotlib/axes/_subplots.py index 6d92a754557..46635b1aa85 100644 --- a/lib/matplotlib/axes/_subplots.py +++ b/lib/matplotlib/axes/_subplots.py @@ -127,19 +127,23 @@ def is_last_col(self): # COVERAGE NOTE: Never used internally or from examples def label_outer(self): - """ - set the visible property on ticklabels so xticklabels are - visible only if the subplot is in the last row and yticklabels - are visible only if the subplot is in the first column + """Only show "outer" labels and tick labels. + + x-labels are only kept for subplots on the last row; y-labels only for + subplots on the first column. """ lastrow = self.is_last_row() firstcol = self.is_first_col() for label in self.get_xticklabels(): label.set_visible(lastrow) self.get_xaxis().get_offset_text().set_visible(lastrow) + if not lastrow: + self.set_xlabel("") for label in self.get_yticklabels(): label.set_visible(firstcol) self.get_yaxis().get_offset_text().set_visible(firstcol) + if not firstcol: + self.set_ylabel("") def _make_twin_axes(self, *kl, **kwargs): """ From f5c1ab7ff86cfd26797dfa8ec25c5778acf954c1 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 21 Jun 2016 12:07:12 -0700 Subject: [PATCH 2/2] Add example for label_outer. --- examples/pylab_examples/subplots_demo.py | 31 +++++++++++++++++-------------- lib/matplotlib/axes/_subplots.py | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/examples/pylab_examples/subplots_demo.py b/examples/pylab_examples/subplots_demo.py index 8d54f053595..6e9c9be4a07 100644 --- a/examples/pylab_examples/subplots_demo.py +++ b/examples/pylab_examples/subplots_demo.py @@ -22,31 +22,32 @@ # Two subplots, the axes array is 1-d f, axarr = plt.subplots(2, sharex=True) +f.suptitle('Sharing X axis') axarr[0].plot(x, y) -axarr[0].set_title('Sharing X axis') axarr[1].scatter(x, y) # Two subplots, unpack the axes array immediately f, (ax1, ax2) = plt.subplots(1, 2, sharey=True) +f.suptitle('Sharing Y axis') ax1.plot(x, y) -ax1.set_title('Sharing Y axis') ax2.scatter(x, y) # Three subplots sharing both x/y axes -f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True) -ax1.plot(x, y) -ax1.set_title('Sharing both axes') -ax2.scatter(x, y) -ax3.scatter(x, 2 * y ** 2 - 1, color='r') -# Fine-tune figure; make subplots close to each other and hide x ticks for -# all but bottom plot. +f, axarr = plt.subplots(3, sharex=True, sharey=True) +f.suptitle('Sharing both axes') +axarr[0].plot(x, y) +axarr[1].scatter(x, y) +axarr[2].scatter(x, 2 * y ** 2 - 1, color='r') +# Bring subplots close to each other. f.subplots_adjust(hspace=0) -plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False) +# Hide x labels and tick labels for all but bottom plot. +for ax in axarr: + ax.label_outer() # row and column sharing f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row') +f.suptitle('Sharing x per column, y per row') ax1.plot(x, y) -ax1.set_title('Sharing x per column, y per row') ax2.scatter(x, y) ax3.scatter(x, 2 * y ** 2 - 1, color='r') ax4.plot(x, 2 * y ** 2 - 1, color='r') @@ -61,9 +62,11 @@ axarr[1, 0].set_title('Axis [1,0]') axarr[1, 1].scatter(x, y ** 2) axarr[1, 1].set_title('Axis [1,1]') -# Fine-tune figure; hide x ticks for top plots and y ticks for right plots -plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False) -plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False) +for ax in axarr.flat: + ax.set(xlabel='x-label', ylabel='y-label') +# Hide x labels and tick labels for top plots and y ticks for right plots. +for ax in axarr.flat: + ax.label_outer() # Four polar axes f, axarr = plt.subplots(2, 2, subplot_kw=dict(projection='polar')) diff --git a/lib/matplotlib/axes/_subplots.py b/lib/matplotlib/axes/_subplots.py index 46635b1aa85..47f3c15749d 100644 --- a/lib/matplotlib/axes/_subplots.py +++ b/lib/matplotlib/axes/_subplots.py @@ -125,7 +125,7 @@ def is_last_row(self): def is_last_col(self): return self.colNum == self.numCols - 1 - # COVERAGE NOTE: Never used internally or from examples + # COVERAGE NOTE: Never used internally. def label_outer(self): """Only show "outer" labels and tick labels.