Pylab仅在raw_input调用后更新

时间:2014-06-06 16:26:06

标签: python python-2.7 matplotlib

我尝试使用matplotlib作为创建动画的一种方式,不断读取文件和绘图。这是工作代码:

import numpy as np
import pylab
import time

print "start"

pylab.ion() # Unfortunately, will still need to run pylab.show() all the time
fig = pylab.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xlim((0,128))
ax.set_ylim((0,128))

#Initialize the circle object to a silly location
f1 = pylab.Circle((60,68), radius=2, fc='y')
ax.add_patch(f1)
pylab.show()

# Start the animation
for i in range(0,1000):
    if(i%2 == 0):
        f1.center = 30, 30
    else:
        f1.center = 40, 40
    pylab.show()
    raw_input("Press Enter to continue...")
    print("Updating")
    #time.sleep(2) # This is going to be used later

现在,我想用睡眠定时器替换用户输入:

import numpy as np
import pylab
import time

print "start"

pylab.ion() # Unfortunately, will still need to run pylab.show() all the time
fig = pylab.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xlim((0,128))
ax.set_ylim((0,128))

#Initialize the circle object to a silly location
f1 = pylab.Circle((60,68), radius=2, fc='y')
ax.add_patch(f1)
pylab.show()

# Start the animation
for i in range(0,1000):
    if(i%2 == 0):
        f1.center = 30, 30
    else:
        f1.center = 40, 40
    pylab.show()
    #raw_input("Press Enter to continue...")
    print("Updating")
    time.sleep(2) # This is going to be used later

但是这段代码不起作用!它睡了,但没有更新。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要将time.sleep(2)替换为pylab.pause(2),如this question的评论中所示。