26

I'm using matplotlib to generate many plots of the results of a numerical simulation. The plots are used as frames in a video, and so I'm generating many of them by repeatedly calling a function similar to this one:

from pylab import *

def plot_density(filename,i,t,psi_Na):  
    figure(figsize=(8,6))
    imshow(abs(psi_Na)**2,origin = 'lower')
    savefig(filename + '_%04d.png'%i)
    clf()

The problem is that the memory usage of the python process grows by a couple of megabytes with every call to this function. For example if I call it with this loop:

if __name__ == "__main__":
    x = linspace(-6e-6,6e-6,128,endpoint=False)
    y = linspace(-6e-6,6e-6,128,endpoint=False)
    X,Y = meshgrid(x,y)
    k = 1000000
    omega = 200
    times = linspace(0,100e-3,100,endpoint=False)
    for i,t in enumerate(times):
        psi_Na = sin(k*X-omega*t)
        plot_density('wavefunction',i,t,psi_Na)
        print i

then the ram usage grows with time to 600MB. If however I comment out the line figure(figsize=(8,6)) in the function definition, then the ram usage stays steady at 52MB. (8,6) is the default figure size and so identical images are produced in both cases. I'd like to make different sized plots from my numerical data without running out of ram. How might I force python to free up this memory?

I've tried gc.collect() each loop to force garbage collection, and I've tried f = gcf() to get the current figure and then del f to delete it, but to no avail.

I'm running CPython 2.6.5 on 64 bit Ubuntu 10.04.

Chris Billington
  • 855
  • 1
  • 8
  • 14

2 Answers2

36

From the docstring for pylab.figure:

In [313]: pylab.figure?

If you are creating many figures, make sure you explicitly call "close" on the figures you are not using, because this will enable pylab to properly clean up the memory.

So perhaps try:

pylab.close()     # closes the current figure
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • That does it! Thanks very much. – Chris Billington Sep 02 '10 at 03:27
  • 2
    Yep, `clf` doesn't delete the figure or release its resources, it only clears it. And `f = gcf(); del f` only deletes the reference you just created, it doesn't delete the figure object itself. That's what you need `close` for. (+1) – David Z Sep 02 '10 at 04:56
10

Closing a figure is definitely an option, however, repeated many times, this is time consuming. What I suggest is to have a single persistent figure object (via static function variable, or as additional function argument). If that object is fig, the function will then call fig.clf() before each plotting cycle.

from matplotlib import pylab as pl
import numpy as np

TIMES = 10
x = np.linspace(-10, 10, 100)
y = np.sin(x)
def withClose():
    def plotStuff(i):
        fig = pl.figure()
        pl.plot(x, y + x * i, '-k')
        pl.savefig('withClose_%03d.png'%i)
        pl.close(fig)
    for i in range(TIMES):
        plotStuff(i)


def withCLF():
    def plotStuff(i):
        if plotStuff.fig is None:
            plotStuff.fig = pl.figure()
        pl.clf()
        pl.plot(x, y + x * i, '-')
        pl.savefig('withCLF_%03d.png'%i)
    plotStuff.fig = None

    for i in range(TIMES):
        plotStuff(i)

Here is the timing values

In [7]: %timeit withClose()
1 loops, best of 3: 3.05 s per loop

In [8]: %timeit withCLF()
1 loops, best of 3: 2.24 s per loop
Community
  • 1
  • 1
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
  • 1
    Nice. It's clear to me now that with every call to `figure()` in my original example I was creating a new figure and not closing the old one. As well as hanging on to a reference, one could also provide the number argument: `figure(0,figsize=whatever)` to ensure the same figure was being used each time. – Chris Billington Jan 30 '12 at 03:26