Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67eca83

Browse files
authored
Merge pull request #19805 from jklymak/fix-suptitle-out-of-layout
Fix suptitle out of layout
2 parents 7b3ac93 + 6924c26 commit 67eca83

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
@@ -367,11 +367,15 @@ def _suplabels(self, t, info, **kwargs):
367367
Additional kwargs are `matplotlib.text.Text` properties.
368368
"""
369369

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

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

376380
if 'horizontalalignment' not in kwargs and 'ha' not in kwargs:
377381
kwargs['horizontalalignment'] = info['ha']
@@ -394,8 +398,7 @@ def _suplabels(self, t, info, **kwargs):
394398
sup.remove()
395399
else:
396400
suplab = sup
397-
if manual_position:
398-
suplab.set_in_layout(False)
401+
suplab._autopos = autopos
399402
setattr(self, info['name'], suplab)
400403
self.stale = True
401404
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)