Plotly Draw a 3d line between two points

My question is clear. I want to draw 3d line between two points. I have a set of points i_list = [(0,0,0), (5,5,5), (10,10,10)] j_list = [(20,20,20), (25,25,25), (30,30,30)]

I want to draw a line between (0,0,0) and (20,20,20). Also, another line between (5,5,5) and (25,25,25). Last line between (10,10,10) and (30,30,30). how can i do that with python and plotly.

Upvotes: 0

Views: 1399

Answers (2)

Derek O
Derek O

Reputation: 19610

You can iterate through your list of starting and ending coordinates, and add each pair of points as a go.Scatter3d trace:

import plotly.graph_objects as go

i_list = [(0,0,0), (5,5,5), (10,10,10)] 
j_list = [(20,20,20), (25,25,25), (30,30,30)]

fig = go.Figure()
for start, end in zip(i_list, j_list):
    fig.add_trace(go.Scatter3d(
        x=[start[0], end[0]], 
        y=[start[1], end[1]], 
        z=[start[2], end[2]],
        mode='lines'
    ))

fig.show()

enter image description here

Upvotes: 0

darren
darren

Reputation: 5744

If it helps, it is possible to do this relatively simply with matplotlib without the need for plotly. All the user has to do is identify the points correctly and it's done...

here is an example for which solves the question above:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111, projection='3d')

# lists of points
i_list = [(0,0,0), (5,5,5), (10,10,10)]
j_list = [(20,20,20), (25,25,25), (30,30,30)]


for i in range(3):
    # example of (0,0,0) -> (20,20,20)
    # example of (5,5,5) -> (25,25,25)
    # example of (10,10,10) -> (30,30,30)
    xs = [i_list[i][0],j_list[i][0]]
    ys = [i_list[i][1],j_list[i][1]]
    zs = [i_list[i][2],j_list[i][2]]

    # Plot contour curves
    ax.plot(xs, ys, zs)


plt.show()

The result is this:

enter image description here

Upvotes: 0

Related Questions