python - Fitting a closed curve to a set of points -


i have set of points pts form loop , looks this:

enter image description here

this similar 31243002, instead of putting points in between pairs of points, fit smooth curve through points (coordinates given @ end of question), tried similar scipy documentation on interpolation:

values = pts tck = interpolate.splrep(values[:,0], values[:,1], s=1) xnew = np.arange(2,7,0.01) ynew = interpolate.splev(xnew, tck, der=0) 

but error:

valueerror: error on input data

is there way find such fit?

coordinates of points:

pts = array([[ 6.55525 ,  3.05472 ],    [ 6.17284 ,  2.802609],    [ 5.53946 ,  2.649209],    [ 4.93053 ,  2.444444],    [ 4.32544 ,  2.318749],    [ 3.90982 ,  2.2875  ],    [ 3.51294 ,  2.221875],    [ 3.09107 ,  2.29375 ],    [ 2.64013 ,  2.4375  ],    [ 2.275444,  2.653124],    [ 2.137945,  3.26562 ],    [ 2.15982 ,  3.84375 ],    [ 2.20982 ,  4.31562 ],    [ 2.334704,  4.87873 ],    [ 2.314264,  5.5047  ],    [ 2.311709,  5.9135  ],    [ 2.29638 ,  6.42961 ],    [ 2.619374,  6.75021 ],    [ 3.32448 ,  6.66353 ],    [ 3.31582 ,  5.68866 ],    [ 3.35159 ,  5.17255 ],    [ 3.48482 ,  4.73125 ],    [ 3.70669 ,  4.51875 ],    [ 4.23639 ,  4.58968 ],    [ 4.39592 ,  4.94615 ],    [ 4.33527 ,  5.33862 ],    [ 3.95968 ,  5.61967 ],    [ 3.56366 ,  5.73976 ],    [ 3.78818 ,  6.55292 ],    [ 4.27712 ,  6.8283  ],    [ 4.89532 ,  6.78615 ],    [ 5.35334 ,  6.72433 ],    [ 5.71583 ,  6.54449 ],    [ 6.13452 ,  6.46019 ],    [ 6.54478 ,  6.26068 ],    [ 6.7873  ,  5.74615 ],    [ 6.64086 ,  5.25269 ],    [ 6.45649 ,  4.86206 ],    [ 6.41586 ,  4.46519 ],    [ 5.44711 ,  4.26519 ],    [ 5.04087 ,  4.10581 ],    [ 4.70013 ,  3.67405 ],    [ 4.83482 ,  3.4375  ],    [ 5.34086 ,  3.43394 ],    [ 5.76392 ,  3.55156 ],    [ 6.37056 ,  3.8778  ],    [ 6.53116 ,  3.47228 ]]) 

actually, not far solution in question.

using scipy.interpolate.splprep parametric b-spline interpolation simplest approach. natively supports closed curves, if provide per=1 parameter,

import numpy np scipy.interpolate import splprep, splev import matplotlib.pyplot plt  # define pts question  tck, u = splprep(pts.t, u=none, s=0.0, per=1)  u_new = np.linspace(u.min(), u.max(), 1000) x_new, y_new = splev(u_new, tck, der=0)  plt.plot(pts[:,0], pts[:,1], 'ro') plt.plot(x_new, y_new, 'b--') plt.show() 

enter image description here

fundamentally, approach not different 1 in @joe kington's answer. although, bit more robust, because equivalent of i vector chosen, default, based on distances between points , not index (see splprep documentation u parameter).


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -