Closing a figure from the notebook does not close the python figure
See original GitHub issueThis 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 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 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.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:12 (8 by maintainers)

Top Related StackOverflow Question
As @jkrumbiegel points out at https://github.com/jupyterlab/jupyterlab/issues/5373, it can also be confusing to get this error by just evaluating the cell creating the figure over and over again.
Would it be possible to eliminate the cross-language counting problem by adding a magic directive that allows a user to specify that there will only be one view of the widget?
That way, it doesn’t break any of the more complicated use cases. For the the 99% of cases where only one view of a figure is used, it solves this issue.