【问题标题】:FuncAnimation goes past the frames argumentFuncAnimation 超越了 frames 参数
【发布时间】:2014-10-05 11:26:14
【问题描述】:

我正在使用 FuncAnimation 包制作高斯波包与势垒碰撞的电影,使用有限差分实空间方法求解薛定谔方程。相关代码如下。基本上,当我运行它时,一切正常——会弹出一部电影,显示我想要的。但是,更改“frames=”参数实际上并不会改变帧数。你可以看到我在我的 animate 函数中打印了当前的迭代。该计数器上升到“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()

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    默认动画函数循环,只需使用repeat kwarg:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-08
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      相关资源
      最近更新 更多