Differences between revisions 23 and 24

Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
Note that not all examples on this page are up to date, so some of them might not be working. Note that not all examples on this page are up to date, so some of them might not be working. For other examples, see http://matplotlib.sourceforge.net/examples/mplot3d/
Line 12: Line 12:
import matplotlib.axes3d as p3 #import matplotlib.axes3d as p3
import mpl_toolkits.mplot3d.axes3d as p3
Line 165: Line 167:
p3.test_wire()
}}}
attachment:test1.jpg attachment:test2.jpg attachment:test3.jpg

== Another example ==
Below is a simpler example to show use of ''plot_wireframe'' for some data collected:

{{{
#!python
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:

{{{
#!python
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):

{{{
#!python
Z = numpy.zeros((len(Y), len(X)), 'Float32')
for d in data:
   x, y, z = d
   ix = int(x * 10)
   iy = int(y * 10)
   Z[iy, ix] = z
}}}
And now you can call the plot function:

{{{
#!python
import pylab as p
import matplotlib.axes3d as p3
fig = p.figure()
ax = p3.Axes3D(fig)
ax.plot_wireframe(X, Y, Z)
p.show()
}}}
----
 . CategoryCookbookMatplotlib
p3.test_wir

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 the matplotlib-0.98 branch, but you can use either the latest 0.99 release or the 0.91 maintenance version 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. For other examples, see http://matplotlib.sourceforge.net/examples/mplot3d/

3D Plotting examples:

   1 from numpy import *
   2 import pylab as p
   3 #import matplotlib.axes3d as p3
   4 import mpl_toolkits.mplot3d.axes3d as p3
   5 
   6 # u and v are parametric variables.
   7 # u is an array from 0 to 2*pi, with 100 elements
   8 u=r_[0:2*pi:100j]
   9 # v is an array from 0 to 2*pi, with 100 elements
  10 v=r_[0:pi:100j]
  11 # x, y, and z are the coordinates of the points for plotting
  12 # each is arranged in a 100x100 array
  13 x=10*outer(cos(u),sin(v))
  14 y=10*outer(sin(u),sin(v))
  15 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:

Cookbook/Matplotlib/mplot3D (last edited 2010-03-30 19:50:19 by MartinSpacek)