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 within Python but this is quite cumbersome to get it to work. Furthermore, I have tried the labella
package but this requires installation of a Latex distribution. With matplotlib
I can't find a way to include the comments next to the event bars.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答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 in range(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)
def get_offset(when):
global df
if type(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 ?
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.