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.


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to