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

Matplotlib | cla() Vs. ca() Vs. close() methods: In this tutorial, we will learn about the difference between cla(), clf(), and close() methods in Matplotlib. By Pranit Sharma Last updated : July 19, 2023

What is cla() in Matplotlib?

The pyploy.cla() is a method of the Matplotlib library, it is used to clear the current axes in the current figure.

The following is the syntax of the cla() method:

matplotlib.pyplot.cla()

Example

Consider the below example demonstrating the working of the cla() method:

# Import numpy
import numpy as np

# Import pyplot module
import matplotlib.pyplot as plt

# Preparing some data
x = np.linspace(10, 20, 2)
y = np.cos(x**2)

# Creating a plot
plt.plot(x, y)
plt.ylabel("y-axis")
plt.xlabel("x-axis")

# Display original Result
plt.show()

The output is -

Matplotlib | cla() Vs. ca() Vs. close() methods | Output 1

Clearing axes

# Clear axis and display result
plt.cla()
plt.show()

The output after clearing axes is -

Matplotlib | cla() Vs. ca() Vs. close() methods | Output 2

What is clf() in Matplotlib?

The pyploy.clf() is a method of the Matplotlib library, it is used to clear the figure including all the axes but it does not close the figure window.

The following is the syntax of the cla() method:

matplotlib.pyplot.clf()

Example

Consider the below example demonstrating the working of the clf() method:

# Import numpy
import numpy as np

# Import pyplot module
import matplotlib.pyplot as plt

# Preparing some data
x = np.sin([0, 30, 45, 60, 90])

# Creating a plot
plt.plot(x)
plt.ylabel("y-axis")
plt.xlabel("x-axis")

# Display result
plt.show()

The output is -

Matplotlib | cla() Vs. ca() Vs. close() methods | Output 3

Clearing figure

# Clear figure and display result
plt.clf()
plt.show()

The output after clearing figure is -

Matplotlib | cla() Vs. ca() Vs. close() methods | Output 4

What is close() in Matplotlib?

The pyploy.close() is a method of the Matplotlib library, it is used to close the current opened window.

The following is the syntax of the close() method:

matplotlib.pyplot.close(fig=None)

Example

Consider the below example demonstrating the working of the close() method:

# Import numpy
import numpy as np

# Import pyplot module
import matplotlib.pyplot as plt

# Preparing some data
x = np.sin([0, 30, 45, 60, 90])

# Creating a plot
plt.plot(x)
plt.ylabel("y-axis")
plt.xlabel("x-axis")

# Clear Window
plt.close()

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

The cla() method is used to clear axes in the current figure, whereas the clf() method is used to clear the figure including all the axes but it does not close the figure window, whereas the close() method is used to close the currently opened window. The following is the comparison table between the cla(), clf(), and close() methods of the Matplotlib library.

cla() clf() close()
The pyplot.cla() clears the axes in the current figure. The pyplot.clf() clears the entire figure including axes. The pyplot.close() closes a the current opened window.
It does not affect the unactive axis. It does not close the window. If multiple windows are opened, it allows to specify which window should be closed.
Syntax of cla() method is: pyplot.cla() Syntax of clf() method is: pyplot.clf() Syntax of close() method is: pyplot.close(fig=None)



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.