A Particle in Space#

In this tutorial, we’ll go through the steps to plot a single particle in a 3D coordinate system using Plotly in Python.

Show a Particle#

First we’ll learn how to display a point in 3D space with \(x\), \(y\), \(z\) coordinates.

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.

[63]:
import plotly.graph_objects as go

Step 2: Set the Coordinates#

Plotly’s go.Scatter3d function is used for creating 3D scatter plots. To plot a single particle, we will use this function with a single point’s coordinates. \((1, 2, 3)\)

  • mode = 'markers' specifies that we are plotting points (or markers).

  • x, y, and z are lists containing the coordinates of the particle. Since we’re plotting a single particle, each list has only one element.

[64]:
# Coordinates of the particle
x, y, z = [1], [2], [3]

# Create a 3D scatter plot
fig = go.Figure(data=[go.Scatter3d(
    x=x,
    y=y,
    z=z,
    mode='markers',
    marker=dict(
        size=10, # set the size of the particle
        color='red',  # set the color of the particle
    )
)])

Step 3: Set titles and labels#

[65]:
fig.update_layout(
    title="Plotting a Particle in 3D Space",
    scene = dict(
        xaxis_title='X Axis',
        yaxis_title='Y Axis',
        zaxis_title='Z Axis'
    )
)

# Show the plot
fig.show()

Congratulations! You’ve successfully plotted a particle in 3D space using Plotly. This fundamental skill is a stepping stone to more complex 3D visualizations, such as plotting trajectories, vectors, or mechanical components in 3D space.

Adding an Origin#

After plotting a single particle in 3D space, the next step is to enhance our visualization by adding an origin. This will help us better understand the position of the particle in relation to a central reference point. Let’s continue our tutorial by adding the origin to our 3D plot.

Step 4: Adding the Origin#

We’ll add the origin \((0, 0, 0)\) as another particle on our plot, but with a different color to distinguish it. We’ll also slightly adjust our previous code to include this new point.

We’ll create separate traces for each point and customize our plot further. This approach provides more flexibility and clarity, especially when dealing with multiple elements in a plot.

We’ll create two different traces – one for the particle and another for the origin. Each trace will have its own characteristics like color and name.

[66]:
# Trace for the particle
dot_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
)

We created two separate Scatter3d objects, dot_trace and origin_trace, each representing a point in our 3D space.

text=["O"] will add the text over the origin and you need to change the mode as mode='markers+text' to show it. As well as, showlegend=False will hide the Origin from our legend.

Step 5: Defining the Layout#

We will define a layout for our plot to customize the appearance, including titles and axis labels.

[67]:
layout = go.Layout(
    title="Plotting a Particle and the Origin in 3D Space with Separate Traces",
    scene=dict(
        xaxis_title='X Axis',
        yaxis_title='Y Axis',
        zaxis_title='Z Axis'
    )
)

Note

Don’t confuse with fig.update_layout() and layout = go.Layout(). We can define the layout in a separate variable and add this in the plot later with go.Layout(). fig.update_layout() can be used to directly update the layout for the graph object.

Step 6: Combining Traces and Layout#

[68]:
fig = go.Figure(data=[dot_trace, origin_trace], layout=layout)

# Show the plot
fig.show()

But, in above plot you may not able to see the origin clearly. Let’s adjust the range for each axis.

Step 7: Set Axis Range#

[69]:
# Updated layout with axis ranges
layout = go.Layout(
    title="Plotting a Particle and the Origin in 3D Space",
    scene=dict(
        xaxis=dict(title='X Axis', range=[-5, 5]),  # Setting range for X axis
        yaxis=dict(title='Y Axis', range=[-5, 5]),  # Setting range for Y axis
        zaxis=dict(title='Z Axis', range=[-5, 5])   # Setting range for Z axis
    )
)

# Combining traces and updated layout into a figure
fig = go.Figure(data=[dot_trace, origin_trace], layout=layout)

# Show the plot
fig.show()

In this updated code, the range property is added to each axis within the scene dictionary of the layout. The range is defined as \([-5, 5]\) for all three axes (\(x\), \(y\), and \(z\)), but you can adjust these values based on your specific requirements. This setting ensures that the plot will focus on the specified range, making it easier to view and interpret the points within the defined space.

With this approach, you’ve learned how to create and manipulate separate traces and combine them with a custom layout in Plotly. This method is particularly useful for complex plots where you need to manage multiple elements with distinct properties.