I'd like to know the correct way to release memory after a plot is done since I'm getting a RuntimeError: Could not allocate memory for image
error when plotting multiple images in a loop.
Currently I have the following commands in another code to supposedly do just that:
import matplotlib.pyplot as plt
# The code
.....
# Make plot
fig = plt.figure()
# Plotting stuff.
plt.imshow(...)
plt.plot(...)
plt.scatter(...)
# Save plot to file.
plt.savefig(...)
# Release memory.
plt.clf()
plt.close()
A comment in this answer states that the correct syntax is actually plt.close(fig)
but the highest voted answer given here says that plt.clf()
is enough and doesn't mention .close
.
The questions are: what is(are) the correct command(s) to release memory after the plot is saved to file? Do I need both .clf
and .close
or is one of them enough?