我試圖遵循一些導遊的生成實時圖表,如:real-time plotting in while loop with matplotlib和http://thread.gmane.org/gmane.comp.python.matplotlib.general/35705實時情節與Python 3.x的安裝
但是,我相信,示例代碼與蟒蛇編譯2.7。當我嘗試編譯我的時候,我沒有看到實時情節正在更新。這是因爲python 3不支持它嗎?或者我錯過了一個圖書館或什麼?只有當我停止while循環時,我才能看到最後繪製的值。我使用Rodeo作爲我的IDE;這是否會阻止我查看實時情節?
import serial
import numpy as np
import matplotlib.pyplot as plt
def plotlive():
plt.plot(ard_dat,'o')
plt.xlabel('count', fontsize=12)
plt.ylabel('reading', fontsize=12)
plt.legend(loc='upper right')
ard_dat=[]
plt.ion()
cnt=0
arduinoSerialData = serial.Serial('com5',9600)
while True:
while (arduinoSerialData.inWaiting()==0):
pass
srdata = arduinoSerialData.readline()
try:
intstrdata = int(srdata)
except ValueError:
pass
ard_dat.append(intstrdata)
drawnow(plotlive)
plt.pause(.00001)
cnt+=1
if (cnt>50):
ard_dat.pop(0)
很好,謝謝你的信息。我是jupyter的新手,所以必須做一些閱讀和試驗。我不熟悉後端交互模式的這個概念。如果你知道任何開始的資源,請讓我知道。 –
我嘗試在頂部添加%matplotlib筆記本,並在jupyter筆記本中運行相同的代碼,但沒有執行任何操作。我能夠輸出從arduino提取的40個值的圖,但是,所以繪圖似乎工作(如果它是一個靜態列表),我的代碼中的東西不是? –
對不起,雖然我確實說過從腳本執行時代碼可能會運行,但我不確定它會在Jupyter中運行。我說的是一般動畫可以在Jupyter中運行。作爲第一步,將您的代碼作爲腳本運行,看看它是否有效。如果你還想能夠在jupyter中運行它,可能需要對它進行調整。 – ImportanceOfBeingErnest