Streamline Plots in Python

How to make a streamline plot in Python. A streamline plot displays vector field data.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.

A Streamline plot is a representation based on a 2-D vector field interpreted as a velocity field, consisting of closed curves tangent to the velocity field. In the case of a stationary velocity field, streamlines coincide with trajectories (see also the Wikipedia page on streamlines, streaklines and pathlines).

For the streamline figure factory, one needs to provide

  • uniformly spaced ranges of x and y values (1D)
  • 2-D velocity values u and v defined on the cross-product (np.meshgrid(x, y)) of x and y.

Velocity values are interpolated when determining the streamlines. Streamlines are initialized on the boundary of the x-y domain.

Basic Streamline Plot

Streamline plots can be made with a figure factory as detailed in this page.

In [1]:
import plotly.figure_factory as ff

import numpy as np

x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
Y, X = np.meshgrid(x, y)
u = -1 - X**2 + Y
v = 1 + X - Y**2

# Create streamline figure
fig = ff.create_streamline(x, y, u, v, arrow_scale=.1)
fig.show()
−3−2−1012−3−2−10123

Streamline and Source Point Plot

In [2]:
import plotly.figure_factory as ff
import plotly.graph_objects as go

import numpy as np

N = 50
x_start, x_end = -2.0, 2.0
y_start, y_end = -1.0, 1.0
x = np.linspace(x_start, x_end, N)
y = np.linspace(y_start, y_end, N)
X, Y = np.meshgrid(x, y)
source_strength = 5.0
x_source, y_source = -1.0, 0.0

# Compute the velocity field on the mesh grid
u = (source_strength/(2*np.pi) *
     (X - x_source)/((X - x_source)**2 + (Y - y_source)**2))
v = (source_strength/(2*np.pi) *
     (Y - y_source)/((X - x_source)**2 + (Y - y_source)**2))

# Create streamline figure
fig = ff.create_streamline(x, y, u, v,
                           name='streamline')

# Add source point
fig.add_trace(go.Scatter(x=[x_source], y=[y_source],
                          mode='markers',
                          marker_size=14,
                          name='source point'))

fig.show()
−2−1.5−1−0.500.511.5−1−0.500.51
streamlinesource point

See also

For a 3D version of streamlines, use the trace go.Streamtube documented here.

For representing the 2-D vector field as arrows, see the quiver plot tutorial.

Reference

For more info on ff.create_streamline(), see the full function reference

What About Dash?

Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash at https://dash.plot.ly/installation.

Everywhere in this page that you see fig.show(), you can display the same figure in a Dash application by passing it to the figure argument of the Graph component from the built-in dash_core_components package like this:

import plotly.graph_objects as go # or plotly.express as px
fig = go.Figure() # or any Plotly Express function e.g. px.bar(...)
# fig.add_trace( ... )
# fig.update_layout( ... )

from dash import Dash, dcc, html

app = Dash()
app.layout = html.Div([
    dcc.Graph(figure=fig)
])

app.run(debug=True, use_reloader=False)  # Turn off reloader if inside Jupyter