4

I've been using python extensively to extract data from various external pieces of equipment (ranging from arduinos to oscilloscopes), and I'm looking for a simplistic way to plot stuff.

There's already some answers to similar questions on stack overflow: What is the best real time plotting widget for wxPython?

With most pointing to this fine piece of code by Eli Bendersky http://eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis/

But the scope of the code is far more complicated that what I'm looking for. I'm looking for something rather minimalistic that just plots the data in real-time as it streams from a source -- it doesn't need a GUI, radio buttons, knobs and sliders, or anything like that.

It seems that solutions such as calling pylab.plot() or pylab.show() in a loop doesn't seem to give the correct behavior.

Does anyone have suggestions?

Community
  • 1
  • 1
nathan lachenmyer
  • 5,298
  • 8
  • 36
  • 57
  • Did you try the hints given here? [matplotlib animations](http://www.scipy.org/Cookbook/Matplotlib/Animations) – cha0site Feb 22 '12 at 19:51

3 Answers3

4

Well, this isn't a wxPython answer but I've used Chaco for this sort of thing and it's pretty straight forward. There is a nice example of a realtime spectrum analyzer that may be similar to your use case and a nice tutorial. So, if you aren't tied to wxPython for other reasons, that might be worth a look.

snim2
  • 4,004
  • 27
  • 44
1

To use real time plotting you need to send signals to the GUI loop. If you use interactive mode (Ipython) then you might also like to use threads.

I have written some decorators to handle the GUI and threading in a really easy and clean way. They work for the QT backend. https://gist.github.com/Vrekrer/106c49a3ae6d420937aa

A sample code for Ipython will look like this

#%pylab qt

#https://gist.github.com/Vrekrer/106c49a3ae6d420937aa    
import QThreadDecorators
import time

@QThreadDecorators.GUI_Safe
def myplot(x,y):
    #This will plot a new line for each call (ok for an example)
    plot(x, y, 'bo-')
    grid(True)


@QThreadDecorators.AsQThread
def myLoop(x):
    y = x * nan
    for i, xi in enumerate(x):
        #get some data
        time.sleep(1)
        y[i] = xi**2
        #plot in real time
        myplot(x,y)

#just call the function and it will run on a thread
myLoop( arange(20) )
Vrekrer
  • 176
  • 1
  • 7
1

Besides the matplotlib examples you've found, there's also wx.lib.plot and several answers here: http://wxpython-users.1045709.n5.nabble.com/real-time-data-plots-td2344816.html

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88