Skip to content

Instantly share code, notes, and snippets.

@whitead
Created August 27, 2021 14:48
Show Gist options
  • Save whitead/07f4ca3a24d505daea2ddb15cfa805e7 to your computer and use it in GitHub Desktop.
Save whitead/07f4ca3a24d505daea2ddb15cfa805e7 to your computer and use it in GitHub Desktop.
Some code for animating
from matplotlib.collections import LineCollection
fps = 60.
stride = 1
duration = (T - 5) / fps / stride
print(duration, fps)
all_segments = [make_segments(paths, i) for i in range(N)]
fig = plt.figure(figsize=(1080 //180, 1080 // 180), dpi=180)
ax = fig.gca()
all_lcs = []
carray = np.linspace(1,0,T)
#viridis
#cividis
for i in range(N):
lc = LineCollection(all_segments[i], cmap='viridis', norm=plt.Normalize(0,1), alpha=0.5)
# Set the values used for colormapping
lc.set_array(np.concatenate((carray[:10], np.repeat(np.nan, T - 1))))
lc.set_linewidth(1)
line = ax.add_collection(lc)
all_lcs.append(lc)
ax.set_xlim(-system.box.Lx / 2, system.box.Lx / 2)
ax.set_ylim(-system.box.Ly / 2, system.box.Ly / 2)
ax.set_xticks([])
ax.set_yticks([])
ax.set_aspect('equal')
fig.patch.set_facecolor('black')
ax.set_facecolor('black')
plt.tight_layout()
def new_make_frame(t):
i = int(t * fps * stride)
for j in range(N):
#all_lcs[j].set_segments(all_segments[j][:i])
all_lcs[j].set_array(np.concatenate((carray[:i], np.repeat(np.nan, T - i))))
return mplfig_to_npimage(fig)
for i,t in tqdm(enumerate(np.arange(0,duration,1 / fps))):
a = new_make_frame(t)
im = Image.fromarray(a)
im.save(f'parts-{i:05d}.png')
del im
del a
# when complete
# ffmpeg -framerate 40 -i parts-%05d.png -c:v h264 -crf 14 -c:v libx264 -movflags +faststart -vf format=yuv420p -maxrate 25M -tune animation -bufsize 1MB mov.mp4
# if on twitter, maybe consider
# ffmpeg -framerate 15 -i parts-%05d.png mov.gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment