Show Sidebar Hide Sidebar

Quiver Plots in Python

How to make a quiver plot in Python. A quiver plot displays velocity vectors a arrows.

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 Quiver Plot

In [2]:
import plotly.plotly as py
import plotly.figure_factory as ff

import numpy as np

x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y

fig = ff.create_quiver(x, y, u, v)
py.iplot(fig, filename='Quiver Plot Example')
Out[2]:

Quiver Plot with Points

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

import numpy as np

x,y = np.meshgrid(np.arange(-2, 2, .2),
                  np.arange(-2, 2, .25))
z = x*np.exp(-x**2 - y**2)
v, u = np.gradient(z, .2, .2)

# Create quiver figure
fig = ff.create_quiver(x, y, u, v,
                       scale=.25,
                       arrow_scale=.4,
                       name='quiver',
                       line=dict(width=1))

# Create points
points = go.Scatter(x=[-.7, .75], y=[0,0],
                    mode='markers',
                    marker=dict(size=12),
                    name='points')

# Add points to figure
fig['data'].append(points)

py.iplot(fig, filename='Quiver with Points')
Out[2]:

Reference

In [3]:
help(ff.create_quiver)
Help on function create_quiver in module plotly.figure_factory._quiver:

create_quiver(x, y, u, v, scale=0.1, arrow_scale=0.3, angle=0.3490658503988659, **kwargs)
    Returns data for a quiver plot.
    
    :param (list|ndarray) x: x coordinates of the arrow locations
    :param (list|ndarray) y: y coordinates of the arrow locations
    :param (list|ndarray) u: x components of the arrow vectors
    :param (list|ndarray) v: y components of the arrow vectors
    :param (float in [0,1]) scale: scales size of the arrows(ideally to
        avoid overlap). Default = .1
    :param (float in [0,1]) arrow_scale: value multiplied to length of barb
        to get length of arrowhead. Default = .3
    :param (angle in radians) angle: angle of arrowhead. Default = pi/9
    :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 quiver figure.
    
    Example 1: Trivial Quiver
    ```
    import plotly.plotly as py
    from plotly.figure_factory import create_quiver
    
    import math
    
    # 1 Arrow from (0,0) to (1,1)
    fig = create_quiver(x=[0], y=[0], u=[1], v=[1], scale=1)
    
    py.plot(fig, filename='quiver')
    ```
    
    Example 2: Quiver plot using meshgrid
    ```
    import plotly.plotly as py
    from plotly.figure_factory import create_quiver
    
    import numpy as np
    import math
    
    # Add data
    x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
    u = np.cos(x)*y
    v = np.sin(x)*y
    
    #Create quiver
    fig = create_quiver(x, y, u, v)
    
    # Plot
    py.plot(fig, filename='quiver')
    ```
    
    Example 3: Styling the quiver plot
    ```
    import plotly.plotly as py
    from plotly.figure_factory import create_quiver
    import numpy as np
    import math
    
    # Add data
    x, y = np.meshgrid(np.arange(-np.pi, math.pi, .5),
                       np.arange(-math.pi, math.pi, .5))
    u = np.cos(x)*y
    v = np.sin(x)*y
    
    # Create quiver
    fig = create_quiver(x, y, u, v, scale=.2, arrow_scale=.3, angle=math.pi/6,
                        name='Wind Velocity', line=Line(width=1))
    
    # Add title to layout
    fig['layout'].update(title='Quiver Plot')
    
    # Plot
    py.plot(fig, filename='quiver')
    ```

Still need help?
Contact Us

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