如何从matplotlib.animation中删除静态图?

时间:2016-11-14 19:35:40

标签: python animation matplotlib jupyter-notebook

以下是使用matplotlib生成动画的代码。当我在Jupyter笔记本中运行它时,我还在动画图下面得到了另一个静态图。如何删除它?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML

fig, ax = plt.subplots()

x = np.arange(0, 20, 0.1)
ax.scatter(x, x + np.random.normal(0, 3.0, len(x)))
line, = ax.plot(x, x - 5, 'r-', linewidth=2)

def update(i):
    label = 'timestep {0}'.format(i)
    line.set_ydata(x - 5 + i)
    ax.set_xlabel(label)
    return line, ax

anim = FuncAnimation(fig, update, frames=np.arange(0, 10), interval=200)
HTML(anim.to_html5_video())

1 个答案:

答案 0 :(得分:0)

我使用名为JSAnimation的模块(参见this example notebook from the Author)。 要显示动画,只需调用:

from JSAnimation.IPython_display import display_animation
display_animation(anim)