8

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.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Upriser
  • 418
  • 2
  • 7
  • 23
  • First look up what the equations are (mathematics), then think of how exactly you want to represent a plane -- maybe some square, or grid... – trincot Mar 29 '22 at 21:16

1 Answers1

15

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

enter image description here

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

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • Is there a way I can do this xx, yy = np.meshgrid(range(10), range(10)) in range of (0 and 1)?? and also visualize vectors and normals? – Upriser Dec 10 '18 at 05:03