Show Sidebar Hide Sidebar

Streamline Plots in Python

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

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Plotly's python API is updated frequesntly. Run pip install plotly --upgrade to update your Plotly version.

In [1]:
import plotly
plotly.__version__
Out[1]:
'2.0.2'

Basic Streamline Plot

In [2]:
import plotly.plotly as py
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)
py.iplot(fig, filename='Streamline Plot Example')
/usr/local/lib/python2.7/site-packages/plotly/figure_factory/_streamline.py:357: RuntimeWarning:

invalid value encountered in divide

Out[2]:

Streamline and Source Point Plot

In [3]:
import plotly.plotly as py
import plotly.figure_factory as ff
import plotly.graph_objs 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
source_point = go.Scatter(x=[x_source], y=[y_source],
                          mode='markers',
                          marker=go.Marker(size=14),
                          name='source point')

# Add source point to figure
fig['data'].append(source_point)
py.iplot(fig, filename='streamline_source')
Out[3]:

Reference

In [4]:
help(ff.create_streamline)
Help on function create_streamline in module plotly.figure_factory._streamline:

create_streamline(x, y, u, v, density=1, angle=0.3490658503988659, arrow_scale=0.09, **kwargs)
    Returns data for a streamline plot.
    
    :param (list|ndarray) x: 1 dimensional, evenly spaced list or array
    :param (list|ndarray) y: 1 dimensional, evenly spaced list or array
    :param (ndarray) u: 2 dimensional array
    :param (ndarray) v: 2 dimensional array
    :param (float|int) density: controls the density of streamlines in
        plot. This is multiplied by 30 to scale similiarly to other
        available streamline functions such as matplotlib.
        Default = 1
    :param (angle in radians) angle: angle of arrowhead. Default = pi/9
    :param (float in [0,1]) arrow_scale: value to scale length of arrowhead
        Default = .09
    :param kwargs: kwargs passed through plotly.graph_objs.Scatter
        for more information on valid kwargs call
        help(plotly.graph_objs.Scatter)
    
    :rtype (dict): returns a representation of streamline figure.
    
    Example 1: Plot simple streamline and increase arrow size
    ```
    import plotly.plotly as py
    from plotly.figure_factory import create_streamline
    
    import numpy as np
    import math
    
    # Add data
    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
    u = u.T  # Transpose
    v = v.T  # Transpose
    
    # Create streamline
    fig = create_streamline(x, y, u, v, arrow_scale=.1)
    
    # Plot
    py.plot(fig, filename='streamline')
    ```
    
    Example 2: from nbviewer.ipython.org/github/barbagroup/AeroPython
    ```
    import plotly.plotly as py
    from plotly.figure_factory import create_streamline
    
    import numpy as np
    import math
    
    # Add data
    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)
    ss = 5.0
    x_s, y_s = -1.0, 0.0
    
    # Compute the velocity field on the mesh grid
    u_s = ss/(2*np.pi) * (X-x_s)/((X-x_s)**2 + (Y-y_s)**2)
    v_s = ss/(2*np.pi) * (Y-y_s)/((X-x_s)**2 + (Y-y_s)**2)
    
    # Create streamline
    fig = create_streamline(x, y, u_s, v_s, density=2, name='streamline')
    
    # Add source point
    point = Scatter(x=[x_s], y=[y_s], mode='markers',
                    marker=Marker(size=14), name='source point')
    
    # Plot
    fig['data'].append(point)
    py.plot(fig, filename='streamline')
    ```

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.