Skip to content Skip to sidebar Skip to footer

Timeline In Python

Is there any way to create a timeline in Python similar to this post using only 1 vizualiation package and no other setup? I have tried to use the plotnine package to use ggplot2 w

Solution 1:

I also had this issue ... and this is my solution. Please ignore the Ugly Color Generation ... this is more of a work in progress.

It is Python 3.6 tested ....

import numpy as np
import matplotlib.pylab as plt
import pandas as pd
from cycler import cycler
from datetime import datetime, timedelta

idx = pd.date_range('2018-1-1', '2018-9-10', freq='1D')
df = pd.DataFrame({'Offset': 20,'val': 2}, index=idx)
df['Offset']=[n for n inrange(0,len(df))]

sched=[{'name':'tim',
     'jobs':{
          1:(datetime(2018,1,1,),datetime(2018,2,1)),
          2:(datetime(2018,4,1) ,datetime(2018,5,1)),
          3:(datetime(2018,6,1) ,datetime(2018,7,1))}
       },
       {'name':'BiMonthly',
     'jobs':{
          1:(datetime(2018,2,1,),datetime(2018,3,1)),
          2:(datetime(2018,5,1) ,datetime(2018,6,1)),
          3:(datetime(2018,7,1) ,datetime(2018,8,1))}
       }
       ,
          {'name':'Monthly',
     'jobs':{
           1:(datetime(2018,2,1),datetime(2018,2,10)),
           2:(datetime(2018,3,1),datetime(2018,3,10)),
           3:(datetime(2018,4,1),datetime(2018,4,10)),
           4:(datetime(2018,5,1),datetime(2018,5,10)),
           5:(datetime(2018,6,1),datetime(2018,6,10))
         }},
        {'name':'LongTerm',
     'jobs':{
           1:(datetime(2018,2,1),datetime(2018,5,1))
         }
        }]

color_cycle = cycler(c=['r', 'g', 'b'])
ls_cycle = cycler('ls', ['-', '--'])
sty_cycle = ls_cycle * ( color_cycle)

defget_offset(when):
    global df
    iftype(when)==str:
        when=pd.to_datetime(when)
    try:
        return df.loc[when]['Offset']
    except KeyError:
        print("{} Not Found".format(when))
        return -1

thickness=0.3
timelines=[]

start_period = idx[0].to_period('D').ordinal

for a_job_group in sched:
    timeline=[]
    print("-----")
    for keys in a_job_group['jobs']:
        #print("Dates {} {}".format(a_job_group['jobs'][keys][0],#                     a_job_group['jobs'][keys][1]))

        offset_start = get_offset(a_job_group['jobs'][keys][0])
        offset_end   = get_offset(a_job_group['jobs'][keys][1])
        print("offset {} {} TimeSpan {}".format(offset_start, 
                                                offset_end, offset_end - offset_start))  
        timeline_data=(start_period + offset_start,offset_end-offset_start)
        timeline.append(timeline_data)
    timelines.append(timeline)

pos= 0
df.drop(['Offset'],axis=1,inplace=True,)
ax = df.plot(color='w')
col_schema=[s for s in sty_cycle]
for t in timelines:
    ax.broken_barh(t, [pos, thickness],
                   color=col_schema[pos]['c'],
                  linestyle=col_schema[pos]['ls'])
    pos+= 1
plt.show()

What does it output ?

enter image description here

I have to add an Index - and to check that I can change the TimeSteps (hours weeks etc) but so far this it the best solution I can find.

I plan to add mpld3 to it - and then to run it via Flask.... So I have a little ways to go.

Post a Comment for "Timeline In Python"