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?

Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
tanzil
  • 1,461
  • 2
  • 12
  • 17

2 Answers2

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
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 1
    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: 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 – tanzil May 03 '13 at 14:24
  • 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) – mgilson May 03 '13 at 14:26
  • 3
    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) – tacaswell May 03 '13 at 22:35
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.

tacaswell
  • 84,579
  • 22
  • 210
  • 199