简体   繁体   English

熊猫在不同水平上绘制持续时间

[英]pandas plotting duration with different levels

I have some data like this 我有一些这样的数据

    level   ts
0   1.0 2014-11-27 16:00:28.211950-08:00
1   1.5 2014-12-04 17:54:56.185200-08:00
2   2.1 2014-12-04 17:59:54.303900-08:00
3   2.6 2014-12-04 18:04:54.307770-08:00
4   1.8 2014-12-04 18:09:55.917890-08:00
5   1.1 2014-12-04 18:14:54.977890-08:00
6   1.0 2014-12-04 18:19:56.729060-08:00

I want to generate a plot where the Y-axis is the level, X-axis is the time within the week (assume the overall range of the x-axis is a week, and project the timestamp onto the specific location). 我想生成一个图,其中Y轴是水平,X轴是一周中的时间(假设X轴的整个范围是一周,然后将时间戳投影到特定位置)。 I want to draw lines (or boxes) where, for example, there is a horizontal line from 2014-12-04 17:54:56.185200-08:00 to 2014-12-04 17:59:54.303900-08:00 with a level of 1.5 , another horizontal line from 2014-12-04 17:59:54.303900-08:00 to 2014-12-04 18:04:54.307770-08:00 at the level of 2.1 , etc. Of course, here the timestamps need to be converted to be the values within the week. 我想画线(或框),例如,从2014-12-04 17:54:56.185200-08:002014-12-04 17:59:54.303900-08:002014-12-04 17:59:54.303900-08:001.5水平,从2014-12-04 17:59:54.303900-08:002014-12-04 18:04:54.307770-08:00另一条水平线,以2.1等,当然,这里时间戳需要转换为一周内的值。

How can I do that? 我怎样才能做到这一点?

I am still not certain of what exactly you want to achieve, I'm going to take a wild guess that you want to plot x-axis with labels of weekday + hour + minute , which can be achieved by adding new columns and setting multiple indexes, something like this: 我仍然不确定您要实现的目标,我将做出一个大胆的猜测,即您想使用工作日+小时+分钟的标签来绘制x轴 ,这可以通过添加新列并设置多个索引,如下所示:

import pandas as pd

df
   level                         ts
0    1.0 2014-11-28 00:00:28.211950
1    1.5 2014-12-05 01:54:56.185200
2    2.1 2014-12-05 01:59:54.303900
3    2.6 2014-12-05 02:04:54.307770
4    1.8 2014-12-05 02:09:55.917890
5    1.1 2014-12-05 02:14:54.977890
6    1.0 2014-12-05 02:19:56.729060

df['weekday'] = pd.DatetimeIndex(df['ts']).weekday
# you may use time if you just need timestamp
# df['time'] = pd.DatetimeIndex(df['ts']).time
df['hour'] = pd.DatetimeIndex(df['ts']).hour
df['minute'] = pd.DatetimeIndex(df['ts']).minute
# after that you construct a new dataframe with desired columns
df1 = df[['level', 'weekday', 'hour', 'minute']]
# then plot it by setting index to weekday and time (hour + minute)
df1.set_index(['weekday', 'hour', 'minute']).plot(kind='bar')

在此处输入图片说明

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM