A Line in Space#

In this tutorial, we will learn how to draw a line in 3D space using Plotly in Python. Specifically, we will connect a particle to the origin with a line, building on the previous tutorial where we plotted a particle and the origin.

Plotting a Line in 3D Space#

Step 1: Importing Plotly#

First, we need to import Plotly’s graph objects module, which is used for creating a wide variety of plots, including 3D plots.

[45]:
import plotly.graph_objects as go

Step 2: Define the Points#

Define the coordinates for the two points you want to connect with a line. In this example, we will use points \(A=(1, 2, 3)\) and \(B=(4, 5, 6)\).

[46]:
# Coordinates of Point A and Point B
point_A = [1, 2, 3]
point_B = [4, 5, 6]

Step 3: Create Trace for Line#

Create a trace using go.Scatter3d to represent the line connecting the two points.

[47]:
# Trace for the line
line_trace = go.Scatter3d(
    x=[point_A[0], point_B[0]],
    y=[point_A[1], point_B[1]],
    z=[point_A[2], point_B[2]],
    mode='lines',
    line=dict(color='green', width=2)
)

We are using mode=lines here to make this plot as line. You can see that we have defined the coordinates in x=[point_A[0], point_B[0]] format to specify the starting and end point of the line in \(x\) axis.

Step 4: Define the Layout#

Set up the layout for your 3D plot, including titles and axis ranges.

[48]:
layout = go.Layout(
    title="Line in 3D Space",
    scene=dict(
        xaxis=dict(title='X Axis', range=[0, 10]),
        yaxis=dict(title='Y Axis', range=[0, 10]),
        zaxis=dict(title='Z Axis', range=[0, 10])
    )
)

Step 5: Combine the Trace and Layout#

Create the figure with the line trace and the defined layout. (Rotate the plot to see the line trace)

[49]:
fig = go.Figure(data=[line_trace], layout=layout)
fig.show()

You have now learned how to create a simple line between two points in 3D space using Plotly. This foundational skill is crucial for more complex visualizations in 3D coordinate systems.

Connect Particle and Origin#

Now that you understand how to create a line in 3D space, let’s combine this knowledge with the previous tutorial on plotting a particle and the origin. We’ll connect the particle to the origin with a line, enhancing our 3D visualization.

Step 1: Traces for Origin and Particle#

We will use the origin_trace and particle_trace from the previous tutorial.

[50]:
# Trace for the particle
particle_trace = go.Scatter3d(
    x=[1], y=[2], z=[3],
    mode='markers',
    marker=dict(size=10, color='red'),
    name='Particle'
)

# Trace for the origin
origin_trace = go.Scatter3d(
    x=[0], y=[0], z=[0],
    mode='markers+text',
    marker=dict(size=5, color='blue'),
    name='Origin',
    text=["O"],  # Text labels for point
    textposition="top center",  # Position of the text
    showlegend=False
)

Step 2: Create a Trace for the Connecting Line#

[51]:
# Coordinates of the origin and the particle
origin = [0, 0, 0]
particle = [1, 2, 3]

# Trace for the connecting line
connecting_line_trace = go.Scatter3d(
    x=[origin[0], particle[0]],
    y=[origin[1], particle[1]],
    z=[origin[2], particle[2]],
    mode='lines',
    line=dict(color='green', width=2),
    name='OP'
)

Step 3: Define the Combined Layout#

[52]:
combined_layout = go.Layout(
    title="Particle, Origin, and Connecting Line in 3D Space",
    scene=dict(
        xaxis=dict(title='X Axis', range=[-5, 5]),
        yaxis=dict(title='Y Axis', range=[-5, 5]),
        zaxis=dict(title='Z Axis', range=[-5, 5])
    )
)

Step 4: Combine All Traces and Layout#

[53]:
fig = go.Figure(data=[origin_trace, particle_trace, connecting_line_trace], layout=combined_layout)
fig.show()

This tutorial has shown you how to combine different elements in a 3D Plotly visualization. By connecting a particle to the origin with a line, you can create more informative and visually appealing plots.