8

Is it possible to re-open a closed figure (i.e., one the user X'd) in matplotlib? The following code shows the naive approach:

In [14]: fig = figure(10)

In [15]: close(fig)

In [16]: fig.show()

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 495, in callit
    func(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py", line 253, in idle_draw
    self.draw()
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py", line 239, in draw
    tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/tkagg.py", line 19, in blit
    tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
TclError: this isn't a Tk application

I'm trying to create a figure with widgets in it, so a workaround is available (just make a new figure), but I would like to know if the figure instance is totally useless after being closed.

keflavich
  • 18,278
  • 20
  • 86
  • 118

1 Answers1

0

Yes, closing the figure deletes it completely.

vextorspace
  • 934
  • 2
  • 10
  • 25
  • 6
    Actually, that's not true at all. Closing a figure destroys the gui toolkit's (exactly what depends on the backend) window object, but not the figure object. The figure object still exists, and you can still save it (e.g. `fig.savefig(...)` will still work perfectly after you've closed the figure.) – Joe Kington Mar 08 '12 at 20:38
  • 2
    However, I'm referring to closing the figure via the gui, not by calling `fig.close()` (Which does destroy things). – Joe Kington Mar 08 '12 at 20:49
  • @JoeKington - That's actually the case I'm interested in: when the user closes a window via the GUI. I put the above MWE because I had assumed close(fig) and "pressing X" did the same thing. If I press x and try `fig.show()`, I get the same exception. – keflavich Mar 11 '12 at 22:17
  • @keflavich - I'm afraid I don't have an answer. However, I suspect that any answer will be very gui-backend-specific (and probably not possible with all backends), and that re-creating the figure as you mentioned will be the simplest approach. – Joe Kington Mar 13 '12 at 17:39