From 0f26a46708a96ae851076f889b42c240e25c1aa3 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 22 Apr 2020 22:31:31 +0200 Subject: [PATCH] Add support for suptitle() in tight_layout(). The padding between the suptitle and the axes is the same as around all axes, which is consistent with the behavior of constrained_layout. --- doc/users/next_whats_new/2020-04-22-suptitle-tl.rst | 2 ++ lib/matplotlib/tests/test_tightlayout.py | 8 ++++++++ lib/matplotlib/tight_layout.py | 6 ++++++ 3 files changed, 16 insertions(+) create mode 100644 doc/users/next_whats_new/2020-04-22-suptitle-tl.rst diff --git a/doc/users/next_whats_new/2020-04-22-suptitle-tl.rst b/doc/users/next_whats_new/2020-04-22-suptitle-tl.rst new file mode 100644 index 000000000000..aee6a2c312cb --- /dev/null +++ b/doc/users/next_whats_new/2020-04-22-suptitle-tl.rst @@ -0,0 +1,2 @@ +tight_layout now supports suptitle +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/lib/matplotlib/tests/test_tightlayout.py b/lib/matplotlib/tests/test_tightlayout.py index 20ec0bbaa1b0..6934fe75c53e 100644 --- a/lib/matplotlib/tests/test_tightlayout.py +++ b/lib/matplotlib/tests/test_tightlayout.py @@ -308,3 +308,11 @@ def test_collapsed(): # test that passing a rect doesn't crash... with pytest.warns(UserWarning): plt.tight_layout(rect=[0, 0, 0.8, 0.8]) + + +def test_suptitle(): + fig, ax = plt.subplots(tight_layout=True) + st = fig.suptitle("foo") + t = ax.set_title("bar") + fig.canvas.draw() + assert st.get_window_extent().y0 > t.get_window_extent().y1 diff --git a/lib/matplotlib/tight_layout.py b/lib/matplotlib/tight_layout.py index c02e41710b06..65d8c372f7c9 100644 --- a/lib/matplotlib/tight_layout.py +++ b/lib/matplotlib/tight_layout.py @@ -107,9 +107,15 @@ def auto_adjust_subplotpars( if not margin_top: margin_top = (max(vspaces[0, :].max(), 0) + pad_inches / fig_height_inch) + suptitle = fig._suptitle + if suptitle and suptitle.get_in_layout(): + rel_suptitle_height = fig.transFigure.inverted().transform_bbox( + suptitle.get_window_extent(renderer)).height + margin_top += rel_suptitle_height + pad_inches / fig_height_inch if not margin_bottom: margin_bottom = (max(vspaces[-1, :].max(), 0) + pad_inches / fig_height_inch) + if margin_left + margin_right >= 1: cbook._warn_external('Tight layout not applied. The left and right ' 'margins cannot be made large enough to '