How to get gantt plot using matplotlib for task with start time and end time upto millisecs

佐手、 提交于 2020-01-16 04:14:26

问题


I am having data in a dataframe for each task with start time, end time and status. I want to draw a gantt chart for this. I tried following other question on stackoverflow (link) but they used numerical values so not able to use them. Below is the code.

import pandas as pd   
import matplotlib.pyplot as plt 
data = [['A', '2019-06-27 18:33:58.033', '2019-06-27 19:54:04.658', 'Success'], ['B', '2019-06-27 19:54:04.957', '2019-06-27 19:54:14.570', 'Success'], ['B', '2019-06-27 19:54:04.963', '2019-06-27 19:54:19.928', 'Failed']]
#Converting List to a dataframe
df = pd.DataFrame(data, columns = ['Task', 'Start Time', 'End Time', 'Status']) 
#Calculating the Time Difference
df['Duration'] = pd.to_datetime(df['End Time']) - pd.to_datetime(df['Start Time'])

color = {"Success":"turquoise", "Failed":"crimson"}
fig,ax=plt.subplots(figsize=(6,3))
labels=[]

for i, task in enumerate(df.groupby("Task")):
    labels.append(task[0])
    for r in task[1].groupby("Status"):
        data = r[1][["Start Time", "Duration"]]
        ax.broken_barh(data.values, (i-0.4,0.8), color=color[r[0]] )

ax.set_yticks(range(len(labels)))
ax.set_yticklabels(labels) 
ax.set_xlabel("time [ms]")
plt.tight_layout()       
plt.show()

Its not showing correct graph, may be due to time format. The above code works well if I use decimal numbers in place of time. Any help here.


回答1:


I am able to draw the graph using time in matplotlib, however not able to color bars differently for success and failure. Solution with this feature is welcomed.

import pandas as pd    
from datetime import datetime
import matplotlib.dates as dates
import matplotlib.pyplot as plt
data = [['A', '2019-06-27 18:33:58.033', '2019-06-27 19:54:04.658', 'Success'], ['B', '2019-06-27 19:54:04.957', '2019-06-27 19:58:14.570', 'Success'], ['C', '2019-06-27 19:54:04.963', '2019-06-27 19:54:19.928', 'Failed']]
df = pd.DataFrame(data, columns = ['Task', 'Start_Time', 'End_Time', 'Status']) 

df_phase = df
df_phase['Start_Time'] = pd.to_datetime(df_phase['Start_Time'], format='%Y-%m-%d %H:%M:%S.%f')
df_phase['End_Time'] = pd.to_datetime(df_phase['End_Time'], format='%Y-%m-%d %H:%M:%S.%f')

#Convert DF columns into lists
sdate = df_phase['Start_Time'].tolist()
edate = df_phase['End_Time'].tolist()
tasks = df_phase['Task'].tolist()

#Convert time to Matplotlib number format
edate, sdate = [dates.date2num(item) for item in (edate, sdate)]
time_diff = edate - sdate
ypos = range(len(tasks))
fig, ax = plt.subplots()
ax.barh(ypos, time_diff, left=sdate, height=0.8, align='center', color='blue',edgecolor='black')
plt.yticks(ypos, tasks)
ax.axis('tight')

# We need to tell matplotlib that these are dates...
ax.xaxis_date()
plt.show()

Output Image:



来源:https://stackoverflow.com/questions/56803727/how-to-get-gantt-plot-using-matplotlib-for-task-with-start-time-and-end-time-upt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!