【问题标题】:Animation can only display the firstframe [duplicate]动画只能显示第一帧[重复]
【发布时间】:2018-12-18 02:25:13
【问题描述】:
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))
def animate(i):
    line.set_ydata(np.sin(x + i/10.0))
    return line,
def init():
    line.set_ydata(np.sin(x))
    return line,
ani = animation.FuncAnimation(fig=fig,
                              func=animate,
                              frames=100,
                              init_func=init,
                              interval=20,
                              blit=False)
plt.show()

我在 jupyter Notebook 中编码,matplotlib 的版本是 2.2.0,python 的版本是 2.7。 我试图显示一个动画,但输出只是第一帧,一张静态图片。 我找不到错误。

这是静态图片:

【问题讨论】:

  • 除了下面提供的答案之外,您还可以使用“笔记本”后端。将%matplotlib inline 替换为%matplotlib notebook

标签: python matplotlib jupyter-notebook


【解决方案1】:

Jupyter 笔记本输出无法动画的 png。您可以使用 Javascript 为笔记本中的绘图设置动画。

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


fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))
def animate(i):
    line.set_ydata(np.sin(x + i/10.0))
    return line,
def init():
    line.set_ydata(np.sin(x))
    return line,
ani = animation.FuncAnimation(fig=fig,
                              func=animate,
                              frames=100,
                              init_func=init,
                              interval=20,
                              blit=False)

HTML(ani.to_jshtml())

【讨论】:

    猜你喜欢
    • 2021-05-25
    • 2019-02-09
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多