Skip to content

Instantly share code, notes, and snippets.

@neon-ninja
Created May 18, 2021 03:02
Show Gist options
  • Select an option

  • Save neon-ninja/ea5fed876b2bc18a70423d89209a2d03 to your computer and use it in GitHub Desktop.

Select an option

Save neon-ninja/ea5fed876b2bc18a70423d89209a2d03 to your computer and use it in GitHub Desktop.
Python script to generate a gif animation of random lines
from matplotlib import pyplot as plt
from numpy.random import rand
from matplotlib.collections import LineCollection
from matplotlib import cm
import numpy as np
import imageio
from IPython.display import Image
ims = []
for frame in range(100):
points = rand(10, 4, 2)
values = rand(10)
fig, ax = plt.subplots(figsize=(10, 10))
ax.figure.tight_layout()
ax.add_collection(LineCollection(points, colors=cm.hsv(values)))
fig.canvas.draw() # draw the canvas, cache the renderer
image = np.frombuffer(fig.canvas.tostring_rgb(), dtype='uint8')
image = image.reshape(fig.canvas.get_width_height()[::-1] + (3,))
ims.append(image)
imageio.mimsave("animation.gif", ims, fps=10)
with open('animation.gif','rb') as file:
display(Image(file.read()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment