Search code examples
pythonplot3dplotlystreamlit

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.


Solution

  • 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