Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook Sandbox/scratch.ipynb

Views: 126
Kernel: Python 3 (Ubuntu Linux)
# 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
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)
(-1, 1)
Image in a Jupyter notebook
### Save the animation filename = 'animation.webm' ani.save(filename, fps=6, extra_args=['-vcodec', 'libvpx'])
### 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
### 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))