Chapter 4, example 6

Matplotlib has a toolkit, mplot3d, which offers several 3D plotting features. We are going to show a simple example here.

We first need to load explicitely the 3D toolkit.


In [1]:
from mpl_toolkits.mplot3d import Axes3D

Now, we create a grid with the NumPy function meshgrid.


In [2]:
# create a (X, Y) grid
X = linspace(-5, 5, 50)
Y = X
X, Y = meshgrid(X, Y)

We create a surface plot z = sin(r) where z is the height, and r is the distance from origin.


In [3]:
# compute the Z values
R = sqrt(X**2 + Y**2)
Z = sin(R)

Finally, we display the surface plot.


In [4]:
# plot the surface
ax = gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=mpl.cm.coolwarm, linewidth=0)