0

I have a DataFrame that logs hours based on Employee:

import pandas as pd
data = [['Jack', 0, 6], ['Jack', 8, 12],['Barry', 0, 5], ['Barry', 7, 15]]
df = pd.DataFrame(data, columns=['Employee', 'Start', 'End'])

  Employee  Start  End
0     Jack      0    6
1     Jack      8   12
2    Barry      0    5
3    Barry      7   15

How can I plot out the work intervals where the x axis is time and the y axis is for the employee key using pyplot? To arrive at something like this where the bars are numbered with start/end times.

enter image description here

1 Answer 1

1

Try a for loop:

for _, r in df.iterrows():
    plt.plot(r[['Start','End']], [r['Employee'],r['Employee']], color='blue')
    plt.annotate(f"{r['Start']}",(r['Start'], r['Employee']), va='bottom')
    plt.annotate(f"{r['End']}",(r['End'], r['Employee']), va='bottom', ha='center')

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! that did it. if i wanted to handle multiple bars per employee stacked on top of each other (say one for RestStart/RestEnd, how would I offset the bars so they don't fall on top of one another? here's what I have so far: ibb.co/d6srF7Q you can see the orange bar for 'Rest' is overlapping. pastebin.com/pMtTKn6p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.