我目前正在开展一个项目,该项目涉及获取模拟读数并将其实时映射到图表上。因此,为了完成此任务,我通过 Arduino 模拟端口运行光敏电阻,并通过 python 3.4.3 读取该数据。在Python方面,我安装了maplotlib和drawow。如下所示的代码将绘制电阻器将读取的第一个数据标记,但不会实时更新它。但是,如果我更改电阻然后重新启动程序,它将不断绘制新值。我想要它做的是当我改变光敏电阻的值时改变图表上的值。
import serial # import from pySerial
import numpy # import library from Numerical python
import matplotlib.pyplot as plt # import Library from matplotlib
from drawnow import drawnow # import lib from drawnow
ConF = [] # create an empty array for graphing
ArduinoData = serial.Serial('com3',9600) # set up serial connection with arduino
plt.ion() # tell matplotlib you want interactive mode to plot data
cnt = 0
def makeFig(): # creat a function to make plot
plt.plot(ConF, 'go-')
while True: # loop that lasts forever
while (ArduinoData.inWaiting()==0): # wait till there is data to plot
pass # do nothing
arduinoString = ArduinoData.readline()
dataArray = arduinoString
Con = float(arduinoString) # turn string into numbers
ConF.append(Con) # addinf to the array.
drawnow(makeFig) # call draw now to update
plt.pause(.000001)
cnt=cnt+1
if(cnt>50):
ConF.pop(0)
我不确定我的错误在哪里,没有错误消息......它只是一遍又一遍地绘制相同的数据点。任何帮助将是非常受欢迎的。
就像是:
fig, ax = plt.subplots()
ln, = ax.plot([], [], 'go-')
while True:
x, y = get_new_data()
X, Y = ln.get_xdata(), ln.get_ydata()
ln.set_data(np.r_[X, x], np.r_[Y, y])
fig.canvas.draw()
fig.canvas.flush_events()
应该可以解决问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)