diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 43e33ded1b32..b301149961f7 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1248,6 +1248,8 @@ def align_xlabels(self, axs=None): if axs is None: axs = self.axes axs = np.ravel(axs) + axs = [ax for ax in axs if hasattr(ax, 'get_subplotspec')] + for ax in axs: _log.debug(' Working on: %s', ax.get_xlabel()) rowspan = ax.get_subplotspec().rowspan @@ -1308,6 +1310,8 @@ def align_ylabels(self, axs=None): if axs is None: axs = self.axes axs = np.ravel(axs) + axs = [ax for ax in axs if hasattr(ax, 'get_subplotspec')] + for ax in axs: _log.debug(' Working on: %s', ax.get_ylabel()) colspan = ax.get_subplotspec().colspan diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index cb8f63893aea..5ab1f35a1a2c 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -63,6 +63,41 @@ def test_align_labels(): fig.align_labels() +def test_align_labels_stray_axes(): + fig, axs = plt.subplots(2, 2) + for nn, ax in enumerate(axs.flat): + ax.set_xlabel('Boo') + ax.set_xlabel('Who') + ax.plot(np.arange(4)**nn, np.arange(4)**nn) + fig.align_ylabels() + fig.align_xlabels() + fig.draw_without_rendering() + xn = np.zeros(4) + yn = np.zeros(4) + for nn, ax in enumerate(axs.flat): + yn[nn] = ax.xaxis.label.get_position()[1] + xn[nn] = ax.yaxis.label.get_position()[0] + np.testing.assert_allclose(xn[:2], xn[2:]) + np.testing.assert_allclose(yn[::2], yn[1::2]) + + fig, axs = plt.subplots(2, 2, constrained_layout=True) + for nn, ax in enumerate(axs.flat): + ax.set_xlabel('Boo') + ax.set_xlabel('Who') + pc = ax.pcolormesh(np.random.randn(10, 10)) + fig.colorbar(pc, ax=ax) + fig.align_ylabels() + fig.align_xlabels() + fig.draw_without_rendering() + xn = np.zeros(4) + yn = np.zeros(4) + for nn, ax in enumerate(axs.flat): + yn[nn] = ax.xaxis.label.get_position()[1] + xn[nn] = ax.yaxis.label.get_position()[0] + np.testing.assert_allclose(xn[:2], xn[2:]) + np.testing.assert_allclose(yn[::2], yn[1::2]) + + def test_figure_label(): # pyplot figure creation, selection, and closing with label/number/instance plt.close('all')