Python 在matplotlib中换行打印数据

Python 在matplotlib中换行打印数据,python,matplotlib,plot,Python,Matplotlib,Plot,我正在为医用压力传感器开发一个可视化工具,我需要将x轴固定在0到20秒之间,当Y数据“溢出”该值时,只需用新值覆盖上一个值,就像心电图图一样。我目前可以绘制新数据,但覆盖时旧值仍保留在那里。 这是数据读取的第一次迭代图: 正如您所看到的,当我开始绘制“新”数据时,旧值会一直阻碍我,产生以下效果: 我只需要绘制更新的数据,同时保留旧曲线,但在新数据进入时覆盖,如下所示: 就像那张图像一样,但没有清除时间函数的曲线 这是我当前的代码: import numpy as np from mat

我正在为医用压力传感器开发一个可视化工具,我需要将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()
有没有关于正确执行此操作的帮助或提示?
提前谢谢你

@anon谢谢,那是我的问题,没有意识到我是帧号,并且不断增加,我添加了一个检查,现在它工作了。谢谢大家!@另外,谢谢,这是我的问题,没有意识到我是帧编号,并不断增加,我添加了一个检查,现在它的工作。非常感谢。