How to clear Seaborn Plot : 5 Steps only

How to clear Seaborn Plot

Suppose you want to draw more than one plot on a figure. Then how you will do so? There are many methods to do so. In this tutorial, you will know how to clear seaborn plots through steps.

Steps to clear seaborn plot

In this section, you will know all the steps required to clear a seaborn plot. Just follow the below steps for more understanding.

Step 1: Import the required libraries

The first step is to import all the necessary libraries. Here I will use two libraries. One is matplotlib and the other is the seaborn python package. Let’s import all of them using the import statement.

import matplotlib.pyplot as plt
import seaborn as sns

Step 2: Load the dataset

The next step is to load or create a dataset for plotting. In this example, I am using the Iris dataset that is already provided by the seaborn library. You can use your own dataset. Let’s load the dataset.

iris = sns.load_dataset('iris')

Step 3: Create the plots

After loading the datasets let’s create two plots. One for the sepal_length that will be used to plot the histogram. And the second is the scatterplot of the Iris petal_length and petal_width.

Add the following lines of code.

sns.histplot(data=iris, x="sepal_length", kde=True)
sns.scatterplot(data=iris, x="petal_length", y="petal_width", hue="species")

Step 4: Clear the Searborn plot

Not the final step is to cleat the first plot before plotting the second plot. It means After plotting the histogram plot for the iris data you have to clear it to allow the figure to plot to scatter plot only.

To do you have to put plt.clf() between the two plots just like below.

sns.histplot(data=iris, x="sepal_length", kde=True)
plt.clf()
sns.scatterplot(data=iris, x="petal_length", y="petal_width", hue="species")

Step 5: Show the plot

Now the last step is to plot the figure. To do so you will use plt.show() for that.

Below is the complete code for all the steps done above.


import matplotlib.pyplot as plt
import seaborn as sns

iris = sns.load_dataset('iris')
sns.histplot(data=iris, x="sepal_length", kde=True)
plt.clf()
sns.scatterplot(data=iris, x="petal_length", y="petal_width", hue="species")

plt.show()

Output

plotting second plot after clearing the first plot
plotting the second plot after clearing the first plot

If you don’t use the plt.clf() then both plots will be in the same figure.

two plots on the same figure
two plots on the same figure

Conclusion

It’s better that you should clear the previous plots before plotting the new figure. If you do not do so then more than one plot will be plotted on the same figure. The above steps used the iris data on two plots and plot the scatter plot after clearing the first plot.

I hope you have liked this tutorial. If you have any doubts or suggestions then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner