您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python实时绘图

Python实时绘图

您可能拥有的最轻巧的解决方案是替换现有图的X和Y值。(如果您的X数据不变,则仅显示Y值。一个简单的示例:

import matplotlib.pyplot as plt
import numpy as np
import time

fig = plt.figure()
ax = fig.add_subplot(111)

# some X and Y data
x = np.arange(10000)
y = np.random.randn(10000)

li, = ax.plot(x, y)

# draw and show it
ax.relim() 
ax.autoscale_view(True,True,True)
fig.canvas.draw()
plt.show(block=False)

# loop to update the data
while True:
    try:
        y[:-10] = y[10:]
        y[-10:] = np.random.randn(10)

        # set the new data
        li.set_ydata(y)

        fig.canvas.draw()

        time.sleep(0.01)
    except KeyboardInterrupt:
        break

这个解决方案也非常快。上面代码的最大速度是每秒100次重绘(受限制time.sleep),我得到70-80左右,这意味着一次重绘大约需要4毫秒。但是YMMV取决于后端等。

python 2022/1/1 18:47:31 有336人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