您希望该pause
功能使gui框架有机会重新绘制屏幕:
import pylab
import time
import random
import matplotlib.pyplot as plt
dat=[0,1]
fig = plt.figure()
ax = fig.add_subplot(111)
Ln, = ax.plot(dat)
ax.set_xlim([0,20])
plt.ion()
plt.show()
for i in range (18):
dat.append(random.uniform(0,1))
Ln.set_ydata(dat)
Ln.set_xdata(range(len(dat)))
plt.pause(1)
print 'done with loop'
您无需Line2D
每次都创建一个新对象,只需更新现有对象中的数据即可。
说明文件:
pause(interval)
Pause for *interval* seconds.
If there is an active figure it will be updated and displayed,
and the gui event loop will run during the pause.
If there is no active figure, or if a non-interactive backend
is in use, this executes time.sleep(interval).
This can be used for crude animation. For more complex
animation, see :mod:`matplotlib.animation`.
This function is experimental; its behavior may be changed
or extended in a future release.
一个真正的过度杀伤方法是使用matplotlib.animate
模块。另一方面,如果您愿意的话,它为您提供了一种保存数据的好方法从我对[Python的回答中-1秒连续绘图。