6

I need to create lots of figures, then use savefig to save the figure.

But after about 280 pictures, it throws the exception RuntimeError: Could not allocate memory for image

Is there some function like clear() in Matplotlib ?

3
  • Reassign any variables referring to your pictures and Python should clear the memory for you. Commented Nov 17, 2014 at 6:57
  • 1
    Are you using a single figure object or creating many figures? Commented Nov 17, 2014 at 6:59
  • 1
    possible duplicate of Matplotlib runs out of memory when plotting in a loop Commented Nov 17, 2014 at 19:12

2 Answers 2

7

Yes, you can use:

Sign up to request clarification or add additional context in comments.

1 Comment

dont forget stackoverflow.com/questions/20401057/… : see were it says : Remember that plt.show() is a blocking function, so in the example code you used above, plt.close() isn't being executed until the window is closed, which makes it redundant. You can use plt.ion() at the beginning of your code to make it non-blocking, although this has other implications.
0

You can try:

i=1
while i<=280:
    plt.figure(i).clear()
    i+=1

But you have to numbered your figures:

import matplotlib.pyplot as plt
from numpy import *

# example of data:
x = linspace(0,4,1e3)
data = sin(10*x)

plt.figure(1)
plt.plot(x, data)
plt.show()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.