When to use cla (), clf () or close () to clear a plot in matplotlib?

Matplotlib offers features:

cla() # Clear axis clf() # Clear figure close() # Close a figure window 

The documentation does not contain much information about the difference between these functions. When should each function be used and what exactly does it do?

+303
matplotlib
Nov 21 '11 at 14:38
source share
2 answers

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.

+414
Nov 22 '11 at
source share

There is only a warning I discovered today. If you have a function that calls the plot many times, it is better to use plt.close(fig) instead of fig.clf() , since the first does not accumulate in memory. In short , if memory is a concern, use plt.close (fig) (although it seems that there are more efficient ways, skip to the end of this comment for related links).

So the following script will create an empty list:

 for i in range(5): fig = plot_figure() plt.close(fig) # This returns a list with all figure numbers available print(plt.get_fignums()) 

While this one will create a list with five digits on it.

 for i in range(5): fig = plot_figure() fig.clf() # This returns a list with all figure numbers available print(plt.get_fignums()) 

From the above documentation it is not clear what is the difference between closing a figure and closing a window. Perhaps this will become clear.

If you want to try the full script, you have:

 import numpy as np import matplotlib.pyplot as plt x = np.arange(1000) y = np.sin(x) for i in range(5): fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.plot(x, y) plt.close(fig) print(plt.get_fignums()) for i in range(5): fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.plot(x, y) fig.clf() print(plt.get_fignums()) 

If the memory is troubling, someone has already sent a crawl to SO, see Create a reference counting digit

+39
Oct 26 '15 at 10:19
source share



All Articles