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.
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.
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.
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)
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())
If using matplotlib 2.1, you may also opt for a JavaScript animation,
from IPython.display import HTML
HTML(ani.to_jshtml())
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.