FuncAnimation超越了frames参数 [英] FuncAnimation goes past the frames argument

查看:33
本文介绍了FuncAnimation超越了frames参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FuncAnimation包,使用有限差分实空间方法来求解高定波包与势垒碰撞的影片,以求解薛定inger方程.相关代码如下.基本上,当我运行它时,一切正常 - 弹出一部电影,显示我想要的内容.但是,更改"frames ="参数实际上并不会更改帧数.您可以看到我在动画函数中打印了当前迭代.该计数器上升到"frames =中指定的数字,但随后又回到0并继续运行.动画运行得比指定的更远.即使我指定"frames = 1",影片也会无限期地继续播放(我尝试将其放映一个下午).我对发生的事情感到非常困惑,但我相对确定这是愚蠢的事情.

I'm using the FuncAnimation package to make a movie of a gaussian wavepacket colliding with a potential barrier using the finite difference real-space method for solving the Schrodinger equation. The relevant code is below. Basically, when I run it, everything works well - a movie pops up showing just what I want. However, changing the "frames=" argument does not actually alter the number of frames. You can see that I print the current iteration in my animate function. This counter goes up to the number specified in "frames=", but then goes back to 0 and keeps going. The animation runs farther than specified. Even if I specify "frames=1", the movie will continue indefinitely (I tried leaving it running for an afternoon). I'm pretty stumped as to what's going on but I'm relatively sure it's something stupid.

# 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()

推荐答案

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

By default the animation function loops, just use the repeat kwarg:

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

这篇关于FuncAnimation超越了frames参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