3D Streamtube plots are an elegant way to visualize vector flow data in three dimensions. They are commonly used in fields such as fluid dynamics, aerodynamics, and meteorology to represent the flow of a vector field. The tubes follow the paths of particles as they move through a vector field, and their size can indicate the strength of the flow. With Plotly, a popular Python data visualization library, you can easily create interactive and stunning 3D streamtube plots.
In this guide, we will explore how to create 3D streamtube plots using Plotly in Python. We will go over the necessary steps to install Plotly, understand the concept of streamtube plots, and create a detailed example with explanations.
What is a 3D Streamtube Plot?
A 3D streamtube plot is a graphical representation of the trajectories of particles moving through a vector field. The tubes follow the direction of the flow, and their radius can represent the magnitude of the vector field at various points. These plots are incredibly useful in physics, fluid dynamics, and engineering, where vector fields need to be analyzed for understanding flow behavior.
Why Use Plotly for 3D Streamtube Plots?
Plotly is an interactive visualization library that supports a variety of charts, including 3D plots. It’s known for its simplicity in creating high-quality, interactive visualizations with minimal code. Some key benefits of using Plotly include:
- Interactivity: Hover over the data points to get more details.
- Customization: You can easily modify colors, labels, and other aesthetic properties.
- Compatibility: Plotly integrates seamlessly with Jupyter notebooks, web applications, and more.
Installing Plotly
Before we get started, you need to have Plotly installed in your Python environment. You can install it using pip:
pip install plotly
This will install Plotly and its dependencies so you can use it in your projects.
Creating a 3D Streamtube Plot with Plotly
Now, let’s walk through the process of creating a 3D streamtube plot. We’ll generate some example data representing a vector field and then use Plotly to visualize it.
Step 1: Import Required Libraries
Start by importing the necessary libraries. We’ll be using NumPy to generate some data and Plotly to create the visualization.
import numpy as np
import plotly.graph_objects as go
numpy: Used for generating the vector field data.plotly.graph_objects: The primary module for creating Plotly figures, including 3D streamtube plots.
Step 2: Generate Vector Field Data
To create a streamtube plot, you need three sets of 3D coordinates and their corresponding vector components. For simplicity, we’ll generate a vector field that simulates the flow of particles in a swirling motion.
# Generate the 3D grid points
x, y, z = np.mgrid[-1:1:20j, -1:1:20j, -1:1:20j]
# Define the vector field components
u = -y * z
v = x * z
w = 0.1 * (x**2 + y**2)
x, y, and z represent the 3D coordinates of the grid points where the flow is evaluated.u, v, and w are the components of the vector field at each point (x, y, z). In this case, we’ve generated a swirling flow pattern where the velocity components depend on the position in the grid.
Step 3: Create the 3D Streamtube Plot
Now that we have our vector field data, we can create the streamtube plot using Plotly.
# Create the streamtube plot
streamtube = go.Streamtube(
x=x.flatten(),
y=y.flatten(),
z=z.flatten(),
u=u.flatten(),
v=v.flatten(),
w=w.flatten(),
starts=dict(x=[-0.5, 0.5], y=[-0.5, 0.5], z=[-0.5, 0.5]),
sizeref=0.5,
colorscale='Viridis',
showscale=True
)
# Create the layout for the plot
layout = go.Layout(
title="3D Streamtube Plot of a Vector Field",
scene=dict(
xaxis_title='X',
yaxis_title='Y',
zaxis_title='Z'
)
)
# Create the figure and display it
fig = go.Figure(data=streamtube, layout=layout)
fig.show()
Full Source Code:
import numpy as np
import plotly.graph_objects as go
# Generate the 3D grid points
x, y, z = np.mgrid[-1:1:20j, -1:1:20j, -1:1:20j]
# Define the vector field components
u = -y * z
v = x * z
w = 0.1 * (x**2 + y**2)
# Create the streamtube plot
streamtube = go.Streamtube(
x=x.flatten(),
y=y.flatten(),
z=z.flatten(),
u=u.flatten(),
v=v.flatten(),
w=w.flatten(),
starts=dict(x=[-0.5, 0.5], y=[-0.5, 0.5], z=[-0.5, 0.5]),
sizeref=0.5,
colorscale='Viridis',
showscale=True
)
# Create the layout for the plot
layout = go.Layout(
title="3D Streamtube Plot of a Vector Field",
scene=dict(
xaxis_title='X',
yaxis_title='Y',
zaxis_title='Z'
)
)
# Create the figure and display it
fig = go.Figure(data=streamtube, layout=layout)
fig.show()
Output:

