首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在matplotlib动画中更新串口的值?

如何在matplotlib动画中更新串口的值?
EN

Stack Overflow用户
提问于 2013-12-14 01:47:25
回答 1查看 2.1K关注 0票数 0

我一直在尝试使用matplotlib的动画功能实时绘制来自arduino的串行数据。数据来自ntc温度传感器。我能够得到的图总是显示一条单线,并且这条线只随着温度的变化而向上或向下平移。我想知道我可以做些什么来查看表示图中变化的曲线。下面是代码:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2013-12-14 04:28:09

您正在将y-data设置为单个点(0,y)。你想要做一些类似的事情:

代码语言:javascript
复制
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,
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20572815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档