[Matplotlib-devel] High priority for 3.2: figure cleanup

Jody Klymak jklymak at uvic.ca
Tue Apr 16 10:50:21 EDT 2019

I think if it makes our lives easier the paradigm that a closed window means the figure is closed programmatically is pretty straightforward and standard across GUI interfaces.  If folks need a workflow where they access the figure while it’s open they can turn off blocking.  I vote we keep it simple and predictable rather than tie ourselves in knots with complicated ways of keeping track of what we think the user may want to do.  

Cheers.  Jody

Sent from my iPhone

> On Apr 16, 2019, at 12:19 AM, Eric Firing <efiring at hawaii.edu> wrote:
> In today's weekly meeting, with Hannah, Thomas, and myself, we discussed https://github.com/matplotlib/matplotlib/issues/8519, which points to the long-standing problem that matplotlib relies on garbage collection to release memory after a figure is closed.  With some work-flows the automatic collection doesn't happen often enough and the user must explicitly trigger it.  It would be better if pyplot cleaned up after itself, breaking reference cycles when a figure is closed.  I think it is time to make this change, so I marked the issue "Release-critical". I am hoping we can get this done as a high-priority task, *early* in the 3.2 cycle.
> The central point is that closing a figure should clear it, and clearing it should recursively walk through the children, breaking connections and reference cycles all the way down.  One possibility would be to modify or replace the figure object such that any attempt to operate on the figure after closing would fail by raising an informative exception.
> Question: should closing the gui window also close the figure?  No, because that would prevent the user from calling savefig on it.  This would interfere with a reasonable use-case, in which plt.show is used in blocking mode, the window is closed, and the figure is saved via savefig.  Problem: if closing the gui window doesn't close the figure, how can it ever be closed?
> This leads to the suggestion that the pyplot state manager keep a list of "latent figures" which are no longer displayed in a gui window but have not been closed.  They could then be explicitly closed by "plt.close('all')".  This also opens the possibility for an API allowing one to re-display a figure after closing its gui window. For example, one might do a calculation, show it in a figure in a blocking window with "plt.show", close the window, do additional calculations, add them to the same figure, and bring the figure back in a new gui window.  This might be done in a loop, incrementing the contents of the figure.
> See also:
> https://github.com/matplotlib/matplotlib/issues/6793
> https://github.com/matplotlib/matplotlib/issues/6982
> Eric
> _______________________________________________
> Matplotlib-devel mailing list
> Matplotlib-devel at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-devel

More information about the Matplotlib-devel mailing list