我正在尝试在 spyder 中绘制一个在循环内更新的图,我希望能够在绘制之前等待一段时间。
作为示例,我正在使用以下代码,它在没有 time.sleep() 的情况下运行良好,但在使用睡眠功能时开始变慢。有替代方案吗?
import numpy as np
import matplotlib.pyplot as plt
import time
fig, ax = plt.subplots()
ax.axis([0, 100, 0, 1])
ax.set_title('Testing')
y = np.random.rand(100)
lines = ax.plot(y, 'k.')
fig.canvas.manager.show()
i=0
while 1:
i=i+1
y = np.random.rand(100)
lines[0].set_ydata(y)
fig.canvas.draw()
fig.canvas.flush_events()
time.sleep(1)