Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Last active March 11, 2020 09:38
Show Gist options
  • Save jamescalam/6fcf9357e920b4ea245cce90b14c0381 to your computer and use it in GitHub Desktop.
Save jamescalam/6fcf9357e920b4ea245cce90b14c0381 to your computer and use it in GitHub Desktop.
Matplotlib animation of rotating surface
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, cm
from mpl_toolkits.mplot3d import Axes3D
# create a figure
fig = plt.figure()
# initialise 3D Axes
ax = Axes3D(fig)
# remove background grid, fill and axis
ax.grid(False)
ax.xaxis.pane.fill = ax.yaxis.pane.fill = ax.zaxis.pane.fill = False
plt.axis('off')
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
xx, yy = np.meshgrid(X, Y)
r = np.sqrt(xx**2 + yy**2)
z = np.cos(r)
# create the initialiser with the surface plot
def init():
ax.plot_surface(xx, yy, z, cmap=cm.gnuplot,
linewidth=0, antialiased=False)
return fig,
# create animate function, this will adjust the view one step at a time
def animate(i):
ax.view_init(elev=30.0, azim=i)
return fig,
# create the animated plot
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=360, interval=20, blit=True)
# save as a GIF
anim.save('surface_rotation.gif', fps=30, writer='imagemagick')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment