In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation, writers
In [2]:
n = 2**8
x = np.arange(n) / n
y = np.arange(n) / n
x, y = x - 1/2, y - 1/2
X, Y = np.meshgrid(x, y)
In [3]:
sig = 0.1
a = 1.4
rot = np.pi/32

fig, ax = plt.subplots()
plt.set_cmap("gray")

phase = 0
def animate(i):
    phase = angle = i*rot
    
    X_rot = np.cos(angle) * X + np.sin(angle) * Y
    Y_rot =-np.sin(angle) * X + np.cos(angle) * Y
    
    Z = np.exp(- ( X_rot**2 + Y_rot**2 / a**2 ) / (2*sig**2)) 
    Z *= np.cos(2*np.pi*4*X_rot - phase)
    Z[X_rot**2 + Y_rot**2/a**2 > (3*sig)**2] = 0
    
    img = ax.imshow(Z, vmin=-1, vmax=1, origin="lower", 
                    extent=[x[0], x[-1], y[0], y[-1]])
    return [img]

# Set up formatting for the movie files
Writer = writers['ffmpeg']
writer = Writer(fps=10, bitrate=1800)

ani = FuncAnimation(fig, animate, 64, blit=True)
ani.save("movie.mp4", writer=writer)

plt.show()