4

I am using an iterative loop to plot soame data using Matplotlib. When the code has saved around 768 plots, it throws the following exception.

RuntimeError: Could not allocate memory for image

My computer has around 3.5 GB RAM. Is there any method to free the memory in parallel so that the memory does not get exhausted?

2 Answers 2

8

Are you remembering to close your figures when you are done with them? e.g.:

import matplotlib.pyplot as plt

#generate figure here
#...
plt.close(fig)  #release resources associated with fig
Sign up to request clarification or add additional context in comments.

3 Comments

Yes I was not closing the figure. Thanks a lot. But I am getting an exception: Traceback (most recent call last): File "C:\Python27\lib\site-packages\matplotlib\backends\backend_qt4.py", line 156, in <lambda> lambda: self.close_event()) File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 1564, in close_event self.callbacks.process(s, event) RuntimeError: wrapped C/C++ object of type FigureCanvasQTAgg has been deleted
Seems like something on funny with one of the backends. Are you modifying which backends are incorporated? (FWIW, I doubt I'll know the answer to this one, it might be worth opening another question after a little more investigation)
That is a bug with the QT4 backend where they get torn down slightly out of order and the c++ object is released and cleaned up by pyqt before mpl is really done with it. It is safe to ignore those errors and it has been fixed on trunk (I don't remember if the fix made it into the 1.2 bug fix releases)
4

As a slightly different answer, remember that you can re-use figures. Something like:

fig = plt.figure()
ax = plt.gca()

im = ax.imshow(data_list[0],...)

for new_data in data_list:
    im.set_cdata(new_data)
    fig.savefig(..)

Which will make your code run much faster as it will not need to set up and tear down the figure 700+ times.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.