[SciPy-User] Interpolating discrete values with splprep and splev
Matthew Cragun
mcragun@gmail....
Thu Aug 19 16:08:49 CDT 2010
I'm trying to fit a curve to some data points using splprep and splev. Then
I would like to take a few discrete values and get the inerpolated values
back using the spline.
I am having a problem getting the correct y values out based on the spline
fit. In my code below I would like a plot of the saex values against their
interpolated values, but can't get them out properly.
saex2 gives: [ 37.27079882 55.12775627 106.64397968 142.10925103
158.67651813 176.27838508]
saey gives: [ 1.76685635e-01 -3.89842080e+00 -5.48621169e+01
-1.42948834e+02 -2.03504645e+02 -2.83162159e+02]
Any thoughts? Here is my code:
from numpy import arange, cos, linspace, pi, sin, random, r_, array
from scipy.interpolate import splprep, splev
# Original points
x = array([0,3,6,9,12,15])
y = array([0.530,0.546,0.586,0.658,0.727,0.801])
#Interpolated X locations
saex = array([2.484,3.671,7.063,9.354,10.408,11.515])
# spline parameters
s=3.0 # smoothness parameter
k=3 # spline order
nest=-1 # estimate of number of knots needed (-1 = maximal)
# find the knot points
tckp,u = splprep([x,y],s=s,k=k,nest=nest)
# evaluate spline
splinex,spliney= splev(linspace(0,1,400),tckp)
#evaluate at discrete points
saex2,saey= splev(saex,tckp,der=0)
print saex2
#plot
import pylab
#plot the data
data,=pylab.plot(x,y,'o',label='data')
#plot the fitted curve
fit,=pylab.plot(splinex,spliney,'r-',label='fit')
#plot the discrete locations
#sae,=pylab.plot(saex2,saey,'x',label='sae points')
pylab.legend()
pylab.xlabel('x')
pylab.ylabel('y')
pylab.savefig('splprep_demo.png')
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20100819/597863bc/attachment.html
More information about the SciPy-User
mailing list