Skip to content

Commit

Permalink
Merge pull request matplotlib#28734 from rcomer/compressed-suptitle
Browse files Browse the repository at this point in the history
  • Loading branch information
timhoffm authored Aug 21, 2024
2 parents 3891c15 + 146ba53 commit 85d7295
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/behavior/28734-REC.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
``suptitle`` in compressed layout
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Compressed layout now automatically positions the `~.Figure.suptitle` just
above the top row of axes. To keep this title in its previous position,
either pass ``in_layout=False`` or explicitly set ``y=0.98`` in the
`~.Figure.suptitle` call.
7 changes: 7 additions & 0 deletions lib/matplotlib/_constrained_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ def do_constrained_layout(fig, h_pad, w_pad,
w_pad=w_pad, hspace=hspace, wspace=wspace)
else:
_api.warn_external(warn_collapsed)

if ((suptitle := fig._suptitle) is not None and
suptitle.get_in_layout() and suptitle._autopos):
x, _ = suptitle.get_position()
suptitle.set_position(
(x, layoutgrids[fig].get_inner_bbox().y1 + h_pad))
suptitle.set_verticalalignment('bottom')
else:
_api.warn_external(warn_collapsed)
reset_margins(layoutgrids, fig)
Expand Down
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,30 @@ def test_compressed1():
np.testing.assert_allclose(pos.y0, 0.1934, atol=1e-3)


def test_compressed_suptitle():
fig, (ax0, ax1) = plt.subplots(
nrows=2, figsize=(4, 10), layout="compressed",
gridspec_kw={"height_ratios": (1 / 4, 3 / 4), "hspace": 0})

ax0.axis("equal")
ax0.set_box_aspect(1/3)

ax1.axis("equal")
ax1.set_box_aspect(1)

title = fig.suptitle("Title")
fig.draw_without_rendering()
assert title.get_position()[1] == pytest.approx(0.7457, abs=1e-3)

title = fig.suptitle("Title", y=0.98)
fig.draw_without_rendering()
assert title.get_position()[1] == 0.98

title = fig.suptitle("Title", in_layout=False)
fig.draw_without_rendering()
assert title.get_position()[1] == 0.98


@pytest.mark.parametrize('arg, state', [
(True, True),
(False, False),
Expand Down

0 comments on commit 85d7295

Please sign in to comment.