Skip to content

Instantly share code, notes, and snippets.

@nicoguaro
Created July 22, 2015 20:27
Show Gist options
  • Save nicoguaro/f68e1ba0afc7d1f79bf6 to your computer and use it in GitHub Desktop.
Save nicoguaro/f68e1ba0afc7d1f79bf6 to your computer and use it in GitHub Desktop.
3D surface with light shading using matplotlib.
"""
Example showing shaded 3d plots. It is based on the [shading example](
http://matplotlib.org/examples/pylab_examples/shading_example.html).
The surface used is the Matlab `peaks()`.
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import LightSource
fig = plt.figure()
ax = fig.gca(projection='3d')
# Test data: Matlab `peaks()`
x, y = np.mgrid[-3:3:150j,-3:3:150j]
z = 3*(1 - x)**2 * np.exp(-x**2 - (y + 1)**2) \
- 10*(x/5 - x**3 - y**5)*np.exp(-x**2 - y**2) \
- 1./3*np.exp(-(x + 1)**2 - y**2)
# create light source object.
ls = LightSource(azdeg=0, altdeg=65)
# shade data, creating an rgb array.
rgb = ls.shade(z, plt.cm.RdYlBu)
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, linewidth=0,
antialiased=False, facecolors=rgb)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment