首页 > 解决方案 > 在 matplotlib 中包装绘图数据

问题描述

我正在为医疗压力传感器开发一个可视化工具,我需要将我的 x 轴固定在 0 到 20 秒之间,当 Y 数据“溢出”该值时,只需用新的值覆盖最后一个值,比如心电图可以。我目前可以绘制新数据,但是当我覆盖时旧值会留在那里。这是数据读取的第一次迭代的图: 在此处输入图像描述

正如你所看到的,当我开始绘制“新”数据时,旧值一直在阻碍,产生这种效果: 在此处输入图像描述

我只需要绘制更新的数据,同时保留旧曲线,但在新数据进入时覆盖,如下所示: 在此处输入图像描述

就像那个图像,但没有清除时间函数中的曲线。

这是我当前的代码:

  import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
import time
plt.style.use('ggplot')

lastmillis = int(round(time.time() * 1000))

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

class Plot:
    def __init__(self):
        self.line, = ax.plot([], [], lw=1)
        self.line.set_data([], [])
        self.x = np.linspace(0, 2, 1000)
        self.y = np.empty(shape=(1000,))
        self.state = 0
        return
    def animate(self,i):
        if i >= self.x.size:
            i = i % self.x.size
            if self.state == 1:
                self.state = 0
            elif self.state == 0:
                self.state = 1

        print(i,",",self.x.size)
        if self.state == 0:
            self.y[i] = np.sin(2 * np.pi * (0.01 * i))
        else:
            self.y[i] = np.cos(2 * np.pi * (0.01 * i))

        self.line.set_data(self.x, self.y)
        return self.line,

plot = Plot()
anim = FuncAnimation(fig, plot.animate,
                                interval=20, blit=True)

plt.show()

正确执行此操作的任何帮助或提示?先感谢您

标签: pythonmatplotlibplot

解决方案


推荐阅读