# [SciPy-user] How to fit a surface from a list of measured 3D points ?

Zachary Pincus zachary.pincus@yale....
Tue Mar 31 11:29:23 CDT 2009

```Hi Loïc,

What's your eventual goal for the fit surface? There are a lot of
possible approaches possible with the tools in scipy (e.g. fit a
spline, as you have done, or fit a simpler parameteric surface in a
least-squares fashion, or go for a nonparametric approach and
interpolate between the points with radial basis functions).  The best
approach will likely depend on what you want to do with the surface...
but what you have seems like a reasonable start.

Also, ndimage.map_coordinates is for resampling of an array (e.g.
image) at arbitrary coordinates: perhaps not of direct use here.

Zach

On Mar 31, 2009, at 4:48 AM, Loïc BERTHE wrote:

> Hi,
>
> I have a list of 856 measured 3d points and would like to fit a 3D
> surface from theses points.
> Theses points are not regularly spaced.
>
> Here is the code I used :
>
> from numpy import *
> from matplotlib.mlab   import csv2rec
> from matplotlib.pyplot import *
>
> data = csv2rec('data.csv', delimiter=';')
>
> figure(1)
> scatter(data.x, data.y, s=data.z, c=data.z)
> colorbar()
> title('data points : z=f(x,y)')
> xlabel('x')
> ylabel('y')
> grid()
>
>
> # interpolation spline with scipy
> from scipy import interpolate
>
> tck0 = interpolate.bisplrep(data.x, data.y, data.z)
> xnew,ynew = mgrid[-1:1:70j,-1:1:70j]
> znew = interpolate.bisplev(xnew[:,0],ynew[0,:],tck0)
> figure()
> pcolor(xnew,ynew,znew)
> colorbar()
> title("Interpolated z=f(xnew,ynew)")
> show()
>
> I've attached the two figures describing the data and the fit which is
> not very interesting.
> Is there a better approach to fit theses data ?
> I had a look to the ndimage module and the map_coordinates function
> but I don't know if this is a better tool for this problem.
> Have you an example of the ndimage use ?
>
> Regards,
> <
> small_data
> .jpg><small_fit.jpg>_______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

```