New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
plt.close() not firing close_events on non-interactive backends #18609
Comments
To be honest I'm not really sure the concept of "closing" a non-interactive figure even makes sense. Nothing really changes from the point of view of the figure after you close it, if you keep a pointer to it, e.g. import matplotlib as mpl; mpl.use("agg")
from pylab import *
f = gcf()
plot()
close("all")
f.savefig("/tmp/test.png") saves a figure just fine. Or we could possibly really say |
I don't disagree. But since it doesn't raise an error or warning to call To give a bit more context/motivation: my close event listeners are designed to clean up lingering pointers to the figure being closed. There's a parent figure with data, and a few different child figures for changing various settings that affect the data display. The parent figure has pointers to the child figs stored as attributes. When the child figure is closed I want to reset all those attributes to Of course, doing all that with a non-interactive backend is not a typical use case, but (1) having it work the way I suggested makes testing easier by increasing consistency across backends, and (2) so far I don't see a downside to doing it that way (is there a downside I'm not seeing?). |
I agree that having |
To me this seems like an important and relevant "if". In our case (and in other scripts I've written), we don't keep a pointer, so it should get GC'ed after calling |
I guess we can tweak the semantics to whatever we want (I don't have a very strong opinion there), but they should hopefully be consistent across use cases. Currently, close_event is not emitted at all for the case of pyplotless canvas embedding in Qt or wx GUIs, because they handle it at the Manager (Qt)/Frame (wx, equiv. to MainWindow) level. This can easily be checked by adding @larsoner I guess if you really want to track object deletion you can use https://docs.python.org/3/library/weakref.html#weakref.finalize. |
Wow, I had no idea that I'm the reason we're in this middle ground |
Discussion summary: Add a Figure.close() method which only emits a We may also want to revisit the ability of figures being revived after being close()d. Personal comments: we may additionally want to add a flag checking whether |
drammock commentedSep 29, 2020
•
edited
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
Actual outcome
No console output with backends
agg
,svg
, andpdf
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
A less-minimal example that more closely approximates my use case is here: https://stackoverflow.com/q/64105454/1664024
The text was updated successfully, but these errors were encountered: