简体   繁体   English

如何将坐标数组排序为列坐标组?

[英]How to sort array of coordinates to groups of column coordinates?

Have an array of point which are plotted as below.有一个点数组,绘制如下。 There are column structures in the set of point.点集中有列结构。 在此处输入图像描述

How can these points be grouped into different columns?如何将这些点分组到不同的列中?

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]])

Expected output is预期 output 是

在此处输入图像描述

where the line is representing each group of coordinates which visually forms a column.其中线代表每组坐标,其中 forms 可视为一列。

Thanks in advance for your time – if I've missed out anything, over- or under-emphasized a specific point let me know in the comments.提前感谢您的宝贵时间——如果我遗漏了任何内容、过分强调或过分强调某个具体点,请在评论中告诉我。

From Scratch从头开始

# 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)

This creates a new NumPy array:这将创建一个新的 NumPy 数组:

data = [columns [points within columns]]

Note that this does not handle the re-graphing because not enough information was provided for that section, only the splitting into groups.请注意,这不处理重新绘制图表,因为没有为该部分提供足够的信息,仅提供分组。

You can use a defaultdict to append to a list for each point you find in a column.您可以使用 append 的默认字典来列出您在列中找到的每个点。

from collections import defaultdict
d = defaultdict(list)

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

To group the columns by points (grouping by x), you can do something like this.要按点对列进行分组(按 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 }

This creates a dictionary containing column as keys and its corresponding y values.这将创建一个字典,其中包含作为键的列及其对应的 y 值。 If you want to group by y to get rows then you can simply flip the 0's and 1's and you'll get group by y.如果您想按 y 分组以获得行,那么您只需翻转 0 和 1,您将获得按 y 分组。

Then, to re-print the graph, you can do this (There is probably a better way of doing this)然后,要重新打印图表,您可以这样做(可能有更好的方法)

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

Which gives back the original graph.这会返回原始图表。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM