Jupyter中的内联动画

Ari*_*man 32 python animation matplotlib ipython-notebook jupyter-notebook

我有一个python动画脚本(使用matplotlib的funcAnimation),它在Spyder中运行但在Jupyter中不运行.我尝试过以下各种建议,例如添加"%matplotlib inline"和将matplotlib后端更改为"Qt4agg",都没有成功.我也试过运行几个动画示例(来自Jupyter教程),但没有一个有效.有时我会收到错误消息,有时会出现情节,但不会生成动画.顺便说一句,我已经让 pyplot.plot()使用"%matplotlib inline"工作.

有没有人知道一个工作Jupyter笔记本与SIMPLE内联动画示例使用funcAnimation.在此先感谢您的帮助!

[注意:我在Windows 7上]

Imp*_*est 47

笔记本后端

"内联"表示图表显示为png图形.那些png图像无法动画.虽然原则上可以通过连续替换png图像来构建动画,但这可能是不期望的.

一个解决方案是使用笔记本后端,它完全兼容,FuncAnimation因为它呈现matplotlib图本身:

%matplotlib notebook
Run Code Online (Sandbox Code Playgroud)

jsanimation

从matplotlib 2.1开始,我们可以使用JavaScript创建动画.这与ani.to_html5()解决方案类似,不同之处在于它不需要任何视频编解码器.

from IPython.display import HTML
HTML(ani.to_jshtml())
Run Code Online (Sandbox Code Playgroud)

一些完整的例子:

import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
ax.axis([0,2*np.pi,-1,1])
l, = ax.plot([],[])

def animate(i):
    l.set_data(t[:i], x[:i])

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))

from IPython.display import HTML
HTML(ani.to_jshtml())
Run Code Online (Sandbox Code Playgroud)

或者,将jsanimation设置为显示动画的默认设置,

plt.rcParams["animation.html"] = "jshtml"
Run Code Online (Sandbox Code Playgroud)

然后在最后简单地说明ani获取动画.

另请参阅此答案以获取完整概述.

  • AttributeError:'FuncAnimation'对象没有属性'to_jshtml' (6认同)
  • @AliceSchwarze 将 `plt.close()` 放在 `ani = ...` 声明之后,以消除初始图。然后第二个“ani”启动动画。 (3认同)
  • 当我将示例复制到网络中时,它在我第一次执行时运行良好。第二次它返回动画和一个我似乎无法摆脱的空图形。它从何而来? (2认同)

Big*_*gsy 13

本教程中有一个简单的例子:http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-notebooks/

总结一下上面的教程,你基本上需要这样的东西:

from matplotlib import animation
from IPython.display import HTML

# <insert animation setup code here>

anim = animation.FuncAnimation()  # With arguments of course!
HTML(anim.to_html5_video())
Run Code Online (Sandbox Code Playgroud)

然而...

工作上我遇到了很多麻烦.本质上,问题是上面使用(默认情况下)ffmpeg和x264背景中的编解码器,但这些在我的机器上没有正确配置.解决方案是卸载它们并使用正确的配置从源重建它们.有关详细信息,请参阅Andrew Heusser的工作答案我提出的问题:ipython(jupyter)笔记本中的动画 - ValueError:关闭文件的I/O操作

所以,首先尝试to_html5_video上面的解决方案,如果它不起作用,那么也尝试卸载/重建ffmpeg和x264.


Mos*_*Geo 5

这是我从多个来源(包括官方示例)汇总的答案。我使用最新版本的 Jupyter 和 Python 进行了测试。

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

#=========================================
# Create Fake Images using Numpy 
# You don't need this in your code as you have your own imageList.
# This is used as an example.

imageList = []
x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
for i in range(60):
    x += np.pi / 15.
    y += np.pi / 20.
    imageList.append(np.sin(x) + np.cos(y))

#=========================================
# Animate Fake Images (in Jupyter)

def getImageFromList(x):
    return imageList[x]

fig = plt.figure(figsize=(10, 10))
ims = []
for i in range(len(imageList)):
    im = plt.imshow(getImageFromList(i), animated=True)
    ims.append([im])

ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True, repeat_delay=1000)
plt.close()

# Show the animation
HTML(ani.to_html5_video())

#=========================================
# Save animation as video (if required)
# ani.save('dynamic_images.mp4')
Run Code Online (Sandbox Code Playgroud)


Kol*_*ril 5

另外一个选项:

import matplotlib.animation
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["animation.html"] = "jshtml"
plt.rcParams['figure.dpi'] = 150  
plt.ioff()
fig, ax = plt.subplots()

x= np.linspace(0,10,100)
def animate(t):
    plt.cla()
    plt.plot(x-t,x)
    plt.xlim(0,10)

matplotlib.animation.FuncAnimation(fig, animate, frames=10)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 由于某种原因,这会分别给出静态图像和动画。 (4认同)