Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

Broken barh timeline using mattplotlib when row items matches (python)

Let met start by saying that this is my first post so if something is out of the ordinary - my apologies.

I've tried to find solutions to solve my problem, but unfortunately to no avail. I'm trying to create a timeline based on my dataframe ('df_phase') which has the following headers:

Name | time_entries.days | time_entries.end_time | time_entries.hours | time_entries.minutes | time_entries.seconds | time_entries.start_time

In theory, the only columns that matter are the name column, the end time column and the start time column.

Right now I've made a script that can plot the timeline correctly - however, it shows each Activity (column 'name') on a new y-axis row. Ideally, I would like to have row items with the same activity name on the same y-axis using a broken barh and every new row item with a new activity name on a new y-axis row.

My current script is as follows:

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

#Convert DF columns into lists
sdate = df_phase['Start_Time'].tolist()
edate = df_phase['End_Time'].tolist()
tasks = df_phase['name'].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()
ax.set_xlabel('Time line')
plt.xlim(df_phase['Start_Time'].min(), df_phase['End_Time'].max())
ax.xaxis.set_major_formatter(dates.DateFormatter('%H:%M:%S'))

plt.show()

Would it be possible to create such broken barh for each Activity with multiple time entries? If possible with new colours for each y-axis barh.

I would really appreciate your help - thanks :)

question from:https://stackoverflow.com/questions/65841644/broken-barh-timeline-using-mattplotlib-when-row-items-matches-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...