# [SciPy-User] creating a 3D surface plot from collected data

Joe Kington jkington@wisc....
Tue Feb 16 08:23:54 CST 2010

```No worries about the bottom post. I actually think I'm breaking protocol by
top-posting, but I use gmail, and it's just more natural to top-post.

Anyway, I think your problem is coming from the fact that you're working
with an isosurface that fully encloses a volume. I assumed that you had
scattered data points (say, elevation measurements) that you needed to
interpolate between.

The interpolation example I posted implicitly assumes that z = f(x,y). In
other words, that there is only one z value for any given x and y.  If
you're working with a 3D kidney-shape, this is clearly not the case.

Therefore, you're getting a singluar matrix when you try to interpolate
(more than one value of z for identical rows of x and y in the matrix).

The good news is that this may make the problem much simpler. (Your data is
already in some sort of triangle-strip format if it's been exported from an
imaging program. No need to interpolate.)

What happens when you just do:

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(x,y,z)
plt.show()

Where x,y,z are your raw data?  There's a reasonable chance that the program
you used to export the x,y,z isosurface left the verticies in the order that
plot_surface expects them in...

On a side note, you might also look into Mayavi's mlab module for 3D
plotting<http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab.html>.
I prefer it to matplotlib's native 3D plotting.
If you have it installed, you'd just do:

from enthought.mayavi import mlab
s = mlab.mesh(x, y, z)
mlab.show()

Hope that helps some,
-Joe

On Tue, Feb 16, 2010 at 6:00 AM, URI
<zallen@urologicresearchinstitute.org>wrote:

> >Joe Kington <jkington <at> wisc.edu> writes:
> >
> >
> >
> > Are your data already gridded? Or are they irregularly spaced?  Seeing as
> how
> you mentioned "real" data, I'm assuming they're irregularly spaced.The iy,
> ix
> example assumes that you have a regular grid that has been exported as an
> x,y,z
> file instead of a grid of z coordinates.If they're not already a regular
> grid,
> you'll need to interpolate them onto a regular grid before using any of the
> 3D
> surface plotting routines.(I'll skip my "interpolation is an artform" rant,
> here...  For a quick look splines are fine.)Look into using the various
> functions in sp.interpolate.  The example I threw together below uses a
> thin-plate spline, but there are lots of other options.import numpy as
> npimport
> scipy as spimport matplotlib.pyplot as pltimport scipy.interpolatefrom
> mpl_toolkits.mplot3d import Axes3Dx = np.random.random(10)y =
> np.random.random(10)z = np.random.random(10)spline =
> sp.interpolate.Rbf(x,y,z,function='thin-plate')xi = np.linspace(x.min(),
> x.max(), 50)yi = np.linspace(y.min(), y.max(), 50)xi, yi = np.meshgrid(xi,
> yi)zi
> = spline(xi,yi)
> > fig = plt.figure()ax = Axes3D(fig)ax.plot_surface(xi,yi,zi)plt.show()Hope
> that
> helps,-Joe
>
>
>
> Joe-
>
> This is exactly what I'm looking for, thanks.  The problem is that when I
> read
> in my data I get these errors:
>
> Traceback (most recent call last):
>  File "C:\Python26\Lib\site-packages\xy\plotscript_griddata.py", line 33,
> in
> <module>
>    spline = sp.interpolate.Rbf(xlist,ylist,zlist,function='thin-plate')
>  File "C:\Python26\lib\site-packages\scipy\interpolate\rbf.py", line 138,
> in
> __init__
>    self.nodes = linalg.solve(self.A, self.di)
>  File "C:\Python26\lib\site-packages\scipy\linalg\basic.py", line 151, in
> solve
>    raise LinAlgError, "singular matrix"
> LinAlgError: singular matrix
>
>
> The data I'm using is 2560 x,y,z grid points representing an anatomical
> contour
> exported from a medical imaging program.  None of the values are zero and
> it's
> not some crazy shape - basically it looks like a kidney, so no spike or
> holes or
> anything.  I've found that if I chop the data down to 144 lines it will
> then
> work (not that 144 is the exact cutoff, I just kept deleting chunks of
> lines
> from the data file until it worked).  This tells me that I've successfully
> adapted your code and that I'm reading in the data properly as numpy arrays
> - I
> was worried that I might just have some formatting problems.
>
> Any idea why I can't run this with my entire data set?  This is just a test
> run,
> I will potentially need to process much larger datasets in the future.
>
> P.S. Sorry for the bottom-post, the website won't let me submit unless I
> post my
> message below yours.  Seems stupid to me, I think it makes a lot more sense
> to
> see the new information at the top instead of having to scroll through a
> bunch
> of stuff I've already read.
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100216/714b6ffb/attachment.html
```

More information about the SciPy-User mailing list