Skip to content

Instantly share code, notes, and snippets.

@dvalters
Created August 30, 2018 07:25
Show Gist options
  • Save dvalters/09b0cc9c5755d68fe026a0fe9a786171 to your computer and use it in GitHub Desktop.
Save dvalters/09b0cc9c5755d68fe026a0fe9a786171 to your computer and use it in GitHub Desktop.
import subprocess
import matplotlib.pyplot as plt
import numpy as np
def generate_plot_figures():
NUM_PLOTS_TO_ANIMATE = 10
for i in range(NUM_PLOTS_TO_ANIMATE + 1):
# Create each plot separately
fig, ax = plt.subplots()
L = 2 * np.pi
x = np.linspace(0, L)
nb_colors = len(plt.rcParams['axes.prop_cycle'])
shift = np.linspace(0, L, nb_colors, endpoint=False)
for s in shift:
ax.plot(x, np.sin(x + s), '-')
ax.set_xlim([x[0], x[-1]])
fig.savefig("animation_frame_" + str(i) + ".png")
def animate_plots(base_directory, fname_prefix):
"""
This function creates a movie of the plots using ffmpeg
Args:
base_directory (str): the directory with the plots.
fname_prefix (str): the filename for the model run
Returns:
none but creates mp4 from pngs in base directory
Author: FJC
"""
import subprocess
# animate the pngs using ffmpeg
system_call = "ffmpeg -framerate 5 -pattern_type glob -i '"+base_directory+"*.png' -vcodec libx264 -s 1230x566 -pix_fmt yuv420p "+base_directory+fname_prefix+"_movie.mp4"
print system_call
subprocess.call(system_call, shell=True)
generate_plot_figures()
animate_plots(base_dir, prefix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment