# 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)
### 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)
### 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))