摆脱多余的2D点,而不丢失顺序

问题描述 投票:0回答:1

我有以下几点:

import numpy as np
points = np.array([[49.8, 66.35],
 [49.79, 66.35],
 [49.79, 66.35],
 [44.65, 67.25],
 [44.65, 67.25],
 [44.65, 67.25],
 [44.48, 67.24],
 [44.63, 67.21],
 [44.68, 67.2],
 [49.69, 66.21],
 [49.85, 66.17],
 [50.51, 66.04],
 [49.8, 66.35]])

当我绘制它们时,我得到这个形状:

import matplotlib.pyplot as plt
x = [a[0] for a in points ]
y = [a[1] for a in points ]
plt.plot(x,y)

enter image description here

从点列表中可以看到,其中有些是多余的(即,看点1和2(从0开始)。]]

为了只保留非冗余点,我回复了这个问题的答案:Removing duplicate columns and rows from a NumPy 2D array

def unique_2D(a):
    order = np.lexsort(a.T)
    a = a[order]
    diff = np.diff(a, axis=0)
    ui = np.ones(len(a), 'bool')
    ui[1:] = (diff != 0).any(axis=1) 
    return a[ui]

我将此功能应用于我的观点,然后得到:

non_redundant_points = unique_2D(points)

这里是保留点的打印列表:

[[ 50.51  66.04]
 [ 49.85  66.17]
 [ 49.69  66.21]
 [ 49.79  66.35]
 [ 49.8   66.35]
 [ 44.68  67.2 ]
 [ 44.63  67.21]
 [ 44.48  67.24]
 [ 44.65  67.25]]

但是,现在我面临以下问题:当我绘制它们时,顺序以某种方式未保留...

x_nr = [a[0] for a in non_redundant_points ]
y_nr = [a[1] for a in non_redundant_points ]
plt.plot(x_nr,y_nr)

enter image description here

您知道我该如何解决吗?

为了简化复制和粘贴,以下是完整代码:

import numpy as np    
import matplotlib.pyplot as plt

points = np.array([[49.8, 66.35],
 [49.79, 66.35],
 [49.79, 66.35],
 [44.65, 67.25],
 [44.65, 67.25],
 [44.65, 67.25],
 [44.48, 67.24],
 [44.63, 67.21],
 [44.68, 67.2],
 [49.69, 66.21],
 [49.85, 66.17],
 [50.51, 66.04],
 [49.8, 66.35]])

x = [a[0] for a in points ]
y = [a[1] for a in points ]
plt.plot(x,y)

def unique_2D(a):
        order = np.lexsort(a.T)
        a = a[order]
        diff = np.diff(a, axis=0)
        ui = np.ones(len(a), 'bool')
        ui[1:] = (diff != 0).any(axis=1) 
        return a[ui]

x_nr = [a[0] for a in non_redundant_points ]
y_nr = [a[1] for a in non_redundant_points ]
plt.plot(x_nr,y_nr)

[我有以下几点:导入numpy作为np点= np.array([[49.8,66.35],[49.79,66.35],[49.79,66.35],[44.65,67.25],[44.65,67.25],[ 44.65,67.25],[44.48,67.24],[44.63,67.21],...

python list numpy matplotlib
1个回答
0
投票

您可以跟踪看到的set中已经存在的点。为此,您可以创建一个允许散列和比较点的类:

© www.soinside.com 2019 - 2024. All rights reserved.