question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 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:open
  • Created 7 years ago
  • Reactions:4
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
jasongroutcommented, Sep 24, 2018

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.

1reaction
EricGallimorecommented, Aug 3, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

matplotlib.close() does not close plot - Stack Overflow
I'm using Python 3.6 in jupyter notebook. plt.close does not close plot. I tried with plt.ion() also and many other ways.
Read more >
How To Clear A Plot In Python (without closing the window)
Click to clear a plot using two methods - clearing Pyplot Figure or clearning PyPlot axes, without closing the plot window.
Read more >
matplotlib.pyplot.show — Matplotlib 3.6.2 documentation
At the end of (a blocking) show() the figure is closed and thus unregistered from pyplot. Calling pyplot.savefig afterwards would save a new...
Read more >
Visualization with Matplotlib | Python Data Science Handbook
show() in Matplotlib mode is not required. Plotting from an IPython notebook¶. The IPython notebook is a browser-based interactive data analysis tool that ......
Read more >
How do I close all the open pyplot windows (Matplotlib)?
How do I close all the open pyplot windows (Matplotlib)? - plt.figure().close(): Close a figure window.close() by itself closes the current ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found