From 3235ed8b875a55d869629e6d6c7aa34b2b35e968 Mon Sep 17 00:00:00 2001 From: Nora Moseman Date: Tue, 26 Jan 2021 17:07:14 -0700 Subject: [PATCH 1/3] test for align_ylabel constrained_layout bug: first implementation --- .../tests/test_constrainedlayout.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index ad7dbcd3bf57..d042040f3b88 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -521,3 +521,36 @@ 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 + + """ + + data = [0,1] + + fig, (ax3, ax1, ax2) = plt.subplots(3, 1, constrained_layout=True, + figsize=(6.4,8), + gridspec_kw={"height_ratios":(1, 1, 0.7)}) + + ax1.plot(data, data) + ax1.set_ylabel("Label") + + ax2.plot(data, data) + ax2.set_ylim(-1.5, 1.5) + ax2.set_ylabel("Label") + + ax3.plot(data, data) + 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()) + + assert round(after_align[0].x0, 5) == round(after_align[1].x0, 5) == round(after_align[2].x0, 5) > 1 From 40f78483f7266749eb6007b1c7cf1b55db9ec037 Mon Sep 17 00:00:00 2001 From: Nora Moseman Date: Tue, 26 Jan 2021 17:38:11 -0700 Subject: [PATCH 2/3] fixed formatting --- .../tests/test_constrainedlayout.py | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index d042040f3b88..2ffe538dd334 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -522,19 +522,22 @@ 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 + 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 """ - data = [0,1] + data = [0, 1] fig, (ax3, ax1, ax2) = plt.subplots(3, 1, constrained_layout=True, - figsize=(6.4,8), - gridspec_kw={"height_ratios":(1, 1, 0.7)}) + figsize=(6.4, 8), + gridspec_kw={"height_ratios": (1, 1, + 0.7)}) ax1.plot(data, data) ax1.set_ylabel("Label") @@ -552,5 +555,8 @@ def test_align_labels(): after_align = (ax1.yaxis.label.get_window_extent(), ax2.yaxis.label.get_window_extent(), ax3.yaxis.label.get_window_extent()) - - assert round(after_align[0].x0, 5) == round(after_align[1].x0, 5) == round(after_align[2].x0, 5) > 1 + #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 From 2ab0e05399d1d156283166f97e84304adff5113b Mon Sep 17 00:00:00 2001 From: Nora Moseman Date: Thu, 28 Jan 2021 10:50:14 -0700 Subject: [PATCH 3/3] do not call plot(), set explicit positive y_lim for subplots, fix formatting --- lib/matplotlib/tests/test_constrainedlayout.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/tests/test_constrainedlayout.py b/lib/matplotlib/tests/test_constrainedlayout.py index 2ffe538dd334..67474628e7bf 100644 --- a/lib/matplotlib/tests/test_constrainedlayout.py +++ b/lib/matplotlib/tests/test_constrainedlayout.py @@ -529,34 +529,29 @@ def test_align_labels(): 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 - """ - - data = [0, 1] - fig, (ax3, ax1, ax2) = plt.subplots(3, 1, constrained_layout=True, figsize=(6.4, 8), gridspec_kw={"height_ratios": (1, 1, 0.7)}) - ax1.plot(data, data) + ax1.set_ylim(0, 1) ax1.set_ylabel("Label") - ax2.plot(data, data) ax2.set_ylim(-1.5, 1.5) ax2.set_ylabel("Label") - ax3.plot(data, data) + 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(), + 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 + 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 + # ensure labels do not go off the edge assert after_align[0].x0 >= 1