Plotting and Visualization

Animination

Python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

n = 500
x = np.linspace(0, 6, n)
y = x**2-2*x+5


FFMpegWriter = animation.writers['ffmpeg']

metadata = dict(title = "Movie test", artist = "Matplotlib",  comment='a red circle following a blue sine wave')
writer  = FFMpegWriter(fps = 20, metadata=metadata)

fig = plt.figure()

sin_line = plt.plot(x, y, color = "b")
red_circle, = plt.plot([], [], 'ro', markersize = 10)
plt.xlabel("x")
plt.ylabel('y')

with writer.saving(fig, "writer_test.mp4", 100):
    for i in range(n):
        x0 = x[i]
        y0 = y[i]
        red_circle.set_data(x0, y0)
        writer.grab_frame()
        
from IPython.display import HTML

HTML("""
<div align="middle">
<video width="80%" controls>
      <source src="writer_test.mp4" type="video/mp4">
</video></div>""")