Created
December 16, 2021 16:19
-
-
Save jonahpearl/c4cb8e52e838233751e655723043c42a to your computer and use it in GitHub Desktop.
Template for video from mpl plots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import numpy as np | |
import imageio as iio | |
def my_plt_plotting_func(data): | |
plt.plot(data) # can be as extensive as you want, as long as it just makes a single pyplot fig | |
def write_frame(data, vid): | |
my_plt_plotting_func(data) | |
plt.savefig('current_frame.tiff') | |
plt.close() | |
t = iio.imread('current_frame.tiff') | |
vid.append_data(t) | |
try: | |
min_time = 0 # seconds | |
max_time = 10 | |
step = 0.1 # seconds per frame | |
width = 5 # how long to plot on x-axis | |
fname = 'sniff_test.mp4' | |
vid = iio.get_writer(fname, format='FFMPEG', mode='I', fps=1/step) | |
for timestamp in np.arange(min_time, max_time, step): | |
data = get_window_of_my_data(timestamp, timestamp+width) | |
write_frame(data, vid) | |
vid.close() | |
except KeyboardInterrupt: | |
vid.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment