8000 FIX: fix taking suptitle (and friends) out of get_tightbbox · matplotlib/matplotlib@6924c26 · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6924c26

Browse files
committed
FIX: fix taking suptitle (and friends) out of get_tightbbox
1 parent 12b7c29 commit 6924c26

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,21 +291,24 @@ def _make_margin_suptitles(fig, renderer, *, w_pad=0, h_pad=0):
291291

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

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

304306
if fig._supylabel is not None and fig._supxlabel.get_in_layout():
305307
p = fig._supylabel.get_position()
306-
fig._supylabel.set_position((w_pad_local, p[1]))
307-
bbox = inv_trans_fig(fig._supylabel.get_tightbbox(renderer))
308-
fig._layoutgrid.edit_margin_min('left', bbox.width + 2.0 * w_pad)
308+
if getattr(fig._supylabel, '_autopos', False):
309+
fig._supylabel.set_position((w_pad_local, p[1]))
310+
bbox = inv_trans_fig(fig._supylabel.get_tightbbox(renderer))
311+
fig._layoutgrid.edit_margin_min('left', bbox.width + 2 * w_pad)
309312

310313

311314
def _match_submerged_margins(fig):

lib/matplotlib/figure.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,15 @@ def _suplabels(self, t, info, **kwargs):
369369
Additional kwargs are `matplotlib.text.Text` properties.
370370
"""
371371

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

375-
x = kwargs.pop('x', info['x0'])
376-
y = kwargs.pop('y', info['y0'])
374+
x = kwargs.pop('x', None)
375+
y = kwargs.pop('y', None)
376+
autopos = x is None and y is None
377+
if x is None:
378+
x = info['x0']
379+
if y is None:
380+
y = info['y0']
377381

378382
if 'horizontalalignment' not in kwargs and 'ha' not in kwargs:
379383
kwargs['horizontalalignment'] = info['ha']
@@ -396,8 +400,7 @@ def _suplabels(self, t, info, **kwargs):
396400
sup.remove()
397401
else:
398402
suplab = sup
399-
if manual_position:
400-
suplab.set_in_layout(False)
403+
suplab._autopos = autopos
401404
setattr(self, info['name'], suplab)
402405
self.stale = True
403406
return suplab

lib/matplotlib/tests/test_bbox_tight.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ def y_formatter(y, pos):
6161
plt.xlabel('X axis')
6262

6363

64+
@image_comparison(['bbox_inches_tight_suptile_non_default.png'],
65+
remove_text=False, savefig_kwarg={'bbox_inches': 'tight'},
66+
tol=0.1) # large tolerance because only testing clipping.
67+
def test_bbox_inches_tight_suptitle_non_default():
68+
fig, ax = plt.subplots()
69+
fig.suptitle('Booo', x=0.5, y=1.1)
70+
71+
6472
@image_comparison(['bbox_inches_tight_clipping'],
6573
remove_text=True, savefig_kwarg={'bbox_inches': 'tight'})
6674
def test_bbox_inches_tight_clipping():

0 commit comments

Comments
 (0)
0