简体   繁体   English

在Jupyter Notebook外显示动画

[英]Display animation outside of jupyter notebook

I want to use Jupyter notebook to host my code for a presentation, but I don't want to embed animation into the notebook. 我想使用Jupyter笔记本来托管演示文稿的代码,但我不想将动画嵌入到笔记本中。 (Because it is time-consuming to embed the video.) I want to run the cells and pop up a screen as if I am running the code in the terminal. (因为嵌入视频非常耗时。)我想运行单元格并弹出一个屏幕,就像在终端中运行代码一样。

from matplotlib.animation import FuncAnimation 
from matplotlib.pyplot import plot, show, subplots, title  # annotate
from IPython.display import HTML

anim = FuncAnimation(fig, update, frames=numlin, interval=100, fargs=( 
                     d, g, lr_D, lr_G, hasFake, speed, show_sample),
                     init_func=init, blit=True, repeat=0)           
HTML(anim.to_html5_video())

Why using the notebook? 为什么要使用笔记本? The main reason to use the notebook is that I have many different setups for an experiment. 使用笔记本电脑的主要原因是我有许多用于实验的不同设置。 I want to use different cells to represent different configurations, and if people want to see results from a particular configuration, I can run it right away. 我想使用不同的单元格来代表不同的配置,如果人们想查看特定配置的结果,则可以立即运行它。

Time difference . 时差 The HTML function takes over a minute to generate the video I need. HTML函数需要一分钟以上的时间来生成我需要的视频。 While in the terminal, the animation would just start. 在终端中时,动画将开始。 I want to prototype quickly during a meeting while the audience asks to show the results from different initial conditions. 我想在会议期间快速制作原型,而听众要求显示不同初始条件下的结果。

There is also an unexpected behavior from the notebook . 笔记本计算机还有意外行为 The video from the notebook is different from that popped up in the terminal. 笔记本中的视频与终端中弹出的视频不同。 The video in the notebook did not erase existing frames while drawing, making the animation looks messy and cannot track the trajectory as good as its counterpart. 笔记本中的视频在绘制时不会删除现有的帧,从而使动画看起来很凌乱,无法跟踪其轨迹。

Animation from the notebook's output 笔记本输出中的动画

Animation from the terminal's output 终端输出中的动画

This drawing behavior is another reason why I don't want to use the notebook to display the animation. 这种绘画行为是我不想使用笔记本显示动画的另一个原因。

Will the notebook needs to show other plots. 笔记本需要显示其他图吗? I hope so, but it is not necessary. 我希望如此,但这不是必需的。 I can open another notebook for just plots if needed. 如果需要,我可以打开另一个笔记本仅用于绘图。

Please let me know if I do not explain it well. 如果我不好解释,请告诉我。

Animation inside of notebook 笔记本内的动画

Reading the question, I wonder if you are aware of the %matplotlib notebook backend. 阅读问题,我想知道您是否知道%matplotlib notebook后端。 While it will show the animation inside the notebook, I feel that it would suit all the described needs. 虽然它将在笔记本中显示动画,但我认为它可以满足所有描述的需求。

%matplotlib notebook
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation 
import numpy as np

a = np.random.rand(10,4)
fig, ax =plt.subplots()
ax.axis([0,1,0,1])
points1, = plt.plot([],[], ls="", marker="d", color="indigo")
points2, = plt.plot([],[], ls="", marker="o", color="crimson")

def update(i):
    points1.set_data(a[i:i+2,0],a[i:i+2,1])
    points2.set_data(a[i:i+2,2],a[i:i+2,3])
    return points1, points2

anim = FuncAnimation(fig, update, frames=len(a)-1, repeat=True)

Note that using this kind of animation, where the data is updated with set_data is showing the same whether saved to video or shown on screen. 请注意,使用这种动画(其中用set_data更新数据)无论是保存到视频还是在屏幕上显示都相同。 Hence, if it wasn't for the time it takes to replace the video, you could well use it in the initially shown way, deleting %matplotlib notebook and adding 因此,如果没有时间替换视频,则可以按照最初显示的方式使用它,删除%matplotlib notebook并添加

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

If using matplotlib 2.1, you may also opt for a JavaScript animation, 如果使用matplotlib 2.1,您还可以选择JavaScript动画,

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

Animation in new window 新窗口中的动画

If you want to have a window appearing, you should neither use %matplotlib inline nor %matplotlib notebook , instead replace the first line in the above code by 如果要显示一个窗口,则既不应使用%matplotlib inline也不应该使用%matplotlib notebook ,而应将上面代码中的第一行替换为

%matplotlib tk

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM