Hello,
I want to use imshow to make a real time waterfall plot. The attached
code is the core of my application, and it works, but it is quite
slow, around 200ms to update the plot. Is there a way to accelerate
this? I have seen the blitting demos, and they work well for the line
plots, but I could not figure out how to extend this functionality to
imshow plots. In theory I could throw all of my data into a bitmap
(since I don't really need the antialiasing etc provided by AGG) and
display that directly in WX, but the trouble with that is I want to
display the spectra at a given time and the waterfall plot with the
same X axis, thus computing the area to display the bitmap seems
tricky. Is there any built in MPL functionality for doing that?
One question about the blitting demos: they do not seem to work well
with resizing the window (which make sense because the blit buffer
never gets informed of the window size change). Has anyone modified
this code to allow it to work with resizing? I tried to connect to the
RESIZE event and recapture the base canvas, but did not have any luck.
Thanks,
Glenn
import  wx
import  wx.lib.newevent

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import Toolbar, FigureCanvasWxAgg
from matplotlib.figure import Figure

import pylab as p
import numpy
import os
import gc

import socket, time, string

if os.name == 'posix':
    _time = time.time
else:
    time = time.clock
import gui

class SpecFrame(gui.MainFrame):
    def __init__(self, *args, **kwds):
        gui.MainFrame.__init__(self, *args, **kwds)
        self.PlotInit()
        self.UpdateInterval = 0.5
        self.spec_canvas.SetMinSize((50,50))
        self.time_canvas.SetMinSize((50,50))
        self.var_canvas.SetMinSize((50,50))        
        self.Bind(wx.EVT_TIMER, self.NewSpec)
        self.t1 = None
        
        
    def OnStart(self,event):
        self.avg_count = 0
        self.avg = numpy.zeros(1024)
        self.wfall = numpy.zeros((256,1024))
        self.ch = 100
        self.timeseries = numpy.zeros(256)
        self.start_time = _time()
        self.LastUpdateTime = _time()
        self.t1 = wx.Timer(self)
        self.t1.Start(self.UpdateInterval*100)
    def OnStop(self, event):
        if self.t1:
            self.t1.Stop()
            
 
    def NewSpec(self,event):
        spp = _time()
        self.Status.SetStatusText("%.3f  %d %d" % ((spp-self.start_time)*1000, self.avg_count, len(gc.get_objects())),1)
        self.start_time = spp
        s = numpy.random.random((1024, ))
        self.wfall[1:,:] = self.wfall[:-1,:]
        self.wfall[0,:] = self.avg
        self.timeseries[1:] = self.timeseries[:-1]
        self.timeseries[0] = s[self.ch]
        if self.avg_count == 1:
            if spp - self.LastUpdateTime > self.UpdateInterval:
                #self.plots[2].clear()
                a = _time()
                self.water_plot.imshow(numpy.log10(self.wfall+1).clip(0,5),aspect = 'auto')
                #self.time_plot.plot(self.timeseries)
                ##self.timeplot.set_ydata(self.timeseries)
                ##self.plots[1].axes.set_ylim(self.timeseries.min(),self.timeseries.max())
                #self.spec_plot.clear()
                #self.spec_plot.plot(numpy.log10(self.avg+1))
                #self.spec_plot.plot([self.ch],[numpy.log10(self.avg[self.ch]+1)],'ro')
                ##self.canvas.restore_region(self.bkg)
                ##self.specplot.set_ydata(numpy.log10(self.avg+1))
                #self.spec_plot.axes.set_xlim(0,1023)
                ##self.plots[0].draw_artist(self.line)
                #a = _time()
                self.spec_canvas.draw()
                #self.time_canvas.draw()
#                try:
#                    self.queue.put(self.default_task,False)
#                except:
#                    self.queue.get(False)
#                    print "Plot Dropped"
                print "%f %f" % (_time() - a, _time())
                #self.canvas.blit(self.plots[0].bbox)
                self.LastUpdateTime = _time()
            self.avg_count = 0
            self.avg = s
        else:
            self.avg += s
        self.avg_count += 1

    def PlotInit(self):
        self.spec_plot = self.spec_fig.add_axes([.1,.55,.8,.4])
        self.spec_plot.grid(True)
        #self.spec_plot.hold(False)
        self.water_plot = self.spec_fig.add_axes([.1,.05,.8,.5],sharex = self.spec_plot)
        self.water_plot.grid(True)
        self.water_plot.hold(False)
        self.time_plot = self.time_fig.add_subplot(1,1,1)
        self.time_plot.grid(True)
        self.time_plot.hold(False)
        self.var_plot = self.var_fig.add_subplot(1,1,1)
        self.var_plot.grid(True)
        self.var_plot.hold(False)
        self.spec_canvas.draw()
    def Plot(self, n, *args, **kwds):
        #self.plots[n].clear()
        self.plots[n].plot(*args, **kwds)
        self.plots[n].grid(True)
        #self.canvas.draw()
        
        
if __name__ == "__main__":
    app = wx.App(0)
    wx.InitAllImageHandlers()
    frame_1 = SpecFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# generated by wxGlade 0.6.1 on Wed Mar 26 16:00:56 2008

import wx
import matplotlib
#matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import Toolbar, FigureCanvasWxAgg
from matplotlib.figure import Figure

# begin wxGlade: extracode
# end wxGlade



class MainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        self.spec_fig = Figure()
        self.time_fig = Figure()
        self.var_fig = Figure()
        # begin wxGlade: MainFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.Status = self.CreateStatusBar(2, 0)
        self.Start = wx.Button(self, -1, "Start")
        self.Stop = wx.Button(self, -1, "Stop")
        self.Save = wx.Button(self, -1, "Save")
        self.label_1 = wx.StaticText(self, -1, "LO", style=wx.ALIGN_RIGHT)
        self.TextLO = wx.TextCtrl(self, -1, "")
        self.SpinLO = wx.SpinButton(self, -1 , style=wx.SP_ARROW_KEYS)
        self.SetLO = wx.Button(self, -1, "Set LO")
        self.spec_canvas = FigureCanvasWxAgg(self, -1, self.spec_fig)
        self.time_canvas = FigureCanvasWxAgg(self, -1, self.time_fig)
        self.var_canvas = FigureCanvasWxAgg(self, -1, self.var_fig)

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.OnStart, self.Start)
        self.Bind(wx.EVT_BUTTON, self.OnStop, self.Stop)
        self.Bind(wx.EVT_BUTTON, self.OnSave, self.Save)
        self.Bind(wx.EVT_SPIN, self.OnSpinLO, self.SpinLO)
        self.Bind(wx.EVT_BUTTON, self.OnSetLO, self.SetLO)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MainFrame.__set_properties
        self.SetTitle("UDPSpec")
        self.SetSize((640, 480))
        self.Status.SetStatusWidths([-1, -1])
        # statusbar fields
        Status_fields = ["", ""]
        for i in range(len(Status_fields)):
            self.Status.SetStatusText(Status_fields[i], i)
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MainFrame.__do_layout
        topSizer = wx.BoxSizer(wx.VERTICAL)
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
        ControlSizer = wx.GridSizer(2, 3, 4, 2)
        sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
        ButtonSizer = wx.BoxSizer(wx.HORIZONTAL)
        ButtonSizer.Add(self.Start, 1, wx.EXPAND, 0)
        ButtonSizer.Add(self.Stop, 1, wx.EXPAND, 1)
        ButtonSizer.Add(self.Save, 1, wx.EXPAND, 1)
        topSizer.Add(ButtonSizer, 0, wx.EXPAND, 0)
        ControlSizer.Add(self.label_1, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
        ControlSizer.Add(self.TextLO, 0, wx.ALL|wx.EXPAND, 2)
        sizer_5.Add(self.SpinLO, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        sizer_5.Add(self.SetLO, 1, wx.ALIGN_CENTER_VERTICAL, 0)
        ControlSizer.Add(sizer_5, 1, wx.EXPAND, 0)
        sizer_4.Add(ControlSizer, 0, 0, 0)
        sizer_4.Add((20, 20), 0, 0, 0)
        topSizer.Add(sizer_4, 0, wx.EXPAND, 0)
        sizer_1.Add(self.spec_canvas, 1, wx.EXPAND, 0)
        sizer_2.Add(self.time_canvas, 1, wx.EXPAND, 0)
        sizer_2.Add(self.var_canvas, 1, wx.EXPAND, 0)
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        topSizer.Add(sizer_1, 1, wx.EXPAND, 0)
        self.SetSizer(topSizer)
        self.Layout()
        # end wxGlade

    def OnStart(self, event): # wxGlade: MainFrame.<event_handler>
        print "Event handler `OnStart' not implemented"
        event.Skip()

    def OnStop(self, event): # wxGlade: MainFrame.<event_handler>
        print "Event handler `OnStop' not implemented"
        event.Skip()

    def OnSpinLO(self, event): # wxGlade: MainFrame.<event_handler>
        print "Event handler `OnSpinLO' not implemented"
        event.Skip()

    def OnSetLO(self, event): # wxGlade: MainFrame.<event_handler>
        print "Event handler `OnSetLO' not implemented"
        event.Skip()

    def OnSave(self, event): # wxGlade: MainFrame.<event_handler>
        print "Event handler `OnSave' not implemented"
        event.Skip()

# end of class MainFrame

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to