Clear a Matplotlib Plot: cla(), clf(), clear(), and close()

Quick answer: Clear the smallest Matplotlib object that matches the job: ax.clear() removes artists from one Axes, fig.clear() resets a Figure, plt.cla() clears the current Axes, plt.clf() clears the current Figure, and plt.close() releases a figure that is no longer needed.

Python Pool infographic comparing Matplotlib axes clear, figure clear, pyplot cla, clf, and close
Clear an Axes when reusing a plot area, clear a Figure when resetting its contents, and close figures when they are no longer needed.

To clear a plot in Matplotlib, choose the clearing method based on what you want to remove. Use ax.clear() when you want to clear one axes object, fig.clear() when you want to clear the whole figure, plt.cla() for the current axes, plt.clf() for the current figure, and plt.close() when you are done with a figure and want Matplotlib to release it.

This matters in notebooks, scripts, animation loops, and report generation. Clearing the wrong object can remove more than you expected, while forgetting to close figures in a loop can waste memory.

Quick comparison

Method What it clears Use it when
ax.clear() One Axes You are using the object-oriented Matplotlib style.
fig.clear() The whole Figure You want to reuse a figure after removing its axes and artists.
plt.cla() The current axes You are using pyplot state-style code.
plt.clf() The current figure You want to clear the active figure.
plt.close() The figure window/reference You are finished with the figure and want to free resources.

The official Matplotlib API documents Axes.clear(), Figure.clear(), pyplot.cla(), pyplot.clf(), and pyplot.close() separately because they solve different problems.

Clear one axes with ax.clear()

Use ax.clear() when you created the plot with fig, ax = plt.subplots(). This clears lines, labels, ticks, legends, and other artists from that axes, while keeping the figure object available.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot([1, 2, 3], [2, 4, 6])
ax.set_title("First plot")

ax.clear()
ax.plot([1, 2, 3], [6, 4, 2])
ax.set_title("Plot after ax.clear()")

plt.show()

This is the best default for most new Matplotlib code because the axes object is explicit. If you need to understand how Matplotlib picks the current axes, read our current Matplotlib gca() guide.

Clear an entire figure with fig.clear()

Use fig.clear() when you want to remove everything from a figure. This is broader than ax.clear() because a figure can contain multiple subplots, legends, colorbars, and text artists.

import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot([1, 2, 3], [1, 4, 9])
ax2.plot([1, 2, 3], [9, 4, 1])

fig.clear()

ax = fig.add_subplot(111)
ax.plot([1, 2, 3], [3, 3, 3])
ax.set_title("New axes after fig.clear()")

plt.show()

Figure.clear() also has a keep_observers option for cases such as GUI widgets that track axes. Most simple scripts do not need to set it.

Use plt.cla() for the current axes

plt.cla() clears the current axes. It is the pyplot equivalent of clearing whichever axes Matplotlib currently considers active.

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [10, 20, 30])
plt.title("This will be cleared")

plt.cla()

plt.plot([1, 2, 3], [30, 20, 10])
plt.title("After plt.cla()")
plt.show()

This is convenient in quick examples, but explicit axes code is usually easier to maintain in larger programs.

Use plt.clf() for the current figure

plt.clf() clears the current figure. Use it when you are working with pyplot and want to remove all axes from the active figure.

import matplotlib.pyplot as plt

plt.subplot(1, 2, 1)
plt.plot([1, 2, 3], [1, 4, 9])

plt.subplot(1, 2, 2)
plt.plot([1, 2, 3], [9, 4, 1])

plt.clf()
plt.plot([1, 2, 3], [2, 2, 2])
plt.title("New plot after plt.clf()")
plt.show()

If you are saving figures before clearing them, see our Matplotlib savefig() examples.

Use plt.close() when you are done

Clearing a figure is not the same as closing it. plt.close() unregisters a figure from pyplot. This is important when creating many figures, especially in scripts that save files without showing a window.

import matplotlib.pyplot as plt

