plt.close() not releasing memory? Or memory leak elsewhere?

Hi All.

I’m experiencing some memory issues with some python/matplotlib code. I have a large object/class containing a lot of data, and whilst using that object to create (about 30) graphs I keep running into memory errors. Here is a description of my problem:

Firstly, I am able to create each graph individually. So if I run each graph creation function individually in the interpreter I get the desired result:

MyClass.TypicalPlot(save=True, show = False)

However, if I create a new function to run each graph function in a script then I get a “MemoryError: Could not allocate memory for path”. (varies at the precise point where, but its normally after 3 or 4 graphs). I am able to run each plot function in an interpreter without issue, so I can only assume its some kind of memory/garbage collection issue.

def saveAllPlots(self, comments = False):

        if self.comment is None: comment = False
        else: comment = True
        self.TypicalPlot(save=True, show=False, comment=comment)
        self.AnotherPlot(save=True, show=False)
        self.AnotherPlot2(save=True, show=False)
        self.AnotherPlot3(save=True, show=False)
        ...etc, etc, etc

Here is what a typical plot function looks like in my code (they are all very similar)

def TypicalPlot(self, title=None, comment=False, save=False, show=True):

if title is None:
    title = self.dat.title

fig = plt.figure()
host = SubplotHost(fig, 111)
par = host.twinx()
host.set_xlabel("Time (hrs)")
host.set_ylabel("Power (W)")
par.set_ylabel("Temperature (C)")
p1, = host.plot(self.dat.timebase1, self.dat.pwr, 'b,', label="Power",
                markevery= self.skip)
p2, = par.plot(self.dat.timebase2, self.dat.Temp1, 'r,',
               label="Temp 1", markevery= self.skip)
p3, = par.plot(self.dat.timebase2, self.dat.Temp2, 'g,',
               label="Temp 2", markevery= self.skip)
p4, = par.plot(self.dat.timebase2, self.dat.Temp3, 'm,',
               label="Temp 3", markevery= self.skip)
# par.axis["right"].label.set_color(p2.get_color())
#host.legend(loc='lower left')
plt.title(title+" Temperature")

leg=host.legend(loc='lower left',fancybox=True)
frame  = leg.get_frame()

### make the legend text smaller
for t in leg.get_texts():

### set the legend text color to the same color as the plots for added
### readability

if show is True and save is True:
elif show is True and save is False:
elif show is False and save is True:

Has anyone come across this before?


Sam Stevenson