Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Bug]: Cannot clear figure of subfigures #22137

Copy link
Copy link
Closed
@stanleyjs

Description

@stanleyjs
Issue body actions

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.