Python:实时绘制Y轴问题

时间:2018-12-20 20:44:35

标签: python matplotlib serial-port real-time graphing

我目前正在尝试从传感器实时绘制一些数据。不幸的是,无论我使用多少种不同的方法,都是plot(x,y),动画还是drawow。我经常遇到这个错误。

由于某种原因,我似乎无法创建固定的Y轴。我希望它是介于0到5之间的一个设置范围。但是,每当我开始记录数据时,它读取的第一个数据点都将成为原点(左下),并且当我减小该值时,图形将沿正Y方向移动。

任何帮助将不胜感激。

我正在通过串行端口读取数据。

Problem as seen

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []
xval =0

# Initialize communication with Photodiode
ports = list(serial.tools.list_ports.comports())
print ('Available Ports:\n')
for p in ports:
    print (p)

Serialport= input("\n Type in the port associate with Arduino\n(For example, 
                  Type in 'COM8' without the '): \n")
ser = serial.Serial(Serialport, 9600)


# This function is called periodically from FuncAnimation
def animate(i, xs, ys):
    global xval
    # Read data from serial port and convert to list.
    reading = ser.readline().decode()
    Incominglist = str(reading).split(",")

    # Add x and y to lists
    ys.extend(Incominglist)

   for val in range(len(Incominglist)):
       if  xval == 0 and val ==0:
           xs.append(xval)  # Beginning X Value to 0
       else:
           xval += 0.0001
           xs.append(xval) #increases X by increment 0.0001

    # Draw x and y lists
    ax.clear()
    ax.plot(xs, ys)

    # Format plot
    plt.xticks(rotation=45, ha='right')
    plt.subplots_adjust(bottom=0.30)


# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=100)
plt.show()

0 个答案:

没有答案