Skip to content

plt.close() not firing close_events on non-interactive backends #18609

@drammock

Description

@drammock

Bug report

Bug summary

When using a non-interactive backend, calling plt.close() does not trigger close events. This is a problem for testing / continuous integration; code that works interactively to manage multiple inter-related figures does not work on the CI server.

Code for reproduction

import matplotlib
matplotlib.use('Agg')  # also occurs on SVG, PDF...
import matplotlib.pyplot as plt


def close_callback(event):
    print('close callback was called')


fig = plt.figure()
cid = fig.canvas.mpl_connect('close_event', close_callback)
plt.close(fig)

Actual outcome

No console output with backends agg, svg, and pdf at least (didn't try every single backend).

Expected outcome

With e.g., Qt5Agg backend there is (correctly) a line of console output saying close callback was called.

Matplotlib version

  • Operating system: Linux
  • Matplotlib version: 3.3.1 (via conda-forge). Also happens as far back as version 2.1.2
  • Matplotlib backend: Agg
  • Python version: 3.8.5
  • Jupyter version (if applicable): n/a
  • Other libraries: n/a

A less-minimal example that more closely approximates my use case is here: https://stackoverflow.com/q/64105454/1664024

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions