如何在我的 Python Jupyter Notebook 上播放使用 ImageMagick 制作的动画?
我对 Python 动画很陌生,所以我正在研究如何制作动画。到目前为止,我从 DevelopPaper 复制了代码,但是当我运行单元格时,它只显示它的静态图像。我还尝试了其他动画示例,但所有这些示例都是一样的——来自 Imagemagick 的动画无法在 Jupyter Notebook 上播放,但当我手动打开保存的文件时,它工作得很好。如何让动画在 Jupyter Notebook 本身上播放?谢谢!
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
plt.style.use('seaborn-pastel')
fig = plt.figure()
ax = plt.axes(xlim=(0, 4), ylim=(-2, 2))
line, = ax.plot([], [], lw=3)
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 4, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
anim = FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
anim.save('sine_wave.gif', writer='imagemagick')
有很多选择。 (您可能无法轻松找到解决方案,因为您没有使用帖子标题所反映的正确术语。您似乎想在 Jupyter 中播放
.gif
或由帧组成的动画情节。)
最简单的方法是使用 JupyterLab 打开一个新笔记本并运行您的代码单击此处后。 (要在那里使用您的代码,请将最后一行更改为
anim.save('sine_wave.gif')
,因为我还没有费心在那里安装 imagemagick。Pillow 能够制作 gif,如果您不这样做,matplotlib 将处理为任务选择 Pillow不指定writer
。)
gif 文件制作完成后,您只需在左侧文件浏览器中双击它即可播放。您可以通过单击其上方的选项卡并拖动然后根据需要释放它来根据需要相对于笔记本排列其播放的窗口。
如果您更喜欢使用更传统的笔记本来播放 gif,只需在生成 gif 文件后将以下代码放入单元格中即可:
from IPython.display import Image
Image("sine_wave.gif")
(请注意,here它表示对于本地
.gif
文件,您需要读取字节并显示它,例如Image("sine_wave.gif","rb").read())
。因此,如果不像我在Unix系统。)
如果 gif 图像文件与笔记本文件位于同一文件夹中,则执行该笔记本单元将播放 gif 作为该单元的输出。
对于如何正确播放绘图动画(不一定是 gif),我建议按照示例和链接添加控件向笔记本底部,您可以通过单击此处的“启动活页夹”以活动形式打开该笔记本 。您正在寻找使用或涉及底部
FuncAnimation()
的示例和链接。我建议涉及框架的框架允许您使用小部件来控制和播放它们,或者制作可以播放和控制的 HTML。重要的是,即使笔记本是“静态”的,该小部件方法也可以工作,正如您在here中看到的那样。我将 static 放在引号中,因为一些活动功能可以在 nbviewer 中使用,尽管通常会在 Github 预览中注明,但现在 Github 尝试对笔记本进行预览渲染,即使是很长的内容,这会让很多新手感到困惑。 (如果您需要生成一个与 HTML5 兼容的便携式视频文件,该文件将在同一部分中介绍。)以下是代码的一些变体,使用相关方法来添加将在活动会话中工作的小部件/框架生成这里:
一个选项import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML, display
plt.style.use('seaborn-pastel')
def generate_animation():
fig = plt.figure()
ax = plt.axes(xlim=(0, 4), ylim=(-2, 2))
lineplot, = ax.plot([], [], lw=3)
def init():
lineplot.set_data([], [])
return lineplot, #return [lineplot] also works like in https://nbviewer.org/github/raphaelquast/jupyter_notebook_intro/blob/master/jupyter_nb_introduction.ipynb#pre-render-animations-and-export-to-HTML
def animate(i):
x = np.linspace(0, 4, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
lineplot.set_data([x], [y])
return [lineplot]
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
display(HTML(anim.to_jshtml()))
plt.close(fig)
generate_animation()
另一种变体# RUN THIS TWICE. SECOND TIME WILL BE NICE SINGLE PLOT DISPLAYED.
# January 2023 I was finding first time I ran this or code like at https://nbviewer.org/gist/fomightez/d862333d8eefb94a74a79022840680b1 that it output a non-interactive frame of plot, too. Just re-ran and then it is just the interactive one with widget.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML, display
plt.rcParams["animation.html"] = "jshtml"
plt.ioff() #needed so the second time you run it you get only single plot
plt.style.use('seaborn-pastel')
fig = plt.figure()
ax = plt.axes(xlim=(0, 4), ylim=(-2, 2))
lineplot, = ax.plot([], [], lw=3)
def init():
lineplot.set_data([], [])
return lineplot, #return [lineplot] also works like in https://nbviewer.org/github/raphaelquast/jupyter_notebook_intro/blob/master/jupyter_nb_introduction.ipynb#pre-render-animations-and-export-to-HTML
def animate(i):
x = np.linspace(0, 4, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
lineplot.set_data([x], [y])
return [lineplot]
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
anim
运行这两个选项的 Jupyter 笔记本更全面的解释在这里;您会注意到小部件在那里工作。
单击此处,在通过 MyBinder.org 提供的活动临时 Jupyter 会话中启动该笔记本,环境中已经安装了 Python 以及使带有小部件的动画在已安装并工作的笔记本中工作所需的一切。