I made a program to plot a figure from point cloud data (x,y,z coordinates shown as list x_list, y_list, z_list). Now, I have to find the closest point to (0,0,0). Anybody has an idea to do that? Here is the program:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import math
from mpl_toolkits.mplot3d import axes3d
cloud = np.loadtxt('c1.txt')
rows = len(cloud)
columns = int(len(cloud[0])/5)
x_list = []
y_list = []
z_list = []
for i in range(rows):
for j in range(columns):
x_list.append(cloud[i][j])
y_list.append(cloud[i][j columns])
z_list.append(cloud[i][j 2*columns])
#x_list = x_list[~pd.isnull(x_list)]
X = x_list
Y = y_list
Z = z_list
#Eliminating 'nan' values
newlist_x = [X for X in x_list if math.isnan(X) == False]
newlist_y = [Y for Y in y_list if math.isnan(Y) == False]
newlist_z = [Z for Z in z_list if math.isnan(Z) == False]
display(newlist_x, newlist_y, newlist_z)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.scatter(newlist_x, newlist_y, newlist_z, c=newlist_z, cmap='plasma', linewidth=0.01)
#3D plotting of points
plt.rcParams["figure.figsize"] = (12,15) #making plot more viewable
plt.show()
CodePudding user response:
This should do the trick.
def closest(X, Y, Z, P):
"""
X: x-axis series
Y: y-axis series
Z: z-axis series
P: centre point to measure distance from
Returns: tuple(closest point, distance between P and closest point)
"""
# Here we combine the X, Y, Z series to get a data struct. with all our points (similar to using the builtin's zip())
points = np.column_stack((X, Y, Z))
# Now we compute the distances between each of the points and the desired point
distances = [np.linalg.norm(p) for p in (points - P)]
# Finally we get the index of the smalles value in the array so that we can determine which point is closest and return it
idx_of_min = np.argmin(distances)
return (points[idx_of_min], distances[idx_of_min])
You can call closest
as such:
X = [-1, -6, 8, 1, -2, -5, -1, 5, -3, -10, 6, 3, -4, 9, -5, -2, 4, -1, 3, 0]
Y = [-2, -1, -9, -6, -8, 7, 9, -2, -9, -9, 0, -2, -2, -3, 6, -5, -1, 3, 8, -5]
Z = [-1, 2, 3, 2, 6, -2, 9, 3, -10, 4, -6, 9, 8, 3, 3, -6, 4, 1, -10, 1]
closest_to_point(X, Y, Z, (0, 0, -100))
An example output looks like this:
(array([ 3, 8, -10]), 90.40464589831653)