Skip to content

Instantly share code, notes, and snippets.

@PfannenHans
Created October 27, 2021 19:31
Show Gist options
  • Select an option

  • Save PfannenHans/06a6717fb82a8819ffb773d9b5f6de4f to your computer and use it in GitHub Desktop.

Select an option

Save PfannenHans/06a6717fb82a8819ffb773d9b5f6de4f to your computer and use it in GitHub Desktop.
Creating interference animation with matplotlib and ffmpeg
#!/usr/bin/env python3
# Creating output video file with ffmpeg:
# ffmpeg -framerate 60 -i %d.png -loop -1 -threads 8 loop.webm
import multiprocessing
import matplotlib.pyplot as plt
import numpy as np
def compute_frame(offset):
x = np.arange(0, 8*np.pi, 0.1)
y0 = np.sin(x)
y1 = np.sin(x+0.01*offset*np.pi/3) + 2.5
y2 = y0+y1+4
plt.axis('off')
plt.ylim([-1.5, 9])
plt.plot(x, y0, color="#008fff")
plt.plot(x, y1, color="#ee4d2e")
plt.plot(x, y2, color="#1db992")
plt.savefig((str(offset) + ".png"), dpi=500, bbox_inches='tight', pad_inches = 0, facecolor="#161618")
plt.clf()
if __name__ == '__main__':
with multiprocessing.Pool(multiprocessing.cpu_count()) as p:
p.map(compute_frame, range(600))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment