Skip to content

Instantly share code, notes, and snippets.

@ngundotra
Created May 27, 2018 07:10
Show Gist options
  • Save ngundotra/ae7b9862e75690d86282948b933595e7 to your computer and use it in GitHub Desktop.
Save ngundotra/ae7b9862e75690d86282948b933595e7 to your computer and use it in GitHub Desktop.
Generate cool gifs of complex plots in python
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
x = np.linspace(0, 2*np.pi, num=10000)
def f(x):
return np.exp(3*1j*x) + 0.5*np.exp(20j*x) + 1j/7*np.exp(-48j*x) + 0.3j*np.exp(37j*x)
y = f(x)
plt.plot(y.real, y.imag)
fig, ax = plt.subplots(1,1)
coeffs = np.array([1, 0, -1j/7, 0.3j, -0.001+0.04j, -0.003j])
stop_coeffs = np.array([3, np.pi+0.05, 1j/70-1.5, 0.2+0.3j/25, 3j, 0])
inners = np.array([3j, 20j, -48j, 37j, 54j, 71j])
funcs = np.array([np.exp(inner*x) for inner in inners])
start = 1
stop = 100
def update(i):
curr_coeffs = coeffs + i/stop*(stop_coeffs - coeffs)
f = lambda x: np.dot(curr_coeffs, funcs)
y = f(x)
# Uncomment the line below to see what's going on behind the scenes
# ax.clear()
ax.set_xticks([])
ax.set_yticks([])
ax.plot(y.real, y.imag)
return ax
anim = FuncAnimation(fig, update, frames=np.arange(start, stop), interval=70)
anim.save('complex_exponential.gif', dpi=80, writer='imagemagick')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment