Python 使用日期时间数据在Matplotlib中打印矩形

Python 使用日期时间数据在Matplotlib中打印矩形,python,pandas,datetime,matplotlib,Python,Pandas,Datetime,Matplotlib,我有以下问题:我有一个带有日期时间索引的数据框,列包含浮点值或布尔值,如下所示: TAG_2552 TAG_2526 timestamp 2018-06-17 14:41:48 50.0 True 2018-06-17 14:41:49 51.0 True 2018-06-17 14:41:50 52.0 True 2018-06-17 14:41:51 49.0 True

我有以下问题:我有一个带有日期时间索引的数据框,列包含浮点值或布尔值,如下所示:

                     TAG_2552  TAG_2526
timestamp
2018-06-17 14:41:48      50.0      True
2018-06-17 14:41:49      51.0      True
2018-06-17 14:41:50      52.0      True
2018-06-17 14:41:51      49.0      True
2018-06-17 14:41:52      55.0      False
2018-06-17 14:41:53      50.0      False
2018-06-17 14:41:54      48.0      False
2018-06-17 14:41:55      54.0      False
2018-06-17 14:41:56      53.0      True
2018-06-17 14:41:57      50.0      True
我想画出来,这对浮点值来说不是问题。对于布尔型,只要它们是
真的
,我就会画矩形。我试着用它来做

我试过了

df['datetime']=df.index
collection = collections.BrokenBarHCollection.span_where(df['datetime'].dt.date, ymin=0, ymax=1, where=df['TAG_2526']==True, facecolor='green', alpha=.5)
(和其他变体)但这不起作用

可以使用创建集合

df['timedelta'] = df['datetime']-df['datetime'][0]
collection2 = collections.BrokenBarHCollection.span_where(myProc9['timedelta'].astype('timedelta64[ns]')/np.timedelta64(1,'s'), ymin=0, ymax=1, where=myProc9['TAG_2503']==True, facecolor='green', alpha=.5)
但由于我想根据原始日期时间绘图,因为我想共享x轴,所以我能看到的唯一选项是根据
myProc9['timedelta'].astype('timedelta64[ns]')/np.timedelta64(1,'s')
绘制所有内容,并重新标记x轴

我查看了关于更改日期时间格式的不同页面,但是
matplotlib.collections
似乎没有接受任何内容,通常的错误消息是

TypeError: float() argument must be a string or a number, not 'datetime.date'
(或者我尝试过的其他方法,有关datetime格式之间的各种转换,请参见此处:)


我想知道是否有更好的方法(不使用
matplotlib.collections
)或不同的调用方式?

我正要删除这个问题,但也许有一天它会对某些人有用

ax.fill\u介于(df.index,0,1,其中=df['TAG\u 2526'])之间

做这项工作,似乎相当慷慨的时间戳

链接: