Closed
Description
Bug summary
Hi,
I would like to use fig.clear()
on a figure that contains subfigures. This currently (as of Python 3.10.1, matplolib 3.6.0.dev1416+ga4f712079a.d20220107
) throws a ValueError.
Code for reproduction
import matplotlib.pyplot as plt
mainfig = plt.figure()
gs = mainfig.add_gridspec(ncols=1,nrows=1)
subfig = mainfig.add_subfigure(gs[0])
ax = subfig.subplots(1)
im = ax.imshow([[1, 2], [3, 4]])
mainfig.clear()
Actual outcome
Traceback (most recent call last):
File "/home/jay/ExtraDrive1/External/experiments/matplotlib/mwe/test_clear_figure_of_subfigures.py", line 7, in <module>
mainfig.clear()
File "/home/jay/ExtraDrive1/External/experiments/matplotlib/lib/matplotlib/figure.py", line 2782, in clear
self.clf(keep_observers=keep_observers)
File "/home/jay/ExtraDrive1/External/experiments/matplotlib/lib/matplotlib/figure.py", line 2760, in clf
self.delaxes(ax) # Remove ax from self._axstack.
File "/home/jay/ExtraDrive1/External/experiments/matplotlib/lib/matplotlib/figure.py", line 930, in delaxes
self._localaxes.remove(ax)
ValueError: list.remove(x): x not in list
Expected outcome
The program should run and mainfig.axes
as well as mainfig.subfigs
should be empty.
Additional information
I have fixed this in a local branch and am submitting a PR
Operating system
Pop! OS 21.04
Matplotlib Version
3.6.0.dev1416+ga4f712079a.d20220107
Matplotlib Backend
QtAgg and module://matplotlib_inline.backend_inline
Python version
Python 3.10.1
Jupyter version
3.2.5
Installation
git checkout