Skip to content

Commit

Permalink
Add support for suptitle() in tight_layout().
Browse files Browse the repository at this point in the history
The padding between the suptitle and the axes is the same as around all
axes, which is consistent with the behavior of constrained_layout.
  • Loading branch information
anntzer committed Apr 23, 2020
1 parent cc5ddce commit 0f26a46
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/users/next_whats_new/2020-04-22-suptitle-tl.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tight_layout now supports suptitle
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_tightlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions lib/matplotlib/tight_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand Down

0 comments on commit 0f26a46

Please sign in to comment.