CoCalc Shared FilesSandbox / scratch.htmlOpen in CoCalc with one click!
Author: Todd Zimmerman
Views : 9
Description: Jupyter html version of Sandbox/scratch.ipynb
scratch
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
plt.ylim(-1,1)
Out[2]:
(-1, 1)
In [4]:
### Save the animation
filename = 'animation.webm'
ani.save(filename, 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)
print(url)
/a34619e5-067a-491f-9bf6-18ace479ffb3/raw/Sandbox/animation.webm
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))
Out[6]:
In [ ]: