I have the following points:
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]])
When I plot them, I get this shape:
import matplotlib.pyplot as plt
x = [a[0] for a in points ]
y = [a[1] for a in points ]
plt.plot(x,y)
As you can see from the list of points, some of them are redundant (i.e look at point 1 and 2 (starting at 0)).
To only keep the non-redundant points, I reverted to the answer from this question: 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]
I apply this function to my points and I get:
non_redundant_points = unique_2D(points)
Here is the printed list of retained 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]]
However, now I face the following issue: When I plot them, the order is somehow not preserved...
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)
Do you know how I could solve this?
For easier copy and paste, here is the full code:
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)