Skip to content
Related Articles

Related Articles

Improve Article

Difference Between cla(), clf() and close() Methods in Matplotlib

  • Last Updated : 13 Jan, 2021

Prerequisites: Matplotlib 

Matplotlib is a library in Python. It is a mathematical extension of Numpy Library. It is a comprehensive library for creating static, animated, and interactive visualizations in Python. Pyplot is a state-based interface to a Matplotlib module. The Pyplot can create many types of plot such as line graph, bar graph, histogram etc.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

The cla(), clf() and close() are different methods/functions of Matplotlib. The difference between them is as follows:



cla(): This method in the pyplot module of the matplotlib library is used to clear the current axes.

Syntax:

matplotlib.pyplot.cla()

Example :

Python3




import numpy as np
import matplotlib.pyplot as plt
  
t = np.linspace(0.0, 2.0, 401)
s = np.sin(2 * np.pi * t)
  
fig, [ax, ax1] = plt.subplots(2, 1)
  
ax.set_ylabel('y-axis')
ax.plot(t, s)
ax.grid(True)
  
ax1.set_ylabel('y-axis')
ax1.set_xlabel('x-axis')
ax1.plot(t, s)
ax1.grid(True)
# Function call
ax1.cla()
  
fig.suptitle('matplotlib.pyplot.cla Example')
plt.show()

Output:

The subplot ax1 content is cleared.

clf(): The method in the pyplot module of the matplotlib library is used to clear the entire current figure. It even clears the subplot. It leaves the window space open so that it can be reused by other plots.

Syntax:

matplotlib.pyplot.clf()

Example:

Python3




import numpy as np
import matplotlib.pyplot as plt
  
t = np.linspace(0.0, 2.0, 201)
s = np.sin(2 * np.pi * t)
  
fig, [ax, ax1] = plt.subplots(2, 1)
  
ax.set_ylabel('y-axis')
ax.plot(t, s)
ax.grid(True)
  
ax1.set_ylabel('y-axis')
ax1.set_xlabel('x-axis')
ax1.plot(t, s)
ax1.grid(True)
  
# Func. call
plt.clf()
  
fig.suptitle('matplotlib.pyplot.clf Example')
plt.show()

Output:

The clf() function cleared the entire figure and only space is left.

close(): The method in the pyplot module of the matplotlib library is used to close the window of the plot. By default, it closes the current window. Since the window is closed there will no output in this method.

Syntax:

matplotlib.pyplot.close()

Example:

Python3




import numpy as np
import matplotlib.pyplot as plt
  
t = np.linspace(0.0, 2.0, 201)
s = np.sin(2 * np.pi * t)
  
fig, [ax, ax1] = plt.subplots(2, 1)
  
ax.set_ylabel('y-axis')
ax.plot(t, s)
ax.grid(True)
  
ax1.set_ylabel('y-axis')
ax1.set_xlabel('x-axis')
ax1.plot(t, s)
ax1.grid(True)
  
# Function call
plt.close()
  
fig.suptitle('matplotlib.pyplot.close Example')
plt.show()

Table of Difference Between cla() vs. clf() vs. close()

cla()clf()close()
Used to clear the current axesUsed to clear the entire current figureUsed to close the window of the plot
It only clears the current active plotLeaves the window space open so that it can be reused by other plotsTerminates the plot completely
Doesn’t affect any other subplotIt even clears the subplot.Closes the plots entirely



My Personal Notes arrow_drop_up
Recommended Articles
Page :