2012-02-22 89 views
4

我一直在使用python從各種外部設備(從arduinos到示波器)提取數據,我正在尋找一種簡單的方式來繪製東西。Python中的極簡實時繪圖

有已經有一些答案在堆棧溢出類似的問題:由Eli Bendersky http://eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis/

但代碼的範圍 What is the best real time plotting widget for wxPython?

對於大多數指向這一優良的一段代碼更爲複雜,我在找什麼。我正在尋找一種相當簡約的東西,它只是從源頭上實時繪製數據 - 它不需要GUI,單選按鈕,旋鈕和滑塊等。

似乎解決方案,如在循環中調用pylab.plot()或pylab.show()似乎並沒有給出正確的行爲。

有沒有人有建議?

+0

你嘗試這裏給出的提示? [matplotlib動畫](http://www.scipy.org/Cookbook/Matplotlib/Animations) – cha0site 2012-02-22 19:51:14

回答

4

嗯,這不是一個wxPython的答案,但我已經使用Chaco這種事情,它非常簡單。有一個realtime spectrum analyzer的很好的例子,可能類似於你的用例和一個不錯的tutorial。所以,如果你不是因爲其他原因而與wxPython綁定的話,那可能值得一看。

+0

謝謝!查科看起來非常有用,並且意味着我將要使用它的某些事情。我會研究它:) – 2012-02-22 22:59:05

+0

僅供參考,教程鏈接截至2015年2月8日 – dcoz 2015-02-08 18:54:24

+0

謝謝,現在修復。 – snim2 2015-02-08 19:14:20

1

要使用實時繪製你需要將信號發送到GUI循環。如果您使用交互模式(Ipython),那麼您可能還想使用線程。

我已經寫了一些裝飾器來處理GUI和線程以一種非常簡單和乾淨的方式。他們爲QT後端工作。 https://gist.github.com/Vrekrer/106c49a3ae6d420937aa

爲IPython的一個示例代碼看起來像這樣

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