是否有人知道一个工作良好的Jupyter笔记本,其中包含使用funcAnimation的简单内联动画示例。
[注意:我使用的是Windows 7]
是否有人知道一个工作良好的Jupyter笔记本,其中包含使用funcAnimation的简单内联动画示例。
[注意:我使用的是Windows 7]
“内嵌”表示图表以png格式显示。这些png图像无法进行动画处理。虽然原则上可以通过不断替换png图像来构建动画,但这可能是不可取的。
一种解决方案是使用笔记本后端,它与FuncAnimation
完全兼容,因为它自己呈现matplotlib图。
%matplotlib notebook
从matplotlib 2.1开始,我们可以使用JavaScript创建动画。这类似于ani.to_html5()
的解决方案,但它不需要任何视频编解码器。
from IPython.display import HTML
HTML(ani.to_jshtml())
一些完整的示例:
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())
plt.rcParams["animation.html"] = "jshtml"
最后只需简单地声明ani
以获得动画。
此外,请参见此答案以获取完整概述。
在此教程中有一个简单的示例,链接如下: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())
然而...
我在让它工作上遇到了很多麻烦。实际上,问题在于上述内容使用(默认)ffmpeg
和x264
编解码器来处理视频,但这些在我的机器上配置不正确。解决方法是卸载它们,并从源代码重新构建它们以获得正确的配置。有关更多详细信息,请参见我提出的问题,Andrew Heusser给出了一个有效的答案:Animations in ipython (jupyter) notebook - ValueError: I/O operation on closed file
因此,首先尝试上述的to_html5_video
解决方案,如果它不起作用,还可以尝试卸载/重建ffmpeg
和x264
。
另一种选择:
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)
%matplotlib inline
后,代码将输出动画而不是静态图像。 - undefined以下是我从多个来源(包括官方示例)整理出来的答案。我已经测试了最新版本的Jupyter和Python。
imageList
,它是图片的列表(输入)。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')
from keras.preprocessing.image import load_img, img_to_array
from matplotlib import animation
from IPython.display import HTML
import glob
%matplotlib inline
def plot_images(img_list):
def init():
img.set_data(img_list[0])
return (img,)
def animate(i):
img.set_data(img_list[i])
return (img,)
fig = figure()
ax = fig.gca()
img = ax.imshow(img_list[0])
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=len(img_list), interval=20, blit=True)
return anim
imgs = [img_to_array(load_img(i)) for i in glob.glob('*.jpg')]
HTML(plot_images(imgs).to_html5_video())
plt.rcParams['animation.ffmpeg_path'] = '/usr/bin/ffmpeg'
。请将/usr/bin/ffmpeg'
替换为您操作系统中ffmpeg二进制文件的路径。 - Ruthvik Vailaimport matplotlib.animation
import matplotlib.pyplot as plt
from itertools import count
import random
plt.rcParams["animation.html"] = "jshtml"
plt.rcParams['figure.dpi'] = 150
fig, ax = plt.subplots()
x_value = []
y_value = []
index = count();
def animate(t):
x_value.append(next(index))
y_value.append(random.randint(0,10))
ax.cla()
ax.plot(x_value,y_value)
ax.set_xlim(0,10)
matplotlib.animation.FuncAnimation(fig, animate, frames=10, interval = 500)
!pip install matplotlib --upgrade
- Salainplt.close()
放在ani = ...
的声明后面,以消除初始图。第二个ani
然后启动动画。 - hahnecjupyter nbconvert --html <notebook.ipyb>
,但是它没有显示任何输出。 - Gabe Morris