在Matplotlib中用日期时间d绘制矩形

2024-10-02 22:26:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下问题:我有一个带有日期时间索引的dataframe,其中的列包含float或boolean值,如下所示:

                     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

我想把它画出来,这对浮点值来说不是问题。对于布尔人,我想在True的时候画矩形。我试着用matplotlib.collections来这么做。在

我试过了

^{2}$

(和其他变体)但这不起作用。在

可以使用创建集合

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'

(或者我尝试过的其他方法,对于日期时间格式之间的各种转换,请参见此处:Converting between datetime, Timestamp and datetime64

我想知道有没有更好的方法(不要使用matplotlib.collections)或者用不同的方法来调用它?在


Tags: 方法falsetruedfdatetimematplotlibtag时间