在Jupyter Notebook外显示动画 [英] Display animation outside of jupyter notebook

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

问题描述

我想使用Jupyter笔记本托管演示文稿的代码,但我不想将动画嵌入到笔记本中. (因为嵌入视频非常耗时.)我想运行单元格并弹出一个屏幕,就像在终端中运行代码一样.

I want to use Jupyter notebook to host my code for a presentation, but I don't want to embed animation into the notebook. (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.

时差. HTML函数需要一分钟以上的时间来生成我需要的视频.在终端中时,动画将开始.我想在会议期间快速制作原型,而听众要求显示来自不同初始条件的结果.

Time difference. The HTML function takes over a minute to generate the video I need. 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.

笔记本计算机输出中的动画

来自终端输出的动画

这种绘画行为是我不想使用笔记本来显示动画的另一个原因.

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.

推荐答案

笔记本内部的动画

阅读问题,我想知道您是否知道%matplotlib notebook后端.虽然它将在笔记本中显示动画,但我认为它可以满足所有描述的需求.

Animation inside of notebook

Reading the question, I wonder if you are aware of the %matplotlib notebook backend. 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)

请注意,使用这种动画(其中用set_data更新数据)无论是保存到视频还是显示在屏幕上都显示相同的内容. 因此,如果没有时间替换视频,则可以按照最初显示的方式使用它,删除%matplotlib notebook并添加

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. 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

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

如果使用matplotlib 2.1,您还可以选择JavaScript动画

If using matplotlib 2.1, you may also opt for a JavaScript animation,

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

新窗口中的动画

如果要显示一个窗口,则既不要使用%matplotlib inline也不应该使用%matplotlib notebook,而应使用

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 tk

这篇关于在Jupyter Notebook外显示动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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