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 '