Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quiver or Barb with a date axis

What is the standard way of plotting a timeseries (dates) of quiver or barbs? I often have timeseries in a Pandas DataFrame and plot them like this:

plt.plot(df.index.to_pydatetime(), df.parameter)

This works very well, the x-axis can be treated as genuine dates which is very convenient for formatting or setting the xlim() with Datetime object etc.

Using this with quiver or barbs in the same way result in:

TypeError: float() argument must be a string or a number

This can be overcome with something like:

ax.barbs(df.index.values.astype('d'), np.ones(size) * 6.5, df.U.values, df.V.values, length=8, pivot='middle')
ax.set_xticklabels(df.index.to_pydatetime())

Which works, but would mean that everywhere i have to convert the dates to floats and then manually override the labels. Is there a better way?

Here is some sample code resembling my case:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

size = 10

wspd = np.random.randint(0,40,size=size)
wdir = np.linspace(0,360 * np.pi/180, num=size)
U = -wspd*np.sin(wdir)
V = -wspd*np.cos(wdir)

df = pd.DataFrame(np.vstack([U,V]).T, index=pd.date_range('2012-1-1', periods=size, freq='M'), columns=['U', 'V'])

fig, ax = plt.subplots(1,1, figsize=(15,4))

ax.plot(df.index.values.astype('d'), df.V * 0.1 + 4, color='k')
ax.quiver(df.index.values.astype('d'), np.ones(size) * 3.5, df.U.values, df.V.values, pivot='mid')
ax.barbs(df.index.values.astype('d'), np.ones(size) * 6.5, df.U.values, df.V.values, length=8, pivot='middle')

ax.set_xticklabels(df.index.to_pydatetime())

enter image description here

like image 407
Rutger Kassies Avatar asked Dec 19 '12 10:12

Rutger Kassies


1 Answers

I ended up using the following code:

idx = mpl.dates.date2num(df.index)
ax.xaxis.set_major_formatter(mpl.dates.DateFormatter('%d-%m-%Y'))

ax.plot(idx, df.V * 0.1 + 4, 'o-',color='k')
ax.quiver(idx, np.ones(size) * 3.5, df.U.values, df.V.values, pivot='mid')
ax.barbs(idx, np.ones(size) * 6.5, df.U.values, df.V.values, length=8, pivot='middle')
like image 156
Rutger Kassies Avatar answered Sep 27 '22 23:09

Rutger Kassies