diff --git a/lib/matplotlib/_constrained_layout.py b/lib/matplotlib/_constrained_layout.py index 58bb0ccabf95..245679394bc2 100644 --- a/lib/matplotlib/_constrained_layout.py +++ b/lib/matplotlib/_constrained_layout.py @@ -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): diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index af9495af3098..5ecb95bcaa6d 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -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'] @@ -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 diff --git a/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight_suptile_non_default.png b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight_suptile_non_default.png new file mode 100644 index 000000000000..453ea566804d Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_bbox_tight/bbox_inches_tight_suptile_non_default.png differ diff --git a/lib/matplotlib/tests/test_bbox_tight.py b/lib/matplotlib/tests/test_bbox_tight.py index f50b272209ea..c018db5eaa01 100644 --- a/lib/matplotlib/tests/test_bbox_tight.py +++ b/lib/matplotlib/tests/test_bbox_tight.py @@ -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():