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

Skip to content

Commit 35496a7

Browse files
committed
Fix removal of (Sub)Figure suplabels
In such cases, we need to override the default `_remove_method` to ensure that the (Sub)Figure attributes are also cleared. Fixes matplotlib#31073
1 parent eb7c4ce commit 35496a7

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

lib/matplotlib/figure.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,13 @@ def _suplabels(self, t, info, **kwargs):
376376
else:
377377
suplab = self.text(x, y, t, **kwargs)
378378
setattr(self, info['name'], suplab)
379+
orig_remove_method = suplab._remove_method
380+
381+
# Ensure sublabel is properly removed from Figure internals.
382+
def _remove(label):
383+
orig_remove_method(label)
384+
setattr(self, info['name'], None)
385+
suplab._remove_method = _remove
379386
suplab._autopos = autopos
380387
self.stale = True
381388
return suplab

lib/matplotlib/tests/test_figure.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,18 @@ def test_get_suptitle_supxlabel_supylabel():
357357
assert fig.get_suptitle() == ""
358358
assert fig.get_supxlabel() == ""
359359
assert fig.get_supylabel() == ""
360-
fig.suptitle('suptitle')
360+
text = fig.suptitle('suptitle')
361361
assert fig.get_suptitle() == 'suptitle'
362-
fig.supxlabel('supxlabel')
362+
text.remove()
363+
assert fig.get_suptitle() == ''
364+
text = fig.supxlabel('supxlabel')
363365
assert fig.get_supxlabel() == 'supxlabel'
364-
fig.supylabel('supylabel')
366+
text.remove()
367+
assert fig.get_supxlabel() == ''
368+
text = fig.supylabel('supylabel')
365369
assert fig.get_supylabel() == 'supylabel'
370+
text.remove()
371+
assert fig.get_supylabel() == ''
366372

367373

368374
@image_comparison(['alpha_background'],
@@ -1340,6 +1346,10 @@ def test_subfigure():
13401346
sub[0].suptitle('Left Side')
13411347
sub[0].set_facecolor('white')
13421348

1349+
# Check that removing a suplabel on a subfigure works.
1350+
label = sub[0].supxlabel('Should not exist')
1351+
label.remove()
1352+
13431353
axs = sub[1].subplots(1, 3)
13441354
for ax in axs.flat:
13451355
pc = ax.pcolormesh(np.random.randn(30, 30), vmin=-2, vmax=2)

0 commit comments

Comments
 (0)