Skip to content

Figure cannot be closed if it has associated Agg canvas #18163

Closed

Description

@malfet

Bug report

** plt.fignum_exists() returns True after figure is closed**

Code snippet below closes figure after creating FigureCanvasAgg, and it works with matplotlib-3.2.1 but fails with matplotlib-3.3.0

Code for reproduction

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

figure = plt.figure()
plt.axis('scaled')
plt.tight_layout()
matplotlib.backends.backend_agg.FigureCanvasAgg(figure)
plt.close(figure)
print("Matplotlib version:", matplotlib.__version__)
assert plt.fignum_exists(figure.number) == False

Actual outcome

Matplotlib version: 3.3.0
Traceback (most recent call last):
  File "figure_exists.py", line 11, in <module>
    assert plt.fignum_exists(figure.number) == False
AssertionError

Expected outcome
No assertion, which is easy to reproduce if one to downgrade to matplotlib-3.2.1:

 pip3 install matplotlib==3.2.1 --user; python figure_exists.py 
Collecting matplotlib==3.2.1
  Using cached https://files.pythonhosted.org/packages/b2/c2/71fcf957710f3ba1f09088b35776a799ba7dd95f7c2b195ec800933b276b/matplotlib-3.2.1-cp37-cp37m-manylinux1_x86_64.whl
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/lib/python3.7/site-packages (from matplotlib==3.2.1) (2.4.0)
Requirement already satisfied: numpy>=1.11 in /usr/local/lib64/python3.7/site-packages (from matplotlib==3.2.1) (1.18.4)
Requirement already satisfied: python-dateutil>=2.1 in /usr/lib/python3.7/site-packages (from matplotlib==3.2.1) (2.8.0)
Requirement already satisfied: cycler>=0.10 in /usr/lib/python3.7/site-packages (from matplotlib==3.2.1) (0.10.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/lib64/python3.7/site-packages (from matplotlib==3.2.1) (1.1.0)
Requirement already satisfied: six>=1.5 in /usr/lib/python3.7/site-packages (from python-dateutil>=2.1->matplotlib==3.2.1) (1.12.0)
Requirement already satisfied: setuptools in /usr/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib==3.2.1) (40.8.0)
Installing collected packages: matplotlib
  Found existing installation: matplotlib 3.3.0
    Uninstalling matplotlib-3.3.0:
      Successfully uninstalled matplotlib-3.3.0
Successfully installed matplotlib-3.2.1
Matplotlib version: 3.2.1

Matplotlib version

  • Operating system: Fedora Core 30
  • Matplotlib version: 3.3.0 (installed via pip as pip3 install matplotlib==3.3.0 --user )
  • Matplotlib backend (print(matplotlib.get_backend())):
  • Python version: 3.7.0
  • Jupyter version (if applicable):
  • Other libraries:

Activity

  1. added this to the v3.3.1 milestone on Aug 4, 2020
  2. dopplershift commented on Aug 4, 2020

    @dopplershift
    Contributor

    Well, I can reproduce. The problem is the line:

    matplotlib.backends.backend_agg.FigureCanvasAgg(figure)

    This resets the canvas of figure; this also leaves things in a state where figure.canvas is None, which then throws off the machinery for closing the figure because the currently set manager doesn't match the manager under the key 1, so it doesn't do anything. What I'm unclear on is what, if anything should be happening here.

  3. QuLogic commented on Aug 4, 2020

    @QuLogic
    Member

    So the question is, why are you doing this:

    matplotlib.backends.backend_agg.FigureCanvasAgg(figure)

    and what do you expect it to do?

  4. tacaswell commented on Aug 5, 2020

    @tacaswell
    Member

    This goes back to 7fcf123 where we changed from searching the managers we know about for the figure to asking the figure who its manager is. At the time @QuLogic raised slight concerns (https://github.com/matplotlib/matplotlib/pull/13581/files#r261907128).

    I think what is happening is that when we set the new canvas, it does not have a manager, but we still have a manager in the global state that is disconnected from its original manager.

  5. added 2 commits that reference this issue on Aug 5, 2020
    f65eb17
    2ba9f29
  6. changed the title [-]Figure can not be closed if it has associated Agg canvas[/-] [+]Figure cannot be closed if it has associated Agg canvas[/+] on Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions