Show Sidebar Hide Sidebar

Quiver Plots in matplotlib

How to make a quiver plot in Matplotlib Python. A quiver plot displays velocity vectors as 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 package is updated frequently. Run pip install plotly --upgrade to use the latest version.

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

Simple Quiver Plot With Points

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

import numpy as np
import matplotlib.pyplot as plt

x=[-.7, .75]
y=[0, 0]
plt.scatter(x,y, marker='o', color="r")
plt.title('Quiver Plot with Points')
fig = plt.gcf()

plotly_fig = tls.mpl_to_plotly(fig)


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)

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

quiver_fig.add_trace(plotly_fig['data'][0])
py.iplot(quiver_fig, filename='mpl-quiver-with-points')
Out[2]:

Quiver Plot With Custom Arrow Size And Color

In [5]:
import plotly.plotly as py
import plotly.tools as tls
from plotly import figure_factory as ff

import numpy as np
import matplotlib.pyplot as plt

x=[-.7, .75]
y=[0, 0]
plt.scatter(x,y, marker='o', color="r")
plt.title('Quiver Plot with Custom Arrow Size')
fig = plt.gcf()

plotly_fig = tls.mpl_to_plotly(fig)


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)

quiver_fig = ff.create_quiver(x, y, u, v,
                       scale=.25,
                       arrow_scale=.8, # Sets arrow scale
                       name='quiver',
                       angle=np.pi/4,
                       line=dict(width=1))

quiver_fig.add_trace(plotly_fig['data'][0])
quiver_fig['data'][0]['line']['color'] = 'rgb(0,255,0)'

py.iplot(quiver_fig, filename='mpl-quiver-with-custom-arrow-size')
Out[5]:

Reference

See https://plot.ly/python/reference/ for more information and chart attribute options!

Still need help?
Contact Us

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