In Matplotlib:
- I can write
matplotlib.pyplot.cla()asaxes.clear(), whereaxesis anmatplotlib.axes.Axesobject. - I can write
matplotlib.pyplot.clf()asfig.clear(), wherefigis amatplotlib.figure.Figureobject.
Then there is matplotlib.pyplot.close(). This closes a window. Does this window correspond to any class? Is there a x.close() that is equivalent to matplotlib.pyplot.close() but in a more object oriented style?
The source code for matplotlib.pyplot.close() for my version is as follows:
if len(args)==0:
figManager = _pylab_helpers.Gcf.get_active()
if figManager is None: return
else:
_pylab_helpers.Gcf.destroy(figManager.num)
elif len(args)==1:
arg = args[0]
if arg=='all':
_pylab_helpers.Gcf.destroy_all()
elif isinstance(arg, int):
_pylab_helpers.Gcf.destroy(arg)
elif is_string_like(arg):
allLabels = get_figlabels()
if arg in allLabels:
num = get_fignums()[allLabels.index(arg)]
_pylab_helpers.Gcf.destroy(num)
elif isinstance(arg, Figure):
_pylab_helpers.Gcf.destroy_fig(arg)
else:
raise TypeError('Unrecognized argument type %s to close'%type(arg))
else:
raise TypeError('close takes 0 or 1 arguments')
So, it appears to be based on the staticmethod
matplotlib._pylab_helpers.Gcf.destroy which does a variety of things I don’t quite follow, noting that In the interactive backends, this is bound to the window “destroy” and “delete” events. But I’m not sure how figures and windows are related.
Perhaps I’m wasting my time even thinking about this.
Gcfis a global singleton object that keeps a list of the figures you have opened withplt.figure.It is essentially a dictionary with some fancy code around it to keep things consistent and keep track of the active figure, ect. The class that is used as a layer between
Figure(which is what is returned byplt.figure) and the GUI layer isFigureManagerBase(and it’s per-toolkit sub-classes) which is defined inbackend_bases.py.Which sub-class you are using is determined by which backend you are using. These sub-classes deal with things like making sure the figure gets deleted when you click the ‘x’ button, and the system level gui call-backs.
FigureManagers have adestroymethod which triggers their internal tear down. In principle, if you have aFigureobject and want to destroy it by hand, you just need to get a hold of the manager. A reference to it isn’t stored in theFigureobject, but is stored in theCanvasobject so you can destroy a window viawhich will tear the figure down, however if you do this, it might not properly clean up
Gcf(which will retains a reference to the figure manager) which will make future figures made withpyplothave unexpected numbers (it won’t go back to one),gcawill not work as exected, ect (this is the case withQt4).If you are going to use
pyplotat all (and be sure it will always work on every backend), then you either need toGcf(it’s in_pyplot_helpers.pyand really isn’t too complicated once you see what it is trying to dolib/matplotlib/tests/test_backend_qt4.pyfor how to do with withqt4, but this is going to be very toolkit dependent)plt.close(fig.number)