我想从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