您可能拥有的最轻巧的解决方案是替换现有图的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取决于后端等。