Created
May 18, 2021 03:02
-
-
Save neon-ninja/ea5fed876b2bc18a70423d89209a2d03 to your computer and use it in GitHub Desktop.
Python script to generate a gif animation of random lines
This file contains hidden or 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
| 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