【问题标题】:sort points in order to have a continuous curve using python使用python对点进行排序以获得连续曲线
【发布时间】:2017-08-23 01:15:17
【问题描述】:

我有一个未排序的点列表:

列表 = [(-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,-4.748) (4.749) , 89.5087), (-41.6419, 88.9191), (-32.527, 88.7737), (-27.6403, 91.0134), (-22.3035, 95.141), (-18.0168, 100.473), (-15.2918, 1.6401,-14) 112.373), (-13.3475, 118.988), (-14.4509, 125.238), (-17.1246, 131.895), (-21.6766, 139.821), (-28.5735, 149.98), (-33.395, 15670,844)。 ), (-114.964, 87.4599), (-114.328, 89.8325), (-112.314, 91.6144), (-109.546, 92.0209), (-67.9644, 90.179), (-55.2013, 89.271,1), (8-36) , (-34.6987, 161.896), (-33.6055, 164.993), (-87.0365, 75.9683), (-99.8007, 76.0889), (-105.291, 76.5448), (-109.558, 77.3525), (-109.558, 77.3525), (-2502.) (-113.972, 81.3335), (2.30014, 171.635), (4.40918, 169.691), (5.07165, 166.974), (5.34843, 163.817), (5.30879, 161.7 98), (-29.6746, 73.5082), (-42.5876, 74.0206)]

我想对这些点进行排序,以使每个点的连续曲线只经过一次,从 start = (-29.6746, 73.5082) 开始 和 end = (5.30879, 161.798)

这是我迄今为止尝试过的:

import matplotlib.pyplot as plt
import numpy as np
from sklearn.neighbors import NearestNeighbors
import networkx as nx

for 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 the 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()

但我仍然得到一个未排序的列表,我找不到确定起点和终点的方法。

【问题讨论】:

  • 不确定你的代码在做什么,但如果我很好地理解了你的目标,那么简单的事情呢:X,Y = zip(sorted(List, key = lambda x: x[1]))?
  • 另外,你忘了告诉我们xy是如何声明的
  • 此外,order 似乎是一个列表,所以我不清楚 x[order] 应该是什么?
  • @alfasin x 是上面列表中的 x 坐标数组,y 是 y 坐标数组
  • order 是一个索引列表,当我执行 x[order] 时,我得到了新的 x 数组排序

标签: python list sorting curve continuous


【解决方案1】:

我们可以把这个问题看成一个旅行商问题,我们可以通过寻找最近的点来优化它

def distance(P1, P2):
"""
This function computes the distance between 2 points defined by
 P1 = (x1,y1) and P2 = (x2,y2) 
"""

    return ((P1[0] - P2[0])**2 + (P1[1] - P2[1])**2) ** 0.5


def optimized_path(coords, start=None):
"""
This function finds the nearest point to a point
coords should be a list in this format coords = [ [x1, y1], [x2, y2] , ...] 

"""
    if start is None:
        start = coords[0]
    pass_by = coords
    path = [start]
    pass_by.remove(start)
    while pass_by:
        nearest = min(pass_by, key=lambda x: distance(path[-1], x))
        path.append(nearest)
        pass_by.remove(nearest)
    return path

# define a start point
start = [x0, y0]

path = optimized_path(List,start)

【讨论】:

    【解决方案2】:

    不是答案,但评论太多了

    我将数据点绘制为散点和线

    我看到了一个视觉上平滑的(低阶局部导数样条曲线),大约 10% 的点“乱序”

    这是典型的问题吗?数据大部分是有序的吗?

    代码的一般性或具体性如何

    我不知道“大锤”库,但清理了周围的代码并做了同样的情节

    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 as plt
    import numpy as np
    from sklearn.neighbors import NearestNeighbors
    import networkx as 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 the 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()
    

    【讨论】:

    • 是的,已经有序列了。当我绘制我尝试过的新订单时,这正是问题所在,但它仍未排序。但是我尝试通过优化旅行商问题并通过定义起点来解决这个问题,我得到了排序点
    • S.sonia 如果您找到了解决方案,您应该将其作为答案发布在这里,这是 Stack Exchange 政策鼓励的
    猜你喜欢
    • 2013-03-27
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多