我有一段列出活动的数据:
``Process Start Process End Duration Task
1/3/2016 4:07:47 PM 1/3/2016 4:08:09 PM 0:00:22 A
1/3/2016 4:08:09 PM 1/3/2016 4:08:19 PM 0:00:10 B
1/3/2016 4:08:19 PM 1/3/2016 4:08:27 PM 0:00:08 C
1/3/2016 4:08:27 PM 1/3/2016 4:08:35 PM 0:00:08 B
1/3/2016 4:08:35 PM 1/3/2016 4:08:46 PM 0:00:11 A``
对于任何给定的时间,都有一个独特的任务:没有重叠。鉴于此,我想创建一个“条形图”,显示使用颜色执行的任务,如下图所示。我希望条形的长度反映任务的持续时间。我想要每天(或一周)的酒吧。我怎么能用Python做到这一点?
编辑:
根据Evert的建议,我最终做到了这一点:
import pandas as pd
from datetime import datetime
# Read the data in (Time_Sample.txt contains the data I have posted above):
df=pd.read_csv('Time_Sample.txt',sep='\t')
df['Day']=df['Process Start'].str[0:9].str.strip()
df['Week_Day']=df['Day'].apply(lambda x: datetime.strptime(x,"%m/%d/%Y").weekday())
df['Duration_Seconds']=((df['Duration'].str[-5:].str[0:2]).map(float)*60 + \
(df['Duration'].str[-2:]).map(float))
df['Seconds_Since_Midnight'] = df['Process Start'].apply(lambda x:( datetime.strptime(x,"%m/%d/%Y %I:%M:%S %p") - datetime.strptime(x,"%m/%d/%Y %I:%M:%S %p").replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds())
df['Color']=df['Task'].map({'A':'yellow','B':'green','C':'blue'})
plt.figure()
plt.barh(bottom=df['Week_Day']+0.1, width=df['Duration_Seconds'], height=0.8,left=df['Seconds_Since_Midnight'], color=df['Color'], edgecolor = "none")
这给了我:
现在我已经足够了解,我可以进一步定制。谢谢:))