Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
restrepo
GitHub Repository: restrepo/ComputationalMethods
Path: blob/master/JSAnimation/make_animation.py
934 views
1
import numpy as np
2
from matplotlib import pyplot as plt
3
from matplotlib import animation
4
from JSAnimation import HTMLWriter
5
6
7
fig = plt.figure(figsize=(4, 3))
8
ax = plt.axes(xlim=(0, 10), ylim=(-2, 2))
9
line, = ax.plot([], [], lw=2)
10
11
def init():
12
line.set_data([], [])
13
return line,
14
15
def animate(i):
16
x = np.linspace(0, 10, 1000)
17
y = np.cos(i * 0.02 * np.pi) * np.sin(x - i * 0.02 * np.pi)
18
line.set_data(x, y)
19
return line,
20
21
anim = animation.FuncAnimation(fig, animate, init_func=init,
22
frames=100, interval=20, blit=True)
23
24
# set embed_frames=True to embed base64-encoded frames directly in the HTML
25
anim.save('animation.html', writer=HTMLWriter(embed_frames=True))
26
27