Python Pool: How to Clear Plot in Matplotlib Using clear() Method

Hello programmers, in today’s article, we will discuss Matplotlib clear plot in python. Matplotlib is a library in Python, which is a numerical – mathematical extension for NumPy library. The figure module of the Matplotlib library provides the top-level Artist, the Figure, which contains all the plot elements. The figure module is used to control the subplots’ default spacing and top-level container for all plot elements. The Axes Class contains the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. The instances of Axes supports callbacks through a callbacks attribute. The Axes.clear() in the axes module of the matplotlib library is used to clear the axes. Thus, the Matplotlib figure.clear() and axes.clear() is used to clear figure and axes, respectively. Here are the syntax and parameters of the Matplotlib clear plot function.

Contents of Tutorial

Syntax of Matplotlib clear plot in Python

clear(self, keep_observers=False)


The axis.clear() function accepts no parameters.
The ‘keep_observers’ parameter in figure.clear() function is a boolean value.

Return type

The clear() function does not return any value. It is used in python programs to clear plots.

Example of Matplotlib Figure clear plot

import numpy as np 
import matplotlib.pyplot as plt 
fig, ax = plt.subplots() 
ax.plot([1, 2, 3]) 
function Examplenn', fontweight ="bold") 


Matplotlib clear figure plot in Python


In the above example, we first create a plot as per data values given as input. The xlabel is set as ‘x-axis,’ and the ylabel is set as ‘y-axis.’ The title of the figure is also mentioned as ‘matplotlib.figure.Figure.clear() function Example’. The gridlines are also plotted for the figure by setting ax.grid(True). But before the statement that shows the plotted figure, we use the fig.clear() function. The fig.clear() function clears the figure plot when ‘True’ is passed as an argument. Thus, in this example, since fig.clear(True) is implemented before the, the output is given such that the entire current figure is cleared except the figure title.

Example of Matplotlib Axis clear plot

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, sharex = True) 
ax.plot(t, s) 
ax.set_title('matplotlib.axes.Axes.clear() Examplenn Sample Example', 
             fontsize = 12, fontweight ='bold') 
ax1.plot(t, s) 
ax1.set_title('Above example with clear()  
function', fontsize = 12, fontweight ='bold') 


Matplotlib Axes clear plot in python


In the above example, the two plots ‘ax’ and ‘ax1’ are created. The ylabel of figure 1 is set as ‘y-axis.’ The Matplotlib grid() is also set as ‘True,’ which returns grid lines for the figure. Also, the title of the figure is mentioned. But, we do not use the Matplotlib clear() function with the ‘ax’ plot. For the second figure, we plot it as per the given input values. But since the ax2.clear() is used, the current ‘ax2’ figure plot is cleared except for its title. Finally, when the statement is implemented, it gives the ‘ax’ figure and clears the ‘ax2’ figure with just its title.

Must Read


In this article, we have discussed ways of Matplotlib clear plot in Python. The clear() function can be used as axes.clear() or figure.clear() which clears the axes and figure of the plot, respectively. We have discussed both axes clearly and figure clear with examples and explanations. The Matplotlib cla() function can be used as axes.clear() function. Similarly, the clf() function can be used for figure clear. Both cla() and clf() is used for clearing plot in Matplotlib.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Author: Shantun Parmar

3 thoughts on “Python Pool: How to Clear Plot in Matplotlib Using clear() Method

  1. I am also writing to make you know what a impressive encounter my friend’s girl gained reading through your web page. She learned plenty of pieces, which include what it’s like to possess an amazing helping mood to let most people smoothly know several impossible subject matter. You truly exceeded our own expectations. Thank you for offering these invaluable, safe, explanatory and even fun tips about this topic to Julie.

  2. My spouse and i got quite glad that John could deal with his preliminary research through the ideas he grabbed out of your web page. It’s not at all simplistic to just continually be giving out methods that many others have been selling. And we also fully understand we have got the website owner to be grateful to for that. The main explanations you have made, the simple site menu, the friendships you can help foster – it’s got many great, and it’s really helping our son and the family imagine that the matter is excellent, and that is pretty indispensable. Thank you for everything!

Thanks for your support, You may click on ads to encourage us which assits to writers.

Leave a Reply

Your email address will not be published.