【问题标题】:How to sort array of coordinates to groups of column coordinates?如何将坐标数组排序为列坐标组?
【发布时间】:2022-01-12 05:53:51
【问题描述】:

有一个点数组,绘制如下。点集中有列结构。

如何将这些点分组到不同的列中?

np.array([[151,  26],[ 30,  26],[511,  27],[747,  30],[609,  28],[930,  30],
   [ 30,  52],[211,  53],[513,  54],[608,  54],[824,  56],[946,  55],
   [106,  87],[187,  87],[512,  89],[609,  90],[725,  90],[823,  92],
   [931,  92],[ 28, 113],[301, 113],[512, 115],[609, 116],[ 28, 142],
   [107, 141],[220, 142],[511, 143],[724, 145],[823, 146],[937, 146],
   [ 29, 168],[308, 168],[512, 171],[ 28, 197],[107, 197],[205, 198],
   [511, 199],[724, 200],[940, 201],[ 29, 222],[307, 223],[217, 244],
   [107, 273],[ 28, 274],[201, 273],[511, 276],[725, 277],[937, 279],
   [ 28, 299],[273, 301],[218, 321],[ 28, 351],[107, 350],[201, 351],
   [511, 354],[723, 354],[947, 356],[ 29, 376],[297, 377]])

预期输出是

线条代表每组坐标,在视觉上形成一列。

提前感谢您的宝贵时间 - 如果我遗漏了任何内容、过分强调或过分强调某个具体点,请在 cmets 中告诉我。

【问题讨论】:

标签: python numpy scikit-learn scipy


【解决方案1】:

要按点对列进行分组(按 x 分组),您可以执行以下操作。

xy = np.array([
    [151,  26],[ 30,  26],[511,  27],[747,  30],[609,  28],[930,  30],
    [ 30,  52],[211,  53],[513,  54],[608,  54],[824,  56],[946,  55],
    [106,  87],[187,  87],[512,  89],[609,  90],[725,  90],[823,  92],
    [931,  92],[ 28, 113],[301, 113],[512, 115],[609, 116],[ 28, 142],
    [107, 141],[220, 142],[511, 143],[724, 145],[823, 146],[937, 146],
    [ 29, 168],[308, 168],[512, 171],[ 28, 197],[107, 197],[205, 198],
    [511, 199],[724, 200],[940, 201],[ 29, 222],[307, 223],[217, 244],
    [107, 273],[ 28, 274],[201, 273],[511, 276],[725, 277],[937, 279],
    [ 28, 299],[273, 301],[218, 321],[ 28, 351],[107, 350],[201, 351],
    [511, 354],[723, 354],[947, 356],[ 29, 376],[297, 377]])

n = np.unique(xy[:,0])
cols = { i: list(xy[xy[:,0]==i,1]) for i in n }

这将创建一个字典,其中包含作为键的列及其对应的 y 值。 如果您想按 y 分组以获得行,那么您只需翻转 0 和 1,您将获得按 y 分组。

然后,要重新打印图表,您可以这样做(可能有更好的方法)

x, y = np.array([]), np.array([])
for i in cols.items():
    x = np.append(x, [i[0]] * len(i[1]))
    y = np.append(y, (i[1]))
plt.scatter(x, y) 
plt.show()

返回原始图形。

【讨论】:

    【解决方案2】:

    您可以使用 defaultdict 将您在列中找到的每个点附加到列表中。

    from collections import defaultdict
    d = defaultdict(list)
    
    for point in array:
       x, _ = point
       d[x].append(point)
    grouped_by_columns = list(d.values())
    

    【讨论】:

      【解决方案3】:

      从零开始

      # Create variables
      grouped = []
      unique = {}
      
      # Get all unique x values and store y as array in dictionary
      for point in points:
          unique[point[0]] = unique.get(point[0], []) + point[1]
      
      # Convert the x's into columns of points
      for x in unique:
          grouped.append([[x, y] for y in unique[x]])
      
      # Extra polishing
      grouped = np.array(grouped)
      

      这会创建一个新的 NumPy 数组:

      data = [columns [points within columns]]
      

      请注意,这不处理重新绘图,因为没有为该部分提供足够的信息,只提供分组。

      【讨论】:

      • 抱歉@larrythellama 我无法正确表达问题
      • @NithinVarghese - 没问题,反正我不是matplotlib 的专家,主要是为那些未来的人准备的。
      猜你喜欢
      • 2017-10-13
      • 2018-12-19
      • 1970-01-01
      • 2021-06-28
      • 2014-11-22
      • 2019-02-19
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多