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

Skip to content

Commit 313a44d

Browse files
QuLogicmeeseeksmachine
authored andcommitted
Backport PR #31075: Fix remove method for figure title and xy-labels
1 parent 8db8148 commit 313a44d

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

lib/matplotlib/figure.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,17 @@ def _suplabels(self, t, info, **kwargs):
376376
else:
377377
suplab = self.text(x, y, t, **kwargs)
378378
setattr(self, info['name'], suplab)
379+
suplab._remove_method = functools.partial(self._remove_suplabel,
380+
name=info['name'])
381+
379382
suplab._autopos = autopos
380383
self.stale = True
381384
return suplab
382385

386+
def _remove_suplabel(self, label, name):
387+
self.texts.remove(label)
388+
setattr(self, name, None)
389+
383390
@_docstring.Substitution(x0=0.5, y0=0.98, name='super title', ha='center',
384391
va='top', rc='title')
385392
@_docstring.copy(_suplabels)

lib/matplotlib/tests/test_figure.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,28 @@ def test_get_suptitle_supxlabel_supylabel():
364364
assert fig.get_supylabel() == 'supylabel'
365365

366366

367+
def test_remove_suptitle_supxlabel_supylabel():
368+
fig = plt.figure()
369+
370+
title = fig.suptitle('suptitle')
371+
xlabel = fig.supxlabel('supxlabel')
372+
ylabel = fig.supylabel('supylabel')
373+
374+
assert len(fig.texts) == 3
375+
assert fig._suptitle is not None
376+
assert fig._supxlabel is not None
377+
assert fig._supylabel is not None
378+
379+
title.remove()
380+
assert fig._suptitle is None
381+
xlabel.remove()
382+
assert fig._supxlabel is None
383+
ylabel.remove()
384+
assert fig._supylabel is None
385+
386+
assert not fig.texts
387+
388+
367389
@image_comparison(['alpha_background'],
368390
# only test png and svg. The PDF output appears correct,
369391
# but Ghostscript does not preserve the background color.

0 commit comments

Comments
 (0)