CoCalc Shared FilesSandbox / scratch.htmlOpen in CoCalc with one click!
Author: Todd Zimmerman
Views : 9
Description: Jupyter html version of Sandbox/scratch.ipynb
In [1]:
# matplotlib inline animation
import numpy as np
import matplotlib.pyplot as plt
from  matplotlib import animation as am, rc
from IPython.display import HTML
% matplotlib inline

def psi(x, t):
    return np.sin(2*np.pi*x)*np.sin(t)

def updatefig(*args):                       # args[0] = frame
    t = 0.1*args[0]
    plotobj.set_data(x, psi(x, t))     # update data
In [2]:
x = np.linspace(0, 1, 100)
fig = plt.figure()
plotobj = plt.plot(x, x)[0]         # plot object
ani = am.FuncAnimation(fig, updatefig, frames=100, interval=10)     # animate
(-1, 1)
In [4]:
### Save the animation
filename = 'animation.webm', fps=6, extra_args=['-vcodec', 'libvpx'])
In [5]:
### get the URL path where file is served in a project in cocalc:
import os
dirs = os.getcwd().split('/')
project_id = dirs[2]
path = os.path.join(*dirs[3:]) if dirs[3:] else ''
url = os.path.join('/', project_id, 'raw', path, filename)
In [6]:
### Show the animation in an html snippet

from IPython.display import HTML
HTML('''<video autoplay controls loop>
<source src="{url}" type="video/webm">
</video>'''.format(url = url))
In [ ]: