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

URI zallen@urologicresearchinstitute....
Tue Feb 16 06:00:57 CST 2010

```>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