Skip to content
Advertisement

fitting closed curve to a set of noisy points

This is my set of data, where I would like to fit a closed curve to, just like this post

JavaScript

here is the visualized dataset:

points in my dataset However, these are the results I got no matter how I sort my array. sort array according to x values sort array according to y values

I pinned a few problems about my dataset but don’t know how to deal with them:

  1. Many x and y values are not one to one
  2. Points are not sorted in a neighboring order

So if my assumptions are correct, the main question would be how can I sort the array in such an order that the splprep method works? If not, I would really appreciate any solution that helps me solve the problem!

[Update] Thanks to @michael-szczesny ‘s reply I got a satisfying result solved

Advertisement

Answer

You can translate your data to the origin an sort by the complex angle.

Setup the data

JavaScript

Convert your data to complex coordinates with np.angle((xs[:,0] + 1j*xs[:,1])) and use it to sort your data.

JavaScript

Now you can plot(code by @rth) your data in the right order.

JavaScript

Out:

plot sorted data

Advertisement