#!/usr/bin/env python from numpy import arange, cos, linspace, pi, sin, random from scipy.interpolate import splprep, splev # make ascending spiral in 3-space t=linspace(0,1.75*2*pi,100) x = sin(t) y = cos(t) z = t # add noise x+= random.normal(scale=0.1, size=x.shape) y+= random.normal(scale=0.1, size=y.shape) z+= random.normal(scale=0.1, size=z.shape) # spline parameters s=3.0 # smoothness parameter k=2 # spline order nest=-1 # estimate of number of knots needed (-1 = maximal) # find the knot points tckp,u = splprep([x,y,z],s=s,k=k,nest=-1) # evaluate spline, including interpolated points xnew,ynew,znew = splev(linspace(0,1,400),tckp) import matplotlib.pyplot as plt plt.subplot(2,2,1) data,=plt.plot(x,y,'bo-',label='data') fit,=plt.plot(xnew,ynew,'r-',label='fit') plt.legend() plt.xlabel('x') plt.ylabel('y') plt.subplot(2,2,2) data,=plt.plot(x,z,'bo-',label='data') fit,=plt.plot(xnew,znew,'r-',label='fit') plt.legend() plt.xlabel('x') plt.ylabel('z') plt.subplot(2,2,3) data,=plt.plot(y,z,'bo-',label='data') fit,=plt.plot(ynew,znew,'r-',label='fit') plt.legend() plt.xlabel('y') plt.ylabel('z') #plt.savefig('splprep_demo.png',dpi=200) plt.show()