Skip to content
Advertisement

Matplotlib runs out of memory when plotting in a loop

I have a fairly simple plotting routine that looks like this:

JavaScript

When I plot this in single iterations, it works fine. However, the moment I put it in a loop, matplotlib throws a hissy fit…

JavaScript

This happens on iteration 2 (counting from 1), if that makes a difference. The code is running on Windows XP 32-bit with python 2.5 and matplotlib 0.99.1, numpy 1.3.0 and scipy 0.7.1.

EDIT: The code has now been updated to reflect the fact that the crash actually occurs at the call to legend(). Commenting that call out solves the problem, though obviously, I would still like to be able to put a legend on my graphs…

Advertisement

Answer

Is each loop supposed to generate a new figure? I don’t see you closing it or creating a new figure instance from loop to loop.

This call will clear the current figure after you save it at the end of the loop:

JavaScript

I’d refactor, though, and make your code more OO and create a new figure instance on each loop:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement