1

I have data that lists an activity for a time:

``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``

For any given time, there is a unique task: there is no overlap. Given this, I want to create a "bar chart" that shows the tasks being performed using colors, something like the image below. I want the length of the bars to reflect the duration of the task. I want a bar for each day (or week). How can I do this in Python?

This is what I want to see

EDIT:

As per Evert's suggestion, I ended up doing this:

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")

This gave me:

enter image description here

Now I know enough to get started, and I can customize further. Thank you:)

ozzy
  • 643
  • 1
  • 5
  • 7
  • Have you tried anything? Matplotlib? – Reti43 Apr 09 '16 at 22:42
  • No, as I do not know which one to try. What I am trying to do doesn't look like anything from here: http://matplotlib.org/gallery.html. Any suggestions are appreciated. – ozzy Apr 10 '16 at 00:55
  • The [stacked bar](http://matplotlib.org/examples/statistics/histogram_demo_multihist.html#statistics-example-code-histogram-demo-multihist-py) one in the matplotlib gallery looks essentially like what you want. – martineau Apr 10 '16 at 01:30
  • No, stacked bars have one bar for each category. If I wanted to plot total time spend in a day for each task, that would work. But I want to see what tasks are accomplished after which (like going back to task B after C is shown by the green after blue), for how long (indicated by the length of the colored portions) and when each task is started (position of each colored bar on the x-axis). – ozzy Apr 10 '16 at 01:40
  • If you can calculate all the intervals yourself (every block), and keep track of the color for each interval, you can probably use [`broken_barh`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.broken_barh). Some idea in [this example figure](http://matplotlib.org/examples/pylab_examples/broken_barh.html), and perhaps see also a [related SO question](http://stackoverflow.com/questions/24425908/matplotlib-how-to-use-timestamps-with-broken-barh). –  Apr 10 '16 at 03:12
  • I think that might work! I will play around with it, and report the results here when I come up with something – ozzy Apr 10 '16 at 21:28

0 Answers0