Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,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
Expand Down Expand Up @@ -1307,6 +1309,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
Expand Down
35 changes: 35 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down