【问题标题】:Update a chart in realtime with matplotlib使用 matplotlib 实时更新图表
【发布时间】:2023-02-13 19:14:33
【问题描述】:

我想通过实时重新绘制一条新曲线(有 100 个点)来更新绘图。

这有效:

import time, matplotlib.pyplot as plt, numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
t0 = time.time()
for i in range(10000000):
    x = np.random.random(100)
    ax.clear()
    ax.plot(x, color='b')
    fig.show()
    plt.pause(0.01)
    print(i, i/(time.time()-t0))

但只有 ~10 FPS,这看起来很慢。

在 Matplotlib 中执行此操作的标准方法是什么?

我已经读过 How to update a plot in matplotlibHow do I plot in real-time in a while loop using matplotlib? 但这些案例不同,因为它们向现有地块添加新点.在我的用例中,我需要重新绘制所有内容并保留 100 分。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    我不知道获得数量级的任何技术。不过你可以稍微增加FPS

    1. 更新线数据而不是使用set_ydata(和/或set_xdata)创建新图
    2. 使用Figure.canvas.draw_idle()代替Figure.canvas.draw()(参见this question)。

      因此,我建议您尝试以下操作:

      import time
      import matplotlib.pyplot as plt
      import numpy as np
      
      fig = plt.figure()
      ax = fig.add_subplot(111)
      t0 = time.time()
      
      x = np.random.random(100)
      l, *_ = ax.plot(x, color='b')
      fig.show()
      fig.canvas.flush_events()
      ax.set_autoscale_on(False)
      for i in range(10000000):
          x = np.random.random(100)
          l.set_ydata(x)
          fig.canvas.draw_idle()
          fig.canvas.flush_events()
          print(i, i/(time.time()-t0))
      

      我希望这有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多