They all do different things, because matplotlib uses a hierarchical order in which the shape window contains a shape that can consist of many axes. In addition, there are functions from the pyplot interface, and there are methods in the Figure class. I will discuss both cases below.
pyplot interface
pyplot is a module that compiles several functions that allow matplotlib to be used functionally. I assume that pyplot was imported as import matplotlib.pyplot as plt . In this case, there are three commands that delete things:
plt.cla() clears the axis , i.e. the current active axis in the current drawing. This leaves the other axles intact.
plt.clf() clears the entire current drawing with all its axes, but leaves the open window such that it can be reused for other graphs.
plt.close() closes the window , which will be the current window, unless otherwise specified.
Which features suit you best depends on your use case.
In addition, the close() function allows you to specify which window should be closed. The argument can be either a number or the name given in the window when it was created using figure(number_or_name) , or it can be the resulting instance of the figure fig , i.e. Using fig = figure() . If close() not specified, the current active window will be closed. In addition, there is a close('all') syntax that closes all shapes.
Figure class methods
In addition, the Figure class provides methods for clearing shapes. I assume fig is an instance of Figure :
fig.clf() clears the entire drawing . This call is equivalent to plt.clf() only if fig is the current metric.
fig.clear() is synonymous with fig.clf()
Note that even del fig will not close the linked shapes window. As far as I know, the only way to close the shape window is to use plt.close(fig) as described above.
David Zwicker Nov 22 '11 at 2:54
source share