Let’s break down the code above:
go.Streamtube()is used to create the streamtube plot. It takes the flattened 3D coordinates (x,y,z) and vector components (u,v,w) as inputs.startsdefines the starting points for the streamtubes. This is optional but helps control where the tubes begin in the plot.sizerefcontrols the size of the tubes relative to the magnitude of the vector field.colorscaleis used to define the color map for the plot. In this case, we are using the ‘Viridis’ color scale, but Plotly supports many other options.layoutdefines the plot’s title and axis labels.- Finally,
fig.show()renders the plot in a web browser or Jupyter notebook.
Step 4: Interactive Visualization
Once you run the code, an interactive 3D plot will appear. You can zoom in, rotate, and hover over different parts of the streamtube to get detailed information about the flow at each point. The tubes will be colored according to the magnitude of the vector field, providing a clear visual representation of the flow’s strength and direction.
Customizing the 3D Streamtube Plot
You can easily customize the streamtube plot by modifying the properties passed to go.Streamtube() and go.Layout(). Here are a few ways you can customize your plot:
- Starting Points: You can specify more complex starting points for the streamtubes by passing arrays of values to the
startsparameter. - Tube Size: Adjust the
sizerefparameter to make the tubes larger or smaller depending on the magnitude of the vector field. - Color Scale: Change the
colorscaleto other predefined options such as'Cividis','Plasma', or'Inferno', or create your own custom color scale. - Vector Field: You can generate more complex vector fields by changing how you compute the
u,v, andwcomponents.
Example: Customizing with a Different Color Scale
Let’s modify the color scale and starting points of the streamtube plot:
streamtube = go.Streamtube(
x=x.flatten(),
y=y.flatten(),
z=z.flatten(),
u=u.flatten(),
v=v.flatten(),
w=w.flatten(),
starts=dict(x=[-0.5, 0.5, 0], y=[-0.5, 0.5, 0], z=[-0.5, 0.5, 0]),
sizeref=0.3,
colorscale='Cividis',
showscale=True
)
Modified Code:
import numpy as np
import plotly.graph_objects as go
# Generate the 3D grid points
x, y, z = np.mgrid[-1:1:20j, -1:1:20j, -1:1:20j]
# Define the vector field components
u = -y * z
v = x * z
w = 0.1 * (x**2 + y**2)
streamtube = go.Streamtube(
x=x.flatten(),
y=y.flatten(),
z=z.flatten(),
u=u.flatten(),
v=v.flatten(),
w=w.flatten(),
starts=dict(x=[-0.5, 0.5, 0], y=[-0.5, 0.5, 0], z=[-0.5, 0.5, 0]),
sizeref=0.3,
colorscale='Cividis',
showscale=True
)
# Create the layout for the plot
layout = go.Layout(
title="3D Streamtube Plot of a Vector Field",
scene=dict(
xaxis_title='X',
yaxis_title='Y',
zaxis_title='Z'
)
)
# Create the figure and display it
fig = go.Figure(data=streamtube, layout=layout)
fig.show()
Output:

This code changes the starting points to begin at three positions and switches the color scale to 'Cividis'. You can experiment with different starting positions, tube sizes, and color scales to create unique visualizations.
Applications of 3D Streamtube Plots
3D streamtube plots are invaluable for various applications, including:
- Fluid Dynamics: Visualizing airflow around objects such as wings or buildings.
- Meteorology: Understanding wind patterns in three dimensions.
- Engineering: Simulating the flow of fluids through pipes or over surfaces.
- Physics: Studying magnetic or electric fields in space.
These plots allow scientists, engineers, and researchers to gain insights into complex systems where understanding vector fields is crucial.
Conclusion
In this guide, we explored the creation of 3D streamtube plots using Plotly in Python. We covered the basic steps of setting up a 3D grid, defining a vector field, and visualizing it with streamtubes. Plotly makes it incredibly easy to create interactive and customizable 3D visualizations, which are not only visually appealing but also highly informative.
By understanding the principles behind 3D streamtube plots, you can apply these techniques to a wide range of scientific and engineering problems, making it an essential tool for visualizing vector fields in Python. Now that you’ve seen how simple it is to create these plots, you can experiment with more complex vector fields, customize the appearance of the tubes, and gain deeper insights into the behavior of your data.





Leave a Reply