我的代码的目的是导入串行数据,以便在实时数据上进行逻辑回归。目前,我所有的数据都被过滤掉,而不仅仅是y<0所在的点(x,y)。我正在学习python,这是我的第一个项目,Yaa:
import serial
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from scipy import stats
#initialize serial port
ser = serial.Serial()
ser.port = '/dev/cu.usbmodem14101' #Arduino serial port
ser.baudrate = 9600
ser.timeout = 10 #specify timeout when using readline()
ser.open()
if ser.is_open==True:
print("\nAll right, serial port now open. Configuration:\n")
print(ser, "\n") #print serial parameters
fig1 = plt.figure(figsize=(10,7))
ax1 = fig1.add_subplot(2, 1, 1)
xs = [] #store trials here (n)
ys = [] #store relative frequency here
# This function is called periodically from FuncAnimation
def animate(i, xs, ys):
#Acquire and parse data from serial port
line=ser.readline() #ascii
line_as_list = line.split(b',')
i = float(line_as_list[0])
Y = line_as_list[1]
Y_as_list = Y.split(b'\n')
Y_float = float(Y_as_list[0])
cc=str(ser.readline())
print(cc[2:][:-5])
xs.append(i)
ys.append(Y_float)
K = 0
def filter_where(xs, K):
return xs[np.where(ys > K)]
print(xs)
print(ys)
def filter_where(ys, K):
return ys[np.where(ys > K)]
print(ys)
#adding regression - commented out b/c it generates error that it is empty
#slope, intercept, r, p, std_err = stats.linregress(xs, ys)
#def myfunc(xs):
# return slope * xs + intercept
#mymodel = list(map(myfunc, xs))
plt.scatter(xs, ys)
#plt.plot(xs, mymodel)
# Draw x and y lists
ax1.clear()
ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig1, animate, fargs=(xs, ys), interval=10)
plt.show()
编辑:我更改了代码。我把过滤器移出了动画部分。我把新的i和y分别添加到xs和ys中。我把它打印出来,这样你就可以看到它正在获取信息。
这是样本输出
All right, serial port now open. Configuration:
Serial<id=0x109132c50, open=True>(port='/dev/cu.usbmodem14101', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=10, xonxoff=False, rtscts=False, dsrdtr=False)
[]
[]
[]
0.30,0.011
0.70,0.011
1.11,0.013
1.51,0.005
1.91,0.006
2.31,0.012
2.71,0.004
3.12,0.005
3.52,0.004
3.92,0.014
4.32,0.005
4.72,-0.001
5.13,0.008
5.53,0.009
5.93,0.010
6.33,0.013
6.73,0.000
7.13,0.004
发布于 2022-02-12 19:20:50
我无法得到Tempman更简洁的解决方案来工作,但我得到了同样的想法。正如您所建议的,我不需要过滤同桌中的所有xs和ys,我甚至不需要首先定义过滤器。我的串行数据中有两个变量作为一对(x,y)工作。如果y是无效数据,那么x也是。我在解析序列之后,在将其附加到其他数据之前,对其进行过滤。
#Acquire and parse data from serial port
line=ser.readline() #ascii
line_as_list = line.split(b',')
i = float(line_as_list[0])
Y = line_as_list[1]
Y_as_list = Y.split(b'\n')
Y_float = float(Y_as_list[0])
if Y_float>0:
xs.append(i)
ys.append(Y_float)
https://stackoverflow.com/questions/71074624
复制相似问题