gpt4 book ai didi

python - FuncAnimation 超越了 frames 参数

转载 作者:太空狗 更新时间:2023-10-29 20:20:05 25 4
gpt4 key购买 nike

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

# 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:

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

关于python - FuncAnimation 超越了 frames 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26202164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com