如何在Jupyter中显示动画?

huangapple go评论123阅读模式
英文:

How do I show animation in Jupyter?

问题

我从手册中复制了代码并在新的笔记本中运行它。

不幸的是,我只看到了图形:

如何在Jupyter中显示动画?

(并且单元格上标有“*”)。

当我重新启动内核时,有时我会看到动画的开头:

如何在Jupyter中显示动画?

但我从未看到matplotlib手册中提到的控件。

  1. 服务器信息:
  2. 您正在使用Jupyter Notebook
  3. 笔记本服务器的版本是:6.5.4-762f3618
  4. 服务器正在运行的Python版本是:
  5. Python 3.10.6(主要版本,2023529日,11:10:38)[GCC 11.3.0]
  6. 当前内核信息:
  7. Python 3.10.6(主要版本,2023529日,11:10:38)[GCC 11.3.0]

我可以将动画保存为mp4文件并查看它,但我更喜欢交互式功能。

我做错了什么?

英文:

I copied code from the manual:

  1. %matplotlib notebook
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. import numpy as np
  5. fig, ax = plt.subplots()
  6. t = np.linspace(0, 3, 40)
  7. g = -9.81
  8. v0 = 12
  9. z = g * t**2 / 2 + v0 * t
  10. v02 = 5
  11. z2 = g * t**2 / 2 + v02 * t
  12. scat = ax.scatter(t[0], z[0], c="b", s=5, label=f'v0 = {v0} m/s')
  13. line2 = ax.plot(t[0], z2[0], label=f'v0 = {v02} m/s')[0]
  14. ax.set(xlim=[0, 3], ylim=[-4, 10], xlabel='Time
    展开收缩
    ', ylabel='Z [m]')
  15. ax.legend()
  16. def update(frame):
  17. # for each frame, update the data stored on each artist.
  18. x = t[:frame]
  19. y = z[:frame]
  20. # update the scatter plot:
  21. data = np.stack([x, y]).T
  22. scat.set_offsets(data)
  23. # update the line plot:
  24. line2.set_xdata(t[:frame])
  25. line2.set_ydata(z2[:frame])
  26. return (scat, line2)
  27. ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30)
  28. plt.show()

into a new notebook and run it.

Alas, all I see is the figure:

如何在Jupyter中显示动画?

(and the cell is marked with *).

When I restart the kernel, sometimes I get the beginning of the animation:

如何在Jupyter中显示动画?

but I never get the controls present in the matplotlib manual.

  1. Server Information:
  2. You are using Jupyter Notebook.
  3. The version of the notebook server is: 6.5.4-762f3618
  4. The server is running on this version of Python:
  5. Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
  6. Current Kernel Information:
  7. Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]

I can save the animation in an mp4 file and view it, but I would much prefer the interactive facility.

What am I doing wrong?

答案1

得分: 1

在导入库之后,请添加以下内容:

  1. from matplotlib import rc
  2. rc('animation', html='jshtml')

然后在此处调用 ani

  1. ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30)
  2. plt.show()
  3. ani

这将打开交互式窗口。

英文:

Include the following after importing the libraries:

  1. from matplotlib import rc
  2. rc('animation', html='jshtml')

Then call ani here:

  1. ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30)
  2. plt.show()
  3. ani

This would bring the interactive window.

huangapple
  • 本文由 发表于 2023年7月7日 03:39:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定