Skip to content

Backport PR #19805 on branch v3.4.x (Fix suptitle out of layout) #19827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions lib/matplotlib/_constrained_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,24 @@ def _make_margin_suptitles(fig, renderer, *, w_pad=0, h_pad=0):

if fig._suptitle is not None and fig._suptitle.get_in_layout():
p = fig._suptitle.get_position()
fig._suptitle.set_position((p[0], 1 - h_pad_local))
bbox = inv_trans_fig(fig._suptitle.get_tightbbox(renderer))
fig._layoutgrid.edit_margin_min('top', bbox.height + 2.0 * h_pad)
if getattr(fig._suptitle, '_autopos', False):
fig._suptitle.set_position((p[0], 1 - h_pad_local))
bbox = inv_trans_fig(fig._suptitle.get_tightbbox(renderer))
fig._layoutgrid.edit_margin_min('top', bbox.height + 2 * h_pad)

if fig._supxlabel is not None and fig._supxlabel.get_in_layout():
p = fig._supxlabel.get_position()
fig._supxlabel.set_position((p[0], h_pad_local))
bbox = inv_trans_fig(fig._supxlabel.get_tightbbox(renderer))
fig._layoutgrid.edit_margin_min('bottom', bbox.height + 2.0 * h_pad)
if getattr(fig._supxlabel, '_autopos', False):
fig._supxlabel.set_position((p[0], h_pad_local))
bbox = inv_trans_fig(fig._supxlabel.get_tightbbox(renderer))
fig._layoutgrid.edit_margin_min('bottom', bbox.height + 2 * h_pad)

if fig._supylabel is not None and fig._supxlabel.get_in_layout():
p = fig._supylabel.get_position()
fig._supylabel.set_position((w_pad_local, p[1]))
bbox = inv_trans_fig(fig._supylabel.get_tightbbox(renderer))
fig._layoutgrid.edit_margin_min('left', bbox.width + 2.0 * w_pad)
if getattr(fig._supylabel, '_autopos', False):
fig._supylabel.set_position((w_pad_local, p[1]))
bbox = inv_trans_fig(fig._supylabel.get_tightbbox(renderer))
fig._layoutgrid.edit_margin_min('left', bbox.width + 2 * w_pad)


def _match_submerged_margins(fig):
Expand Down
13 changes: 8 additions & 5 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,15 @@ def _suplabels(self, t, info, **kwargs):
Additional kwargs are `matplotlib.text.Text` properties.
"""

manual_position = ('x' in kwargs or 'y' in kwargs)
suplab = getattr(self, info['name'])

x = kwargs.pop('x', info['x0'])
y = kwargs.pop('y', info['y0'])
x = kwargs.pop('x', None)
y = kwargs.pop('y', None)
autopos = x is None and y is None
if x is None:
x = info['x0']
if y is None:
y = info['y0']

if 'horizontalalignment' not in kwargs and 'ha' not in kwargs:
kwargs['horizontalalignment'] = info['ha']
Expand All @@ -396,8 +400,7 @@ def _suplabels(self, t, info, **kwargs):
sup.remove()
else:
suplab = sup
if manual_position:
suplab.set_in_layout(False)
suplab._autopos = autopos
setattr(self, info['name'], suplab)
self.stale = True
return suplab
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_bbox_tight.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def y_formatter(y, pos):
plt.xlabel('X axis')


@image_comparison(['bbox_inches_tight_suptile_non_default.png'],
remove_text=False, savefig_kwarg={'bbox_inches': 'tight'},
tol=0.1) # large tolerance because only testing clipping.
def test_bbox_inches_tight_suptitle_non_default():
fig, ax = plt.subplots()
fig.suptitle('Booo', x=0.5, y=1.1)


@image_comparison(['bbox_inches_tight_clipping'],
remove_text=True, savefig_kwarg={'bbox_inches': 'tight'})
def test_bbox_inches_tight_clipping():
Expand Down