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

Joe Kington jkington at wisc.edu
Mon Feb 15 12:20:30 EST 2010


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 np
import scipy as sp
import matplotlib.pyplot as plt
import scipy.interpolate
from mpl_toolkits.mplot3d import Axes3D

x = 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

On Mon, Feb 15, 2010 at 8:17 AM, URI
<zallen at urologicresearchinstitute.org>wrote:

> I am trying to figure out how to create a 3D surface plot from some x,y,z
> data
> that I have.  I don't have any particular preference as to how to do it,
> but
> from poking around on the web it seems like mplot3d should be able to do
> it,
> based on what I see in the tutorial (look at the wireframe or surface plot
> sections):
>
> http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/tutorial.html
>
> The problem I have is that none of the example source code is based on
> working
> with numbers from actual data.  The only thing I could find that addresses
> this
> issue is at the very bottom of this page under the heading "Another
> Example":
> http://www.scipy.org/Cookbook/Matplotlib/mplot3D
>
> I've tried to duplicate this method but I get errors, and I don't really
> understand it anyway.  The place I get lost is here:
>
> 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
>
>
> I don't understand how ix and iy are supposed to give me valid indeces for
> the Z
> array (and Python doesn't seem to understand it either, it gives me an
> error at
> that last line).
>
> Can someone either explain this method to me, or point me in a different
> direction?
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at 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/20100215/3aee2783/attachment.html>


More information about the SciPy-User mailing list