how to free resources

Hi,
I am using matplotlib first time and I am wonder how to free resources

I have this script

# coding: utf-8

import matplotlib
matplotlib.use('Agg')

import pylab

class Graph:

    def __init__(self):
        # sirka stlpcov
        self.width = 0.5
        self.font = {'fontname':'Tahoma', 'fontsize':10}

    def setTitle(self, title):
        """Nastavi nadpis grafu
        """
        self.title = title

    def setXLabel(self, label):
        self.x_label = label

    def setYLabel(self, label):
        self.y_label = label

    def setYValues(self, values):
        """Nastavi y-ove hodnoty
        """
        self.y_values = values
        # vypocitame lave spodne rohy stlpcov
        self.x_values = [x + self.width / 2 for x in range(len(self.y_values))]
        # vypocitame stred stlpcov (znacky na x-ovej osi)
        self.x_ticks = [x + self.width for x in range(len(self.y_values))]

    def setXTickLabels(self, labels):
        """Nastavi popisky pre ciarky na x-ovej osi
        """
        self.x_ticksLabels = labels

    def makeGraph(self):
        self.fig = pylab.figure()
        self.fig.set_dpi(72)
        self.fig.set_figheight(3)
        self.fig.set_figwidth(5)

        self.fig.subplots_adjust(bottom=0.15)

        self.ax = self.fig.add_subplot(111)

        self.ax.bar(left=self.x_values, height=self.y_values,
width=self.width, color='#f00000')
        self.ax.set_xticks(self.x_ticks)
        self.ax.set_xticklabels(self.x_ticksLabels)

        self.ax.set_xlabel(self.x_label, **self.font)
        self.ax.set_ylabel(self.y_label, **self.font)
        self.ax.set_title(self.title, **self.font)

    def saveGraph(self, path):
        self.fig.savefig(path, format='png')

    def __del__(self):
        self.fig.delaxes(self.ax)
        del self.fig
        del self.ax

if __name__ == '__main__':
    import random
    import gc
    for i in range(5000):
        g = Graph()
        g.setTitle(u'Spotreba za rok 2008 podľa ATC skupín')
        g.setXLabel(u'ATC skupiny')
        g.setYLabel(u'Spotreba v EUR')
        g.setYValues([random.randint(0, x) for x in range(20)])
        g.setXTickLabels([chr(random.randint(65, 90)) for x in range(20)])
        g.makeGraph()
        g.saveGraph('grafy/' + str(i) + '.png')
        del g
        print gc.get_count()
        gc.collect()
        print gc.get_count()

When I used TkAgg, it crashes after 190 images (Fail to create pixmap
with Tk_GetPixmap in ImgPhotoInstanceSetSize. tried to delete photo
image when instances still exist).
When I switch to WX, it crashes after 400 images.
And Agg crashes when pagefile reaches 1.5x ram (cca 2000 images).

It is possible somehow free resources in matplotlib?

I use python 2.5.4, wx 2.8.9.2, numpy 1.3.0b1 on win XP

Thanks for answers.

Stano.

Stano Paška wrote:

Hi,
I am using matplotlib first time and I am wonder how to free resources

I have this script

# coding: utf-8

import matplotlib
matplotlib.use('Agg')

import pylab

class Graph:

    def __init__(self):
        # sirka stlpcov
        self.width = 0.5
        self.font = {'fontname':'Tahoma', 'fontsize':10}

    def setTitle(self, title):
        """Nastavi nadpis grafu
        """
        self.title = title

    def setXLabel(self, label):
        self.x_label = label

    def setYLabel(self, label):
        self.y_label = label

    def setYValues(self, values):
        """Nastavi y-ove hodnoty
        """
        self.y_values = values
        # vypocitame lave spodne rohy stlpcov
        self.x_values = [x + self.width / 2 for x in range(len(self.y_values))]
        # vypocitame stred stlpcov (znacky na x-ovej osi)
        self.x_ticks = [x + self.width for x in range(len(self.y_values))]

    def setXTickLabels(self, labels):
        """Nastavi popisky pre ciarky na x-ovej osi
        """
        self.x_ticksLabels = labels

    def makeGraph(self):
        self.fig = pylab.figure()
        self.fig.set_dpi(72)
        self.fig.set_figheight(3)
        self.fig.set_figwidth(5)

        self.fig.subplots_adjust(bottom=0.15)

        self.ax = self.fig.add_subplot(111)

        self.ax.bar(left=self.x_values, height=self.y_values,
width=self.width, color='#f00000')
        self.ax.set_xticks(self.x_ticks)
        self.ax.set_xticklabels(self.x_ticksLabels)

        self.ax.set_xlabel(self.x_label, **self.font)
        self.ax.set_ylabel(self.y_label, **self.font)
        self.ax.set_title(self.title, **self.font)

    def saveGraph(self, path):
        self.fig.savefig(path, format='png')

    def __del__(self):
        self.fig.delaxes(self.ax)
        del self.fig
        del self.ax

