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

Skip to content

Commit 760a378

Browse files
rcomerQuLogic
andcommitted
Fix remove method for figure title and xy-labels
Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent eb7c4ce commit 760a378

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
@@ -365,6 +365,28 @@ def test_get_suptitle_supxlabel_supylabel():
365365
assert fig.get_supylabel() == 'supylabel'
366366

367367

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

0 commit comments

Comments
 (0)