for number in range(5):
    fig, ax = plt.subplots()
    ax.plot([1, 2, 3], [number, number + 1, number + 2])
    fig.savefig(f"plot-{number}.png")
    plt.close(fig)

Use this pattern for repeated exports. If you only clear the axes or figure inside a long loop, pyplot can still keep references to figures you no longer need.

Common mistakes

  • Using plt.clf() when you only meant to clear one subplot: use ax.clear() for one axes.
  • Expecting clear() to close a window: use plt.close() when you are finished with the figure.
  • Clearing labels and then forgetting to recreate them: after ax.clear(), set the title, labels, legend, and style again.
  • Trying to clear the terminal instead of the plot: use our Python shell clearing guide if you want to clear IDLE, PowerShell, Terminal, or Command Prompt output.

Related Matplotlib guides

For nearby plotting tasks, see how to change Matplotlib background color, add a Matplotlib arrow, create a scree plot in Python, and understand NumPy axis behavior when your plot data comes from arrays.

Conclusion

Use ax.clear() for one axes, fig.clear() for a whole figure object, plt.cla() for the current axes, plt.clf() for the current figure, and plt.close() when the figure should be released. For modern Matplotlib code, prefer the explicit object-oriented pattern with fig, ax = plt.subplots() and clear the specific object you intend to reuse.

Clear One Axes With ax.clear()

The object-oriented API makes the scope explicit. When a Figure contains several subplots, calling clear() on one Axes preserves the other axes and lets the same plotting area be reused.

import matplotlib.pyplot as plt

fig, (left, right) = plt.subplots(1, 2)
left.plot([1, 2, 3])
right.plot([3, 2, 1])
left.clear()
left.plot([2, 4, 6])
plt.show()

Reset A Whole Figure

fig.clear() removes the Figure’s axes and artists. Use it when the next render should start from a blank Figure, not when an individual subplot must remain intact. The optional keep_observers parameter matters only when external GUI observers track the axes.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 4, 9])
fig.clear()
ax = fig.add_subplot(111)
ax.plot([2, 3, 5])
plt.show()

Understand pyplot cla() And clf()

The pyplot functions operate on the current state: cla() clears the current Axes and clf() clears the current Figure. They are convenient in short scripts, but the object-oriented methods are easier to reason about when several figures or axes exist.

import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.cla()
plt.plot([3, 2, 1])
plt.clf()
plt.close()

Close Figures In Loops

Clearing a figure removes its plotted content, while closing it releases the figure manager and is the right final step for batch rendering. In a loop, close each saved figure or an application can retain many open figures and consume memory.

import matplotlib.pyplot as plt

for index in range(3):
    fig, ax = plt.subplots()
    ax.plot([index, index + 1])
    fig.savefig(f"plot-{index}.png")
    plt.close(fig)

Matplotlib’s official Axes.clear(), Figure.clear(), and pyplot.close() references define their scopes and lifecycle behavior.

For related figure workflows, compare saving Matplotlib figures, figure sizing, and subplot spacing when deciding whether to reuse or recreate a plot.

Python Pool infographic showing a Matplotlib figure, axes, artists, and current plot state
[object Object]
Python Pool infographic comparing Matplotlib cla, clf, clear, close, and scope
[object Object]
Python Pool infographic showing a Matplotlib loop redrawing labels, limits, and artists
[object Object]
Python Pool infographic checking Matplotlib output, backend, layout, saved file, and tests
[object Object]

Frequently Asked Questions

How do I clear a Matplotlib plot?

Call ax.clear() to remove artists from one Axes, or plt.clf() to clear the current Figure when using the pyplot state machine.

What is the difference between cla() and clf()?

cla() clears the current Axes, while clf() clears the current Figure and its Axes.

How do I release Matplotlib memory?

Call plt.close(fig) after a figure is no longer needed, especially in loops or server-side rendering.

Should I clear or close a figure in an animation?

Reuse and clear the Axes when the same Figure should remain visible; close the Figure when the output is complete and the object should be released.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted