简体   繁体   中英

Minimalistic Real-Time Plotting in Python

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?

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.

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) )

除了你发现的matplotlib例子之外,还有wx.lib.plot和几个答案: http ://wxpython-users.1045709.n5.nabble.com/real-time-data-plots-td2344816.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM