iPython 笔记本中的动画 [英] Animation in iPython notebook

查看:28
本文介绍了iPython 笔记本中的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将动画放入 iPython 笔记本中,但没有找到解决方案.我看到一篇讨论使用交互式小部件的帖子,但是我遇到了一些问题:首先,我看到的每个小部件示例都使用滑块或其他一些输入,而我只希望动画在单元格中自动运行正在运行.其次,Jupyter 站点上的所有文档似乎都过时了——每当我下载并运行他们的笔记本时,我都会收到有关某些模块已被弃用的消息,然后在文件中的某些内容无法运行,大概是因为他们正在尝试导入和访问不再存在的文件.

I am trying to put animations in an iPython notebook and am not finding a solution. I saw one post that discussed using interactive widgets, but there are a couple problems that I have with this: First, every example I see with widgets uses a slider or some other input, whereas I just want the animation to run automatically when the cell is run. Second, all the documentation seems out of date on Jupyter's site--whenever I download and run their notebooks I get these messages about certain modules being deprecated and then later in the file something fails to run, presumably because they're trying to import and access files that no longer exist.

我已经看到有关该主题的其他一些页面,但它们通常需要下载二进制文件或其他模块,但我部分使用它来教一些学生数学,我让他们下载 Anaconda--我希望不要让他们下载和安装更复杂的东西,同时花时间不谈论数学,从而进一步混淆问题.

I've seen a few other pages on the topic but they often require downloading binaries or other modules, but I'm partly using this to teach some students Math and I've gotten them to download Anaconda--I was hoping to not further confuse the issue by making them also download and install more complicated things all while spending time not talking about the Math.

简而言之,有没有一种方法可以在 iPython 笔记本中创建动画,只需要使用简单的导入命令,这些命令将开箱即用,可以说是来自 Anaconda 的软件?

So in short, is there a way that I can create animations in an iPython notebook that only require the use of simple import commands that will run out-of-the-box so to speak with the software that comes from Anaconda?

[进一步我想我也可以说出我目前希望制作动画的内容,尽管我真的希望在我决定可以制作动画的范围上非常灵活.现在我正在尝试制作一个数字时钟,以苏美尔基数 60 数字显示每个数字,以说明不同的计数和基数系统.所以它最初应该显示 |然后一秒之后 ||依此类推,直到十被表示为 <依此类推,直到最终时钟滴答到一分钟,现在显示 |:|代表一分一秒.]

[Further edit: I suppose I could also say what I am hoping to animate at the moment, although I really want to be pretty flexible about the range of things I could animate if I decide to. Right now I'm trying to make a digital clock that displays each digit in Sumerian base-60 numerals to illustrate a different counting and base system. So it should initially display | then after a second || and so on until ten gets represented as < and so on until eventually the clock ticks over to a minute where it now displays |:| to represent one minute, one second.]

[对未来人类的注意:如果您正在实施一些动画并愿意公开托管它,请在评论中留下链接!我很好奇现在人们是如何制作动画的,也有点好奇他们正在制作什么动画.]

[Note to future humans: If you're implementing some animation and are willing to publicly host it, please leave a link to it in the comments! I'm curious to see how people are making animations these days, and also a little curious to see what they're animating.]

推荐答案

在 Jupyter/IPython 中使用 matplotlib 为绘图设置动画的一些选项:

Some options you have for animating plots in Jupyter/IPython, using matplotlib:

  • 在循环中使用display 使用IPython.display.display(fig) 在输出中显示一个图形.使用循环,您可能希望在显示新图形之前清除输出.请注意,此技术通常提供不那么平滑的结果.因此,我建议使用以下任何一种.

  • Using display in a loop Use IPython.display.display(fig) to display a figure in the output. Using a loop you would want to clear the output before a new figure is shown. Note that this technique gives in general not so smooth resluts. I would hence advice to use any of the below.

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

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

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

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

for i in range(len(x)):
    animate(i)
    clear_output(wait=True)
    display(fig)
    
plt.show()

%matplotlib notebook 使用 IPython magic %matplotlib notebook 将后端设置为 notebook 后端.这将使图形保持活动状态,而不是显示静态 png 文件,因此也可以显示动画.
完整示例:

%matplotlib notebook Use IPython magic %matplotlib notebook to set the backend to the notebook backend. This will keep the figure alive instead of displaying a static png file and can hence also show animations.
Complete example:

%matplotlib notebook
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()
l, = ax.plot([0,2*np.pi],[-1,1])

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

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

plt.show()

%matplotlib tk 使用 IPython magic %matplotlib tk 将后端设置为 tk 后端.这将在一个新的绘图窗口中打开图形,该窗口是交互式的,因此也可以显示动画.
完整示例:

%matplotlib tk Use IPython magic %matplotlib tk to set the backend to the tk backend. This will open the figure in a new plotting window, which is interactive and can thus also show animations.
Complete example:

%matplotlib tk
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()
l, = ax.plot([0,2*np.pi],[-1,1])

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

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

plt.show()

将动画转换为 mp4 视频(@Perfi 已经提到的选项):

Convert animation to mp4 video (option mentionned by @Perfi already):

from IPython.display import HTML
HTML(ani.to_html5_video())

或在笔记本的开头使用 plt.rcParams["animation.html"] = "html5".这将需要有 ffmpeg 视频编解码器可用于转换为 HTML5 视频.然后视频内嵌显示.因此,这与 %matplotlib inline 后端兼容.完整示例:

or use plt.rcParams["animation.html"] = "html5" at the beginning of the notebook. This will require to have ffmpeg video codecs available to convert to HTML5 video. The video is then shown inline. This is therefore compatible with %matplotlib inline backend. Complete example:

%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams["animation.html"] = "html5"
import matplotlib.animation
import numpy as np

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

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

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

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

%matplotlib inline
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()
l, = ax.plot([0,2*np.pi],[-1,1])

animate = lambda 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_html5_video())

将动画转换为 JavaScript:

from IPython.display import HTML
HTML(ani.to_jshtml())

或在笔记本的开头使用 plt.rcParams["animation.html"] = "jshtml".这将使用 JavaScript 将动画显示为 HTML.这与大多数新浏览器以及 %matplotlib inline 后端高度兼容.它在 matplotlib 2.1 或更高版本中可用.
完整示例:

or use plt.rcParams["animation.html"] = "jshtml" at the beginning of the notebook. This will display the animation as HTML with JavaScript. This highly compatible with most new browsers and also with the %matplotlib inline backend. It is available in matplotlib 2.1 or higher.
Complete example:

%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams["animation.html"] = "jshtml"
import matplotlib.animation
import numpy as np

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

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

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

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

%matplotlib inline
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()
l, = ax.plot([0,2*np.pi],[-1,1])

animate = lambda 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())

这篇关于iPython 笔记本中的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