我一直在尝试使用matplotlib的动画功能实时绘制来自arduino的串行数据。数据来自ntc温度传感器。我能够得到的图总是显示一条单线,并且这条线只随着温度的变化而向上或向下平移。我想知道我可以做些什么来查看表示图中变化的曲线。下面是代码:
import serial
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
arduino = serial.Serial('COM3', 9600)
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(10, 40))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 10, 1000)
y = arduino.readline()
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=False)
plt.show()发布于 2013-12-14 04:28:09
您正在将y-data设置为单个点(0,y)。你想要做一些类似的事情:
max_points = 50
# fill initial artist with nans (so nothing draws)
line, = ax.plot(np.arange(max_points),
np.ones(max_points, dtype=np.float)*np.nan,
lw=2)
def init():
return line,
def animate(i):
y = arduino.readline() # I assume this
old_y = line.get_ydata() # grab current data
new_y = np.r_[old_y[1:], y] # stick new data on end of old data
line.set_ydata(new_y) # set the new ydata
return line,https://stackoverflow.com/questions/20572815
复制相似问题