list - sort points in order to have a continuous curve using python -




i have list of unsorted points:

list = [(-50.6261, 74.3683), (-63.2489, 75.0038), (-76.0384, 75.6219), (-79.8451, 75.7855), (-30.9626, 168.085), (-27.381, 170.967), (-22.9191, 172.928), (-16.5869, 173.087), (-4.813, 172.505), (-109.056, 92.0063), (-96.0705, 91.4232), (-83.255, 90.8563), (-80.7807, 90.7498), (-54.1694, 89.5087), (-41.6419, 88.9191), (-32.527, 88.7737), (-27.6403, 91.0134), (-22.3035, 95.141), (-18.0168, 100.473), (-15.3918, 105.542), (-13.6401, 112.373), (-13.3475, 118.988), (-14.4509, 125.238), (-17.1246, 131.895), (-21.6766, 139.821), (-28.5735, 149.98), (-33.395, 156.344), (-114.702, 83.9644), (-114.964, 87.4599), (-114.328, 89.8325), (-112.314, 91.6144), (-109.546, 92.0209), (-67.9644, 90.179), (-55.2013, 89.5624), (-34.4271, 158.876), (-34.6987, 161.896), (-33.6055, 164.993), (-87.0365, 75.9683), (-99.8007, 76.0889), (-105.291, 76.5448), (-109.558, 77.3525), (-112.516, 79.2509), (-113.972, 81.3335), (2.30014, 171.635), (4.40918, 169.691), (5.07165, 166.974), (5.34843, 163.817), (5.30879, 161.798), (-29.6746, 73.5082), (-42.5876, 74.0206)]

i want sort points have continuous curve passing every point once, starting start = (-29.6746, 73.5082) , end = (5.30879, 161.798)

this tried far:

import matplotlib.pyplot plt import numpy np sklearn.neighbors import nearestneighbors import networkx nx  el in list:     x.append(el[0])     y.append(el[1])     x = np.array(x)     y = np.array(y)  points = np.c_[x, y]  # find 2 nearest neighbors  clf = nearestneighbors(2).fit(points)  g = clf.kneighbors_graph()  t = nx.from_scipy_sparse_matrix(g)  # indexes of new order   order = list(nx.dfs_preorder_nodes(t, 0))  # sorted arrays   new_x = x[order] new_y = y[order]  plt.plot(new_x, new_y) plt.show() 

but still unsorted list, , couldn't find way determine start point , end point.

not answer, comment

i plotted data points scatter , line enter image description here

i see visually smooth (low order local derivatve spline curve) ~10% points 'out of order'

is typical of problem?, data in order?

how general or specific code have be

i don't know "big hammer" libs, cleaned surounding code , did same plot

list = [(-50.6261, 74.3683), (-63.2489, 75.0038), (-76.0384, 75.6219), (-79.8451, 75.7855), (-30.9626, 168.085), (-27.381, 170.967), (-22.9191, 172.928), (-16.5869, 173.087), (-4.813, 172.505), (-109.056, 92.0063), (-96.0705, 91.4232), (-83.255, 90.8563), (-80.7807, 90.7498), (-54.1694, 89.5087), (-41.6419, 88.9191), (-32.527, 88.7737), (-27.6403, 91.0134), (-22.3035, 95.141), (-18.0168, 100.473), (-15.3918, 105.542), (-13.6401, 112.373), (-13.3475, 118.988), (-14.4509, 125.238), (-17.1246, 131.895), (-21.6766, 139.821), (-28.5735, 149.98), (-33.395, 156.344), (-114.702, 83.9644), (-114.964, 87.4599), (-114.328, 89.8325), (-112.314, 91.6144), (-109.546, 92.0209), (-67.9644, 90.179), (-55.2013, 89.5624), (-34.4271, 158.876), (-34.6987, 161.896), (-33.6055, 164.993), (-87.0365, 75.9683), (-99.8007, 76.0889), (-105.291, 76.5448), (-109.558, 77.3525), (-112.516, 79.2509), (-113.972, 81.3335), (2.30014, 171.635), (4.40918, 169.691), (5.07165, 166.974), (5.34843, 163.817), (5.30879, 161.798), (-29.6746, 73.5082), (-42.5876, 74.0206)]  import matplotlib.pyplot plt import numpy np sklearn.neighbors import nearestneighbors import networkx nx    points = np.asarray(list)  # find 2 nearest neighbors  clf = nearestneighbors(2).fit(points)  g = clf.kneighbors_graph()  t = nx.from_scipy_sparse_matrix(g)  # indexes of new order   order = list(nx.dfs_preorder_nodes(t, 0))  # sorted arrays   new_points = points[order]  plt.scatter(*zip(*points)) plt.plot(*zip(*new_points), 'r') plt.show() 

enter image description here





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -

python - Read npy file directly from S3 StreamingBody -