WX dynamic plot slowly fills memory

I am trying to make a dynamic plot to monitor sensor data, slowly over time.
It will be updated about once a second.

When I leave my app running it slowly fills up memory. I am new to OO
programming, python and Matplotlib so the chances of me doing something
wrong are huge!

I'm using WXpython and the OO api of matplotlib. My code is below, however
I have the same problem running dynamic_demo_wx.py from the examples(I used
this as my base). I am guessing that every so often I need to clear the
plot data, but am unsure how to do this. If I remove, the 3 lines that
actually set the data, draw the plot and repaint the GUI then the program
has no memory issues.

Any help on this would be greatly appreciated, as everything else works.
This monitor program will be running for days, right now it last only a few
hours before it has claimed most of the system memory.

#!/usr/bin/env python
import time, sys, os
import numpy
import matplotlib
matplotlib.use('WX')
from matplotlib.backends.backend_wx import
FigureCanvasWx,FigureManagerWx,NavigationToolbar2Wx
from matplotlib.figure import Figure
from matplotlib.axes import Subplot
import wx

TIMER_ID = wx.NewId()
class PlotFigure(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Pyro Logger")
        self.fig = Figure((12,3), 75)
        self.canvas = FigureCanvasWx(self, -1, self.fig)
        self.toolbar = NavigationToolbar2Wx(self.canvas)
        self.toolbar.Realize()
        self.figmgr = FigureManagerWx(self.canvas, 1, self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, 1, wx.LEFT|wx.TOP|wx.GROW)
        sizer.Add(self.toolbar, 0, wx.GROW)
        self.SetSizer(sizer)
        self.Fit()
        wx.EVT_TIMER(self, TIMER_ID, self.onTimer)
    def GetToolBar(self):
        return self.toolbar
    def init_plot_data(self):
        self.xlim = 100
        self.ylim = 100
        a =
self.fig.add_subplot(111,xlim=(0,self.xlim),ylim=(0,self.ylim),autoscale_on=False)
        self.x = numpy.array([0])
        self.y = numpy.array([0])
        self.lines = a.plot(self.x,self.y,'-')
        self.count = 0
    def onTimer(self, evt):
        if self.count <= self.xlim:
            self.count = self.count + 1
        if self.count <= self.xlim:
            self.x = numpy.append(self.x,self.count)
        if self.count > self.xlim:
            self.y = self.y[1:self.xlim + 1]
        #Simulating with random Data for now
        self.y=numpy.append(self.y,((numpy.random.random()*50)+25))
        #Problem seems to come in here
        self.lines[0].set_data(self.x,self.y)
        self.canvas.draw()
        self.canvas.gui_repaint()
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = PlotFigure()
    frame.init_plot_data()
    t = wx.Timer(frame, TIMER_ID)
    t.Start(100)
    frame.Show()
    app.MainLoop()

···

--
View this message in context: http://www.nabble.com/WX-dynamic-plot-slowly-fills-memory-tf3590828.html#a10035537
Sent from the matplotlib - users mailing list archive at Nabble.com.

ednspace wrote:

I'm using WXpython and the OO api of matplotlib.

Have you tried wxAgg? if nothing else, it should look better. It would be interesting to see if it behaves differently as far as memory is concerned.

Also, be sure to post your versions and platform.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

Christopher Barker wrote:

ednspace wrote:

I'm using WXpython and the OO api of matplotlib.

Have you tried wxAgg? if nothing else, it should look better. It would
be interesting to see if it behaves differently as far as memory is
concerned.

Also, be sure to post your versions and platform.

switched matplotlib.use('WX') matplotlib.use('WXAgg')
not sure if I need to change anything else in my code to use WXAgg
everything looked the same to me, memory just keeps creeping up.

is there something that needs to be done to clear the:
self.lines[0].set_data(self.x,self.y)

That I am setting with:
self.lines = a.plot(self.x,self.y,'-')

I am confused about what exactly self.lines ends up being, I mean
self.lines[0] makes it seem like an array, however the assignment of the
a.plot does not seem like one.

thanks for the clues
Versions
Python 2.4.3
2.6.15-28-386 (Ubuntu Linux)
Matplotlib 0.82
Numpy 1.0

···

--
View this message in context: http://www.nabble.com/WX-dynamic-plot-slowly-fills-memory-tf3590828.html#a10058901
Sent from the matplotlib - users mailing list archive at Nabble.com.