【发布时间】:2016-04-03 20:43:14
【问题描述】:
我正在尝试使用 matplotlib 从 Arduino UNO 的模拟输入绘制实时讲座。 我的问题:图表不会显示。只有当我停止运行代码(Ctrl+C)时,它才会显示最后一个值的图表。
在代码中添加“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
【问题讨论】:
-
@tyleha 如果您使用的是
draw(),则不需要show() -
@Jason @tyleha Jason 是对的。使用
show()并不能解决问题。
标签: python matplotlib arduino