#!/usr/bin/env python # coding: utf-8 # ## for SO https://stackoverflow.com/q/75006292/8508004 # # The example by Louis Tiao (see http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-as-interactive-javascript-widgets/) seems to be what Gulraiz is using even though no attribution is given. (Louis Tiao cites Jake VanderPlas.) # # There was stuff missing. I probably have a version of this somewhere but these work in [this environment January 2023](https://github.com/fomightez/animated_matplotlib-binder/tree/9cebb12b49ffe2c297398ad95a5a66afa39dcaf1)(specific commit used recorded in link). # And I saw they worked in JupyterLab in that environment as well. # This first is based example by Louis Tiao (see http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-as-interactive-javascript-widgets/) and source code from StackOverflow with approach like https://nbviewer.org/github/raphaelquast/jupyter_notebook_intro/blob/master/jupyter_nb_introduction.ipynb#pre-render-animations-and-export-to-HTML # In[3]: import numpy as np import matplotlib.pyplot as plt from matplotlib import animation from IPython.display import HTML, display plt.style.use('seaborn-v0_8-pastel') def generate_animation(): fig = plt.figure() ax = plt.axes(xlim=(0, 4), ylim=(-2, 2)) lineplot, = ax.plot([], [], lw=3) def init(): lineplot.set_data([], []) return lineplot, #return [lineplot] also works like in https://nbviewer.org/github/raphaelquast/jupyter_notebook_intro/blob/master/jupyter_nb_introduction.ipynb#pre-render-animations-and-export-to-HTML def animate(i): x = np.linspace(0, 4, 1000) y = np.sin(2 * np.pi * (x - 0.01 * i)) lineplot.set_data([x], [y]) return [lineplot] anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True) display(HTML(anim.to_jshtml())) plt.close(fig) generate_animation() # This second is based on more along line of example by Louis Tiao (see http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-as-interactive-javascript-widgets/) and source code from StackOverflow where uses rcParams to set jshtml. I've used something much similar like that [here](https://gist.github.com/fomightez/d862333d8eefb94a74a79022840680b1) and that is sort of what I based it on. It basically handles the `display(HTML(anim.to_jshtml()))` part a little more implicitly (albeit maybe explicitly is the better way to go in most cases [plus that method doesn't seem to require running twice at present??] - I had used the other somewhere and so easier to have both examples around). # In[3]: # RUN THIS TWICE. SECOND TIME WILL BE NICE SINGLE PLOT DIPLAYED. # January 2023 I was finding first time I ran this or code like at https://nbviewer.org/gist/fomightez/d862333d8eefb94a74a79022840680b1 that it output a non-interactive frame of plot, too. Just re-ran and then it is just the interactive one with widget. import numpy as np import matplotlib.pyplot as plt from matplotlib import animation from IPython.display import HTML, display plt.rcParams["animation.html"] = "jshtml" plt.ioff() #needed so the second time you run it you get only single plot plt.style.use('seaborn-v0_8-pastel') fig = plt.figure() ax = plt.axes(xlim=(0, 4), ylim=(-2, 2)) lineplot, = ax.plot([], [], lw=3) def init(): lineplot.set_data([], []) return lineplot, #return [lineplot] also works like in https://nbviewer.org/github/raphaelquast/jupyter_notebook_intro/blob/master/jupyter_nb_introduction.ipynb#pre-render-animations-and-export-to-HTML def animate(i): x = np.linspace(0, 4, 1000) y = np.sin(2 * np.pi * (x - 0.01 * i)) lineplot.set_data([x], [y]) return [lineplot] anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True) anim # In[ ]: