[SciPy-User] creating a 3D surface plot from collected data
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
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)
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
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)
Hope that helps some,
On Tue, Feb 16, 2010 at 6:00 AM, URI
> >Joe Kington <jkington <at> wisc.edu> writes:
> > Are your data already gridded? Or are they irregularly spaced? Seeing as
> you mentioned "real" data, I'm assuming they're irregularly spaced.The iy,
> example assumes that you have a regular grid that has been exported as an
> file instead of a grid of z coordinates.If they're not already a regular
> you'll need to interpolate them onto a regular grid before using any of the
> 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
> 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,
> = spline(xi,yi)
> > fig = plt.figure()ax = Axes3D(fig)ax.plot_surface(xi,yi,zi)plt.show()Hope
> This is exactly what I'm looking for, thanks. The problem is that when I
> in my data I get these errors:
> Traceback (most recent call last):
> File "C:\Python26\Lib\site-packages\xy\plotscript_griddata.py", line 33,
> spline = sp.interpolate.Rbf(xlist,ylist,zlist,function='thin-plate')
> File "C:\Python26\lib\site-packages\scipy\interpolate\rbf.py", line 138,
> self.nodes = linalg.solve(self.A, self.di)
> File "C:\Python26\lib\site-packages\scipy\linalg\basic.py", line 151, in
> raise LinAlgError, "singular matrix"
> LinAlgError: singular matrix
> The data I'm using is 2560 x,y,z grid points representing an anatomical
> exported from a medical imaging program. None of the values are zero and
> 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
> work (not that 144 is the exact cutoff, I just kept deleting chunks of
> 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
> 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
> see the new information at the top instead of having to scroll through a
> of stuff I've already read.
> SciPy-User mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the SciPy-User