diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 85b378fcd422..f195578ac0a6 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -376,10 +376,17 @@ def _suplabels(self, t, info, **kwargs): else: suplab = self.text(x, y, t, **kwargs) setattr(self, info['name'], suplab) + suplab._remove_method = functools.partial(self._remove_suplabel, + name=info['name']) + suplab._autopos = autopos self.stale = True return suplab + def _remove_suplabel(self, label, name): + self.texts.remove(label) + setattr(self, name, None) + @_docstring.Substitution(x0=0.5, y0=0.98, name='super title', ha='center', va='top', rc='title') @_docstring.copy(_suplabels) diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 95568d237b91..93aff4b1a632 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -365,6 +365,28 @@ def test_get_suptitle_supxlabel_supylabel(): assert fig.get_supylabel() == 'supylabel' +def test_remove_suptitle_supxlabel_supylabel(): + fig = plt.figure() + + title = fig.suptitle('suptitle') + xlabel = fig.supxlabel('supxlabel') + ylabel = fig.supylabel('supylabel') + + assert len(fig.texts) == 3 + assert fig._suptitle is not None + assert fig._supxlabel is not None + assert fig._supylabel is not None + + title.remove() + assert fig._suptitle is None + xlabel.remove() + assert fig._supxlabel is None + ylabel.remove() + assert fig._supylabel is None + + assert not fig.texts + + @image_comparison(['alpha_background'], # only test png and svg. The PDF output appears correct, # but Ghostscript does not preserve the background color.