How to clear a plot with Matplotlib

0 min

Sometimes, it might end up needing to clear a plot with Matplotlib.

With the axes.clear() you clear the axis of a figure.

Here is an example

import matplotlib.pyplot as plt
import pandas as pd

# We set up the example dataframe
df = pd.DataFrame({"col1": range(10)})

# We set up our canvas
fig, axes = plt.subplots(1,1, figsize=(10,10))

# We plot the col1
axes.plot(df["col1"])

# How to clear a plot with Matplotlib
axes.clear()

# We plot once again
axes.plot(df["col1"])

plt.show()
How to clear a plot with the .clear() method

Here you are! You know now how to clear a plot with the axes.clear() method.