[SciPyuser] How to draw a 3D graphic of a function?
James A. Bednar
jbednar@inf.ed.ac...
Sun Apr 13 02:22:33 CDT 2008
 Date: Fri, 11 Apr 2008 15:02:03 +0200
 From: Gael Varoquaux <gael.varoquaux@normalesup.org>

 On Fri, Apr 11, 2008 at 08:57:50PM +0800, zhang chi wrote:
 > I want to draw a matrix of 100 X 100, its elements are the values of a function.

 I suppose you want to map the value of your matrix to the altitude of a
 surface?

 You can do this with Mayavi2. Have a look at the user guide,
As shown below, you can also do this with matplotlib, which more
people will probably have installed. There was a suggestion that I add
this to the matplotlib cookbook, but I still haven't gotten a chance
to do so...
Jim
 Date: Tue, 02 Oct 2007 04:56:56 0400
 From: Joe Harrington <jh@physics.ucf.edu>

 Or, you could just do it with matplotlib...

 http://physicsmajor.wordpress.com/2007/04/22/3dsurfacewithmatplotlib/

 This was the first hit on a google search for "matplotlib surface". I
 tested it and it works in 0.90.1.
Interesting! I couldn't find any documentation at all, but after some
hacking on that script I was able to make matplotlib 0.90.1 plot a
wireframe surface for a 2D numpy array, so I thought it could be
useful to include the code (below).
Note that the original example uses plot_surface instead of
plot_wireframe, but I've found plot_surface to be quite buggy, with
plots disappearing entirely much of the time, while plot_wireframe has
been reliable so far. There is also contour3D, though that doesn't
look very useful yet. Hopefully these 3D plots will all be polished
up a bit and made public in a new matplotlib release soon!
Jim
_______________________________________________________________________________
import pylab
from numpy import outer,arange,cos,sin,ones,zeros,array
from matplotlib import axes3d
def matrixplot3d(mat,title=None):
fig = pylab.figure()
ax = axes3d.Axes3D(fig)
# Construct matrices for r and c values
rn,cn = mat.shape
c = outer(ones(rn),arange(cn*1.0))
r = outer(arange(rn*1.0),ones(cn))
ax.plot_wireframe(r,c,mat)
ax.set_xlabel('R')
ax.set_ylabel('C')
ax.set_zlabel('Value')
if title: windowtitle(title)
pylab.show()
matrixplot3d(array([[0.1,0.5,0.9],[0.2,0.1,0.0]]))

The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
More information about the SciPyuser
mailing list