python - 在 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()
正确执行此操作的任何帮助或提示?先感谢您
解决方案
推荐阅读
- jupyter-lab - 在 Jupyter Lab 中显示 vega 规范
- ios - Flutter Firebase google_sign_in 此连接在 IOS 上不是私有的,然后在 Google 主页上重定向
- php - 在另一个数组中捕获数组
- azure-notificationhub - 任何 API 都可以像 Microsoft.Azure.NotificationHubs 一样工作并且可以在 .NETFramework 4.5.2 项目中使用?
- javascript - CSS滚动顶部溢出
- reactjs - 我无法在 typescript 中使用 useRef 钩子正确输入 ref
- php - 检查日期在 PHP 中是 12 小时格式还是 24 小时格式?
- javascript - js 不执行外部函数从数据库接收数据
- c++ - GCC:如何自定义编译产生的警告和错误
- google-sheets - 谷歌金融1分钟数据条怎么做?