matplotlib / matplotlib/ipympl

Closing a figure from the notebook does not close the python figure

Open
#4 15 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
1.7k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

This is essentially a regression of https://github.com/matplotlib/matplotlib/issues/4841 which happened when the nbagg backend was converted to a widget.

To reproduce create a cell with a simple plot.

```
plt.plot(range(10))
plt.show()
```

re execute the cell and observe that the figure count goes up and memory consumption goes up too. If you continue doing this you will eventually have more than 20 open figures and matplotlib will print a warning even if you only have one figure displayed.

The same is true if you explicitly close the widget using the widget close cross on the left of the figure.

I have spent some time trying to figure out how to best fix this but don't really know how to best do this. [This function](https://github.com/matplotlib/jupyter-matplotlib/blob/master/js/src/mpl_widget.js#L122)
is meant to trigger a close message to the python side when the figure is removed from the DOM but no longer works.

The old no longer existing [close button](https://github.com/matplotlib/jupyter-matplotlib/blob/master/js/src/mpl_widget.js#L115) is also intended to send a close signal to the python layer but this is not hooked up to the widget close button.

https://github.com/matplotlib/matplotlib/issues/6414 has some more related issues.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with js/src/mpl_widget.js, especially the DOM-removal function and the widget close-button handling described in the issue. Reproduce the problem by repeatedly executing the plot cell and by explicitly closing the widget, then verify that figure count, memory use, and the more-than-20-figures warning no longer increase.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, jupyter-notebook, python
Domain
backend, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.