if __name__ == '__main__':
    import random
    import gc
    for i in range(5000):
        g = Graph()
        g.setTitle(u'Spotreba za rok 2008 podľa ATC skupín')
        g.setXLabel(u'ATC skupiny')
        g.setYLabel(u'Spotreba v EUR')
        g.setYValues([random.randint(0, x) for x in range(20)])
        g.setXTickLabels([chr(random.randint(65, 90)) for x in range(20)])
        g.makeGraph()
        g.saveGraph('grafy/' + str(i) + '.png')
        del g
        print gc.get_count()
        gc.collect()
        print gc.get_count()

When I used TkAgg, it crashes after 190 images (Fail to create pixmap
with Tk_GetPixmap in ImgPhotoInstanceSetSize. tried to delete photo
image when instances still exist).
When I switch to WX, it crashes after 400 images.
And Agg crashes when pagefile reaches 1.5x ram (cca 2000 images).

It is possible somehow free resources in matplotlib?

You need to close the figure. Try putting
pylab.close(self.fig) at the start of your __del__ method. In fact, I doubt you need to explicitly del anything in that method--you just need to close the figure so that pylab will release its references related to that figure.

There may be more things you need to change, but closing the figure is certainly going to be a big one.

Eric

···

I use python 2.5.4, wx 2.8.9.2, numpy 1.3.0b1 on win XP

Thanks for answers.

Stano.

------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Now it works like charm. Thank you for quick answer.

Stano.

···

On Fri, Mar 20, 2009 at 7:10 PM, Eric Firing <efiring@...202...> wrote:

Stano Paška wrote:

Hi,
I am using matplotlib first time and I am wonder how to free resources

I have this script

# coding: utf-8

import matplotlib
matplotlib.use('Agg')

import pylab

class Graph:

def __init__(self):
# sirka stlpcov
self.width = 0.5
self.font = {'fontname':'Tahoma', 'fontsize':10}

def setTitle(self, title):
"""Nastavi nadpis grafu
"""
self.title = title

def setXLabel(self, label):
self.x_label = label

def setYLabel(self, label):
self.y_label = label

def setYValues(self, values):
"""Nastavi y-ove hodnoty
"""
self.y_values = values
# vypocitame lave spodne rohy stlpcov
self.x_values = [x + self.width / 2 for x in
range(len(self.y_values))]
# vypocitame stred stlpcov (znacky na x-ovej osi)
self.x_ticks = [x + self.width for x in range(len(self.y_values))]

def setXTickLabels(self, labels):
"""Nastavi popisky pre ciarky na x-ovej osi
"""
self.x_ticksLabels = labels

def makeGraph(self):
self.fig = pylab.figure()
self.fig.set_dpi(72)
self.fig.set_figheight(3)
self.fig.set_figwidth(5)

   self\.fig\.subplots\_adjust\(bottom=0\.15\)

   self\.ax = self\.fig\.add\_subplot\(111\)

   self\.ax\.bar\(left=self\.x\_values, height=self\.y\_values,

width=self.width, color='#f00000')
self.ax.set_xticks(self.x_ticks)
self.ax.set_xticklabels(self.x_ticksLabels)

   self\.ax\.set\_xlabel\(self\.x\_label, \*\*self\.font\)
   self\.ax\.set\_ylabel\(self\.y\_label, \*\*self\.font\)
   self\.ax\.set\_title\(self\.title, \*\*self\.font\)

def saveGraph(self, path):
self.fig.savefig(path, format='png')

def __del__(self):
self.fig.delaxes(self.ax)
del self.fig
del self.ax

if __name__ == '__main__':
import random
import gc
for i in range(5000):
g = Graph()
g.setTitle(u'Spotreba za rok 2008 podľa ATC skupín')
g.setXLabel(u'ATC skupiny')
g.setYLabel(u'Spotreba v EUR')
g.setYValues([random.randint(0, x) for x in range(20)])
g.setXTickLabels([chr(random.randint(65, 90)) for x in range(20)])
g.makeGraph()
g.saveGraph('grafy/' + str(i) + '.png')
del g
print gc.get_count()
gc.collect()
print gc.get_count()

When I used TkAgg, it crashes after 190 images (Fail to create pixmap
with Tk_GetPixmap in ImgPhotoInstanceSetSize. tried to delete photo
image when instances still exist).
When I switch to WX, it crashes after 400 images.
And Agg crashes when pagefile reaches 1.5x ram (cca 2000 images).

It is possible somehow free resources in matplotlib?

You need to close the figure. Try putting
pylab.close(self.fig) at the start of your __del__ method. In fact, I doubt
you need to explicitly del anything in that method--you just need to close
the figure so that pylab will release its references related to that figure.

There may be more things you need to change, but closing the figure is
certainly going to be a big one.

Eric

I use python 2.5.4, wx 2.8.9.2, numpy 1.3.0b1 on win XP

Thanks for answers.

Stano.

------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users