I have 3 points in 3D space and I want to define a plane passing through those points in 3D.
X Y Z
0 0.65612 0.53440 0.24175
1 0.62279 0.51946 0.25744
2 0.61216 0.53959 0.26394
Also I need to plot that in 3D space.
You define a plane vectorially with a normal and a point. To find the normal, you calculate the cross product of two of the vectors defined by the three points.
Then you use this normal and one of the points to place the plane in space.
Using matplotlib:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
points = [[0.65612, 0.53440, 0.24175],
[0.62279, 0.51946, 0.25744],
[0.61216, 0.53959, 0.26394]]
p0, p1, p2 = points
x0, y0, z0 = p0
x1, y1, z1 = p1
x2, y2, z2 = p2
ux, uy, uz = u = [x1-x0, y1-y0, z1-z0]
vx, vy, vz = v = [x2-x0, y2-y0, z2-z0]
u_cross_v = [uy*vz-uz*vy, uz*vx-ux*vz, ux*vy-uy*vx]
point = np.array(p0)
normal = np.array(u_cross_v)
d = -point.dot(normal)
xx, yy = np.meshgrid(range(10), range(10))
z = (-normal[0] * xx - normal[1] * yy - d) * 1. / normal[2]
# plot the surface
plt3d = plt.figure().gca(projection='3d')
plt3d.plot_surface(xx, yy, z)
plt.show()
other useful answers:
Matplotlib - Plot a plane and points in 3D simultaneously
Plot a plane based on a normal vector and a point in Matlab or matplotlib
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With