diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index ad7dbcd3bf57..67474628e7bf 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -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