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

Skip to content

test for align_ylabel bug with constrained_layout #19373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 31, 2021
Merged
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
34 changes: 34 additions & 0 deletions lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,37 @@ def test_bboxtight():
def test_bbox():
fig, ax = plt.subplots(constrained_layout=True)
ax.set_aspect(1.)


def test_align_labels():
"""
Tests for a bug in which constrained layout and align_ylabels on
three unevenly sized subplots, one of whose y tick labels include
negative numbers, drives the non-negative subplots' y labels off
the edge of the plot
"""
fig, (ax3, ax1, ax2) = plt.subplots(3, 1, constrained_layout=True,
figsize=(6.4, 8),
gridspec_kw={"height_ratios": (1, 1,
0.7)})

ax1.set_ylim(0, 1)
ax1.set_ylabel("Label")

ax2.set_ylim(-1.5, 1.5)
ax2.set_ylabel("Label")

ax3.set_ylim(0, 1)
ax3.set_ylabel("Label")

fig.align_ylabels(axs=(ax3, ax1, ax2))

fig.canvas.draw()
after_align = [ax1.yaxis.label.get_window_extent(),
ax2.yaxis.label.get_window_extent(),
ax3.yaxis.label.get_window_extent()]
# ensure labels are approximately aligned
np.testing.assert_allclose([after_align[0].x0, after_align[2].x0],
after_align[1].x0, rtol=0, atol=1e-05)
# ensure labels do not go off the edge
assert after_align[0].x0 >= 1