Skip to content
Advertisement

How to plot a line in 3 dimensional space with matplotlib

I have two 3D-points, for example a = (100, 100, 10) and b = (0, 100, 60), and would like to fit a line through those points. I know, the 3D line equation can have different shapes:

Vector-form:

(x,y,z)=(x0,y0,z0)+t(a,b,c)

Parameter-form:

x=x0+ta
y=y0+tb
z=z0+tc

But I have a problem getting the data in the right shape for a numerical function.

Advertisement

Answer

The following code should work

import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.axes(projection ='3d')
 
# defining coordinates for the 2 points.
x = np.array([100, 0])
y = np.array([100, 100])
z = np.array([10, 60])
 
# plotting
ax.plot3D(x, y, z)
plt.show()

Here the ax.plot3D() plots a curve that joins the points (x[i], y[i], z[i]) with straight lines.

Advertisement