2014-10-05 34 views
7

我使用FuncAnimation包使用有限差分实空间方法来求解薛定谔方程,从而制作高斯波包的电影与势垒碰撞的电影。相关代码如下。基本上,当我运行它时,一切运行良好 - 一部电影弹出来显示我想要的。但是,更改“frames =”参数实际上并不会改变帧的数量。你可以看到我在我的animate函数中打印了当前的迭代。该计数器上升到“frames =”中指定的数字,但是然后返回到0并继续。动画运行得比指定的更远。即使我指定“frames = 1”,电影也会无限期地继续播放(我试着让它在一个下午运行)。我非常难以理解发生了什么,但我相对确定这是愚蠢的。FuncAnimation通过帧参数

# Set up the matplotlib figure and axes 
fig = plt.figure() 
ax = plt.axes(xlim = (0, hamiltonian.L), ylim = (0, 3)) 
line, = ax.plot([], [], lw = 2) 
time_text = ax.text(.02, .95, '', transform=ax.transAxes) 
ax.grid() 

def init(): 
    """initialize the animation""" 
    line.set_data([], []) 
    time_text.set_text('') 

    return line, time_text 

def animate(i): 
    """actually perform the animation""" 
    print i 
    global hamiltonian, wavepacket 
    hamiltonian.propagate(wavepacket) 
    line.set_data(wavepacket.x, wavepacket.psi_sq) 
    time_text.set_text('time = %.3f' % wavepacket.time_elapsed) 

    return line, time_text 

# Now call the animator 
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=1, blit=False) 
#anim.save('gaussian_reflection.mp4', fps=150, extra_args=['-vcodec', 'libx264']) 
plt.show() 

回答

8

默认情况下,动画功能循环,只需使用repeat kwarg:

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=1, blit=False, repeat=False) 
+0

解决了,谢谢。 – 2014-10-06 12:07:53