matplotlib / matplotlib/ipympl
Closing a figure from the notebook does not close the python figure
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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