This short post consists of some notes on how to plot a stick plot with matplotlib. The function I used below is mostly based on this message from the mailing list.
The main differences are:
Plot customization via
**kw
.Ensure that the user does not change the
angles
option to avoid breaking the stick plot.Create a new figure or attach the stick plot to an existing
axis
.
In [3]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import date2num
def stick_plot(time, u, v, **kw):
width = kw.pop('width', 0.002)
headwidth = kw.pop('headwidth', 0)
headlength = kw.pop('headlength', 0)
headaxislength = kw.pop('headaxislength', 0)
angles = kw.pop('angles', 'uv')
ax = kw.pop('ax', None)
if angles != 'uv':
raise AssertionError("Stickplot angles must be 'uv' so that"
"if *U*==*V* the angle of the arrow on"
"the plot is 45 degrees CCW from the *x*-axis.")
time, u, v = map(np.asanyarray, (time, u, v))
if not ax:
fig, ax = plt.subplots()
q = ax.quiver(date2num(time), [[0]*len(time)], u, v,
angles='uv', width=width, headwidth=headwidth,
headlength=headlength, headaxislength=headaxislength,
**kw)
ax.axes.get_yaxis().set_visible(False)
ax.xaxis_date()
return q
And here is how to use it.
In [4]:
from datetime import datetime, timedelta
x = np.arange(100, 110, 0.1)
start = datetime.now()
time = [start + timedelta(days=n) for n in range(len(x))]
u, v = np.sin(x), np.cos(x)
In [5]:
q = stick_plot(time, u, v)
ref = 1
qk = plt.quiverkey(q, 0.1, 0.85, ref,
"%s N m$^{-2}$" % ref,
labelpos='N', coordinates='axes')
_ = plt.xticks(rotation=70)
In [6]:
fig, ax = plt.subplots(figsize=(11, 2.75))
q = stick_plot(time, u, v, ax=ax, width=0.002, color='green')
ref = 1
qk = ax.quiverkey(q, 0.1, 0.85, ref,
"%s N m$^{-2}$" % ref,
labelpos='N', coordinates='axes')
In [7]:
q = stick_plot(time, u, v, angles='xy')
ref = 1
qk = ax.quiverkey(q, 0.1, 0.85, ref,
"%s N m$^{-2}$" % ref,
labelpos='N', coordinates='axes')
_ = plt.xticks(rotation=70)
In [8]:
HTML(html)
Out[8]: