Quiver or barb with date axis

What is the standard way to build timers (dates) for a quiver or thorns? I often have timeouts in a Pandas DataFrame, and lay them out like this:

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

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

Using this with a quiver or spines in the same way, do:

 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 will mean that everywhere I have to convert the dates to float and then manually redefine the labels. Is there a better way?

Here is a sample code similar to 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

+4
source share
2 answers

I would suggest converting your dates to timestamps, and then using special formatting ( doc ) to convert the seconds format to the date of your choice, you probably have to play with the locator ( doc ) to make it look good (in terms of setting intervals / marks).

 import datetime def tmp_f(dt,x=None): return datetime.datetime.fromtimestamp(dt).isoformat() mf = matplotlib.ticker.FuncFormatter(tmp_f) ax = gca() ax.get_xaxis().set_major_formatter(mf) draw() 
+1
source

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') 
+1
source

All Articles