我是python的新手,我试图通过连接到树莓派3的串行传感器绘制实时数据图。我一直在尝试将x轴更改为时间轴,但是我一直在获取字符串浮动错误。我如何修改代码以获取实时图,该图每秒读取数据并不断更新,并且x轴显示读取点的时间。
这是我到目前为止所拥有的,
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import serial
import platform
import time
import datetime
import csv
import re
print("Python version: " + platform.python_version())
print("matplotlib version: " + mpl.__version__)
fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10))
ax.set_ylim(-.1, .08)
xdata, ydata = [0]*100, [0]*100
SerialIn = serial.Serial("/dev/ttyUSB0",115200)
FILE = open('Pressure ' + time.strftime("%Y-%m-%d %H:%M") + '.csv','a')
pattern = re.compile( r'([0-9 .+-]*)' ) #Added
def update(data):
line.set_ydata(data)
return line,
def run(data):
global xdata, ydata
x,y = data
if (x == 0):
xdata = [0]*100
ydata = [0]*100
del xdata[0]
del ydata[0]
xdata.append(x)
ydata.append(y)
line.set_data(xdata, ydata)
return line,
def data_gen():
x = 10
while True:
if (x >= 9):
x = 0
else:
x += 0.1
SerialIn.write(b"P\r")
try:
inRaw = re.search( pattern,
SerialIn.read(SerialIn.in_waiting).decode('utf-8') ).group(0)
inInt = float(inRaw)
TIME = time.strftime('%H:%M:%S')
FILE.write("{0},{1}\r".format(TIME, inInt))
FILE.flush()
print('['+time.strftime('%H:%M:%S')+']',inInt)
time.sleep(1-time.time()%1)
except:
inInt = 0
yield x, inInt
ani = animation.FuncAnimation(fig, run, data_gen, interval=0, blit=True)
plt.show()
SerialIn.close()
FILE.close()