我想從Arduino UNO的matplotlib模擬輸入中繪製實時講座。 我的問題:該圖不會顯示。只有當我停止運行代碼(Ctrl + C)時,它纔會顯示最後一個值的圖形。使用matplotlib實時繪製arduino數據時不顯示圖形圖
將「print pData」行添加到代碼以檢查值是否正確到達計算機時,它們在python終端上正確顯示(每秒顯示25個值數組)。
#!/usr/bin/python
from matplotlib import pyplot
import pyfirmata
from time import sleep
# Associate port and board with pyFirmata
port = '/dev/ttyACM0'
board = pyfirmata.Arduino(port)
# Using iterator thread to avoid buffer overflow
it = pyfirmata.util.Iterator(board)
it.start()
# Assign a role and variable to analog pin 0
a0 = board.get_pin('a:0:i')
pyplot.ion()
pData = [0.0] * 25
fig = pyplot.figure()
pyplot.title('Real-time Potentiometer reading')
ax1 = pyplot.axes()
l1, = pyplot.plot(pData)
pyplot.ylim([0, 1])
while True:
try:
sleep(1)
pData.append(float(a0.read()))
pyplot.ylim([0, 1])
del pData[0]
l1.set_xdata([i for i in xrange(25)])
l1.set_ydata(pData) # update the data
#print pData
pyplot.draw() # update the plot
except KeyboardInterrupt:
board.exit()
break
的可能的複製[交互式經由命令行與Python繪製](http://stackoverflow.com/questions/15991968/interactive-plotting-with-python-via-command-line) – tyleha
@tyleha你不'不需要show()'如果你使用'draw()' – Jason
@Jason @tyleha Jason是對的。使用'show()'不能解決問題。 – Paco