The examples below show simple 3D plots using matplotlib. matplotlib's 3D capabilities were added by incorporating John Porter's mplot3d module, thus no additional download is required any more, the following examples will run with an up to date matplotlib installation. Note, this code is not supported in matplotlib-0.98 and later, so please use the 0.91 maintenance release of matplotlib if you need this functionality. Alternatively, the Mayavi2 project provides a pylab-like API for extensive 3D plotting: http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab.html
Note that not all examples on this page are up to date, so some of them might not be working.
3D Plotting examples:
1 from numpy import *
2 import pylab as p
3 import matplotlib.axes3d as p3
4 # u and v are parametric variables.
5 # u is an array from 0 to 2*pi, with 100 elements
6 u=r_[0:2*pi:100j]
7 # v is an array from 0 to 2*pi, with 100 elements
8 v=r_[0:pi:100j]
9 # x, y, and z are the coordinates of the points for plotting
10 # each is arranged in a 100x100 array
11 x=10*outer(cos(u),sin(v))
12 y=10*outer(sin(u),sin(v))
13 z=10*outer(ones(size(u)),cos(v))
Wireframe (works on 0.87.5):
1 fig=p.figure()
2 ax = p3.Axes3D(fig)
3 ax.plot_wireframe(x,y,z)
4 ax.set_xlabel('X')
5 ax.set_ylabel('Y')
6 ax.set_zlabel('Z')
7 p.show()
3D Plot:
1 # this connects each of the points with lines
2 fig=p.figure()
3 ax = p3.Axes3D(fig)
4 # plot3D requires a 1D array for x, y, and z
5 # ravel() converts the 100x100 array into a 1x10000 array
6 ax.plot3D(ravel(x),ravel(y),ravel(z))
7 ax.set_xlabel('X')
8 ax.set_ylabel('Y')
9 ax.set_zlabel('Z')
10 fig.add_axes(ax)
11 p.show()
Scatter (works on 0.87.5, shows some artefacts):
1 fig=p.figure()
2 ax = p3.Axes3D(fig)
3 # scatter3D requires a 1D array for x, y, and z
4 # ravel() converts the 100x100 array into a 1x10000 array
5 ax.scatter3D(ravel(x),ravel(y),ravel(z))
6 ax.set_xlabel('X')
7 ax.set_ylabel('Y')
8 ax.set_zlabel('Z')
9 p.show()
Surface (works on 0.87.5):
1 fig=p.figure()
2 ax = p3.Axes3D(fig)
3 # x, y, and z are 100x100 arrays
4 ax.plot_surface(x,y,z)
5 ax.set_xlabel('X')
6 ax.set_ylabel('Y')
7 ax.set_zlabel('Z')
8 p.show()
Contour3D (works on 0.87.5):
1 delta = 0.025
2 x = arange(-3.0, 3.0, delta)
3 y = arange(-2.0, 2.0, delta)
4 X, Y = p.meshgrid(x, y)
5 Z1 = p.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
6 Z2 = p.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
7 # difference of Gaussians
8 Z = 10.0 * (Z2 - Z1)
9 fig=p.figure()
10 ax = p3.Axes3D(fig)
11 ax.contour3D(X,Y,Z)
12 ax.set_xlabel('X')
13 ax.set_ylabel('Y')
14 ax.set_zlabel('Z')
15 p.show()
Contourf3D:
1 # in mplt3D change:
2 # levels, colls = self.contourf(X, Y, Z, 20)
3 # to:
4 # C = self.contourf(X, Y, Z, *args, **kwargs)
5 # levels, colls = (C.levels, C.collections)
6 fig=p.figure()
7 ax = p3.Axes3D(fig)
8 ax.contourf3D(X,Y,Z)
9 ax.set_xlabel('X')
10 ax.set_ylabel('Y')
11 ax.set_zlabel('Z')
12 fig.add_axes(ax)
13 p.show()
2D Contour Plots (work on 0.87.5):
1 x=r_[-10:10:100j]
2 y=r_[-10:10:100j]
3 z= add.outer(x*x, y*y)
4 ### Contour plot of z = x**2 + y**2
5 p.contour(x,y,z)
6 ### ContourF plot of z = x**2 + y**2
7 p.figure()
8 p.contourf(x,y,z)
9 p.show()
For some other examples of 3d plotting capability, run the following commands. See the source of matplotlib/axes3d.py for more information:
1 # note that for the following to work you have to modify the test funcitons in your site-packages/matplotlib/axes3d.py like this:
2 #def test_xxxx():
3 # import pylab
4 # ax = Axes3D(pylab.figure())
5 # ....
6 # ....
7 # pylab.show()
8 # the following then work on 0.87.5
9 p3.test_bar2D()
10 p3.test_contour()
11 p3.test_scatter()
12 p3.test_scatter2D()
13 p3.test_surface()
14 # the following fail on 0.87.5
15 p3.test_plot()
16 p3.test_polys()
17 p3.test_wire()
Another example
Below is a simpler example to show use of plot_wireframe for some data collected:
1 data = [(x, y, z), (x2, y2, z2), ...]
First you want to create the mesh for the wireframe with meshgrid, in a similar fashion to Matlab:
1 X, Y = numpy.meshgrid(arange(0, 1.0, 0.1), arange(0, 1.0, 0.1))
This will create grid points from 0 to 1 with 0.1 intervals. Next we need to create the matrix to hold our data, and put our collected data into it (you can probably find a more efficient way to do this):
1 Z = numpy.zeros((len(Y), len(X)), 'Float32')
2 for d in data:
3 x, y, z = d
4 ix = int(x * 10)
5 iy = int(y * 10)
6 Z[iy, ix] = z
And now you can call the plot function:
1 import pylab as p
2 import matplotlib.axes3d as p3
3 fig = p.figure()
4 ax = p3.Axes3D(fig)
5 ax.plot_wireframe(X, Y, Z)
6 p.show()
