动画只能显示第一帧

2024-05-11 03:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

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笔记本中编写了代码,matplotlib的版本是2.2.0,python的版本是2.7。 我试图显示一个动画,但输出的只是第一帧,一个静态图片。 我找不到错误。你知道吗

这是静态图片:

enter image description here


Tags: fromimportmatplotlibinitdefasnpline
1条回答
网友
1楼 · 发布于 2024-05-11 03:33:41

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

相关问题 更多 >