【问题标题】:Pylab only updating after raw_input callPylab 仅在 raw_input 调用后更新
【发布时间】:2014-06-06 16:26:06
【问题描述】:

我正在尝试连续读取文件并使用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

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

【问题讨论】:

    标签: python python-2.7 matplotlib


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多