Skip to content

Instantly share code, notes, and snippets.

@nomissbowling
Last active July 29, 2018 05:16
Show Gist options
  • Save nomissbowling/e7499ff59c175f170aff4fa313f40dc9 to your computer and use it in GitHub Desktop.
Save nomissbowling/e7499ff59c175f170aff4fa313f40dc9 to your computer and use it in GitHub Desktop.
animation GIF from matplotlib mesh grid 3D wireframe
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
https://matplotlib.org/examples/mplot3d/wire3d_animation_demo.html
https://snap.textfile.org/20160327173316/
'''
import sys, os
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from PIL import Image
DSTFILE = 'test_animation.gif'
WH = (640, 480)
def animMeshGif(fn):
images = []
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
gen = lambda x, y, t: np.sin(x + 3. * t) + np.cos(y + 5. * t)
x, y = np.meshgrid(
np.linspace(-np.pi, np.pi, 100), np.linspace(-np.pi, np.pi, 100))
ax.set_zlim(-2., 2.)
for t in np.linspace(0., 2. * np.pi, 100):
z = gen(x, y, t)
wf = ax.plot_wireframe(x, y, z) # , rstride=2, cstride=2)
plt.pause(.016)
ar = np.array(ax.figure.canvas.renderer._renderer, dtype='uint8')
images.append(Image.fromarray(ar).resize(WH, Image.BICUBIC))
ax.collections.remove(wf)
images[0].save(fn, save_all=True, append_images=images[1:],
optimize=False, duration=120, loop=0)
if __name__ == '__main__':
animMeshGif(DSTFILE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment