Search code examples
pythonmatplotlibmatplotlib-3d

How can I make a 3D line plot?


I want to generate the lines, which I get from an array in 3D.

Here is the code:

VecStart_x = [0,1,3,5]
VecStart_y = [2,2,5,5]
VecStart_z = [0,1,1,5]
VecEnd_x = [1,2,-1,6]
VecEnd_y = [3,1,-2,7]
VecEnd_z  =[1,0,4,9]

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot([VecStart_x ,VecEnd_x],[VecStart_y,VecEnd_y],[VecStart_z,VecEnd_z])
plt.show()
Axes3D.plot()

I get that error:

ValueError: third arg must be a format string


Solution

  • I guess, you want to plot 4 lines. Then you can try

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    for i in range(4):
        ax.plot([VecStart_x[i], VecEnd_x[i]], [VecStart_y[i],VecEnd_y[i]],zs=[VecStart_z[i],VecEnd_z[i]])
    

    As Nicolas has suggested, do have a look at the matplotlib gallery.

    enter image description here