在Python中定义*def animation[i]*使用ADC serial Arduin进行实时绘图

2024-03-29 11:36:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用matplotlib的def animation[I]实时绘制,但我也有一些问题需要认识到。你知道吗

我试过正常的情节,但它会变慢,因为数组变大。我正在尝试这个网站:https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ 但是我真的不明白如何用串行输入编写def animate[i]。抱歉,对Python还是很陌生的

经典Arduino AnalogreadInput,无延迟串行输出

val = analogRead(A0);
Serial.println(val); 

Python序列化并实时打印

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import serial
import time

fig = plt.figure()
ax = plt.axes(xlim=(0, 25), ylim=(1, 4))
line, = ax.plot([], [], lw=2)

ser = serial.Serial('COM5', 4800)
val = ser.readline().decode()

def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
def animate(i):

    millis = int(round(time.time() * 1000))
    x = str(time.ctime())

    try:
        y = float(val)
    except ValueError:
        ser.readline()

    line.set_data(millis, y)
    return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=20, blit=True)

plt.show()

希望有人能帮我:)


Tags: importtimematplotlibinitdefaslineserial