When to use cla(), clf() or close() for clearing a plot in matplotlib?
They all do different things, since matplotlib uses a hierarchical order in which a figure window contains a figure which may consist of many axes. Additionally, there are functions from the pyplot interface and there are methods on the Figure
class. I will discuss both cases below.
pyplot interface
pyplot
is a module that collects a couple of functions that allow matplotlib to be used in a functional manner. I here assume that pyplot
has been imported as import matplotlib.pyplot as plt
.
In this case, there are three different commands that remove stuff:
See matplotlib.pyplot
Functions:
plt.cla()
clears an axis, i.e. the currently active axis in the current figure. It leaves the other axes untouched.plt.clf()
clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots.plt.close()
closes a window, which will be the current window, if not specified otherwise.
Which functions suits you best depends thus on your use-case.
The close()
function furthermore allows one to specify which window should be closed. The argument can either be a number or name given to a window when it was created using figure(number_or_name)
or it can be a figure instance fig
obtained, i.e., usingfig = figure()
. If no argument is given to close()
, the currently active window will be closed. Furthermore, there is the syntax close('all')
, which closes all figures.
methods of the Figure class
Additionally, the Figure
class provides methods for clearing figures.
I'll assume in the following that fig
is an instance of a Figure
:
fig.clf()
clears the entire figure. This call is equivalent to plt.clf()
only if fig
is the current figure.
fig.clear()
is a synonym for fig.clf()
Note that even del fig
will not close the associated figure window. As far as I know the only way to close a figure window is using plt.close(fig)
as described above.
Difference between plt.close() and plt.clf()
plt.close()
will close the figure window entirely, where plt.clf()
will just clear the figure - you can still paint another plot onto it.
It sounds like, for your needs, you should be preferring plt.clf()
, or better yet keep a handle on the line objects themselves (they are returned in lists by plot
calls) and use .set_data
on those in subsequent iterations.
Clearing a subplot in Matplotlib
ax.clear()
clears the axes. That is, it removes all settings and data from the axes such that you are left with an axes, just as it had been just created.ax.axis("off")
turns the axes off, such that all axes spines and ticklabels are hidden.ax.set_visible(False)
turns the complete axes invisible, including the data that is in it.ax.remove()
removes the axes from the figure.
Complete example:
import matplotlib.pyplot as plt
fig,axes = plt.subplots(2,3)
for ax in axes.flat:
ax.plot([2,3,1])
axes[0,1].clear()
axes[1,0].axis("off")
axes[1,1].set_visible(False)
axes[0,2].remove()
plt.show()
Related Topics
How to Convert .Dat to .Csv Using Python
How to Write to an Existing Excel File Without Overwriting Data (Using Pandas)
Pandas - How to Compare 2 CSV Files and Output Changes
Find Largest Number from User Input
How to Update Sqlalchemy Orm Object by a Python Dict
How to Block Comment Code in the Ipython Notebook
How to Convert Column With String Type to Int Form in Pyspark Data Frame
Python Pandas Count the Number of Occurances Inside Lists in a Column
Making a Discord Bot Change Playing Status Every 10 Seconds
How to Convert List into String With Quotes in Python
How to Connect to a Remote Windows Machine to Execute Commands Using Python
Printing Even Characters With Strings in Python
Python How to Remove Escape Characters from a String
Image.Open() Cannot Identify Image File - Python
How to Send Smtp Email for Office365 With Python Using Tls/Ssl