How to clear memory completely of all matplotlib plots

Especially when you are running multiple processes or threads, it is much better to define your figure variable and work with it directly:

from matplotlib import pyplot as plt

f = plt.figure()
f.clear()
plt.close(f)

In any case, you must combine the use of plt.clear() and plt.close()

UPDATE (2021/01/21)

If you are using a MacOS system along with its default backend (referred as ‘MacOSX’), this does NOT work (at least in Big Sur). The only solution I have found is to switch to other of the well-known backends, such as TkAgg, Cairo, etc. To do it, just type:

import matplotlib
matplotlib.use('TkAgg') # Your favorite interactive or non-interactive backend

Leave a Comment