8000 Merge pull request #21764 from meeseeksmachine/auto-backport-of-pr-21… · matplotlib/matplotlib@647c000 · GitHub
[go: up one dir, main page]

Skip to content

Commit 647c000

Browse files
authored
Merge pull request #21764 from meeseeksmachine/auto-backport-of-pr-21762-on-v3.5.x
Backport PR #21762 on branch v3.5.x (FIX: align_x/ylabels)
2 parents 0f962e4 + d7538e7 commit 647c000

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/matplotlib/figure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,8 @@ def align_xlabels(self, axs=None):
12471247
if axs is None:
12481248
axs = self.axes
12491249
axs = np.ravel(axs)
1250+
axs = [ax for ax in axs if hasattr(ax, 'get_subplotspec')]
1251+
12501252
for ax in axs:
12511253
_log.debug(' Working on: %s', ax.get_xlabel())
12521254
rowspan = ax.get_subplotspec().rowspan
@@ -1307,6 +1309,8 @@ def align_ylabels(self, axs=None):
13071309
if axs is None:
13081310
axs = self.axes
13091311
axs = np.ravel(axs)
1312+
axs = [ax for ax in axs if hasattr(ax, 'get_subplotspec')]
1313+
13101314
for ax in axs:
13111315
_log.debug(' Working on: %s', ax.get_ylabel())
13121316
colspan = ax.get_subplotspec().colspan

lib/matplotlib/tests/test_figure.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,41 @@ def test_align_labels():
6363
fig.align_labels()
6464

6565

66+
def test_align_labels_stray_axes():
67+
fig, axs = plt.subplots(2, 2)
68+
for nn, ax in enumerate(axs.flat):
69+
ax.set_xlabel('Boo')
70+
ax.set_xlabel('Who')
71+
ax.plot(np.arange(4)**nn, np.arange(4)**nn)
72+
fig.align_ylabels()
73+
fig.align_xlabels()
74+
fig.draw_without_rendering()
75+
xn = np.zeros(4)
76+
yn = np.zeros(4)
77+
for nn, ax in enumerate(axs.flat):
78+
yn[nn] = ax.xaxis.label.get_position()[1]
79+
xn[nn] = ax.yaxis.label.get_position()[0]
80+
np.testing.assert_allclose(xn[:2], xn[2:])
81+
np.testing.assert_allclose(yn[::2], yn[1::2])
82+
83+
fig, axs = plt.subplots(2, 2, constrained_layout=True)
84+
for nn, ax in enumerate(axs.flat):
85+
ax.set_xlabel('Boo')
86+
ax.set_xlabel('Who')
87+
pc = ax.pcolormesh(np.random.randn(10, 10))
88+
fig.colorbar(pc, ax=ax)
89+
fig.align_ylabels()
90+
fig.align_xlabels()
91+
fig.draw_without_rendering()
92+
xn = np.zeros(4)
93+
yn = np.zeros(4)
94+
for nn, ax in enumerate(axs.flat):
95+
yn[nn] = ax.xaxis.label.get_position()[1]
96+
xn[nn] = ax.yaxis.label.get_position()[0]
97+
np.testing.assert_allclose(xn[:2], xn[2:])
98+
np.testing.assert_allclose(yn[::2], yn[1::2])
99+
100+
66101
def test_figure_label():
67102
# pyplot figure creation, selection, and closing with label/number/instance
68103
plt.close('all')

0 commit comments

Comments
 (0)
0