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.
from mpl_toolkits.mplot3d import Axes3D
Now, we create a grid with the NumPy function meshgrid
.
# 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.
# compute the Z values
R = sqrt(X**2 + Y**2)
Z = sin(R)
Finally, we display the surface plot.
# plot the surface
ax = gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=mpl.cm.coolwarm, linewidth=0)