Skip to content

Commit 97667eb

Browse files
committed
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.
1 parent dc63fdf commit 97667eb

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tight_layout now supports suptitle
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lib/matplotlib/tests/test_tightlayout.py

+8
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,11 @@ def test_collapsed():
308308
# test that passing a rect doesn't crash...
309309
with pytest.warns(UserWarning):
310310
plt.tight_layout(rect=[0, 0, 0.8, 0.8])
311+
312+
313+
def test_suptitle():
314+
fig, ax = plt.subplots(tight_layout=True)
315+
st = fig.suptitle("foo")
316+
t = ax.set_title("bar")
317+
fig.canvas.draw()
318+
assert st.get_window_extent().y0 > t.get_window_extent().y1

lib/matplotlib/tight_layout.py

+6
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,15 @@ def auto_adjust_subplotpars(
107107
if not margin_top:
108108
margin_top = (max(vspaces[0, :].max(), 0)
109109
+ pad_inches / fig_height_inch)
110+
suptitle = fig._suptitle
111+
if suptitle and suptitle.get_in_layout():
112+
rel_suptitle_height = fig.transFigure.inverted().transform_bbox(
113+
suptitle.get_window_extent(renderer)).height
114+
margin_top += rel_suptitle_height + pad_inches / fig_height_inch
110115
if not margin_bottom:
111116
margin_bottom = (max(vspaces[-1, :].max(), 0)
112117
+ pad_inches / fig_height_inch)
118+
113119
if margin_left + margin_right >= 1:
114120
cbook._warn_external('Tight layout not applied. The left and right '
115121
'margins cannot be made large enough to '

0 commit comments

Comments
 (0)