Trying to plot a gantt chart using plotly and a pandas dataframe. The plot comes fine, except the colors are in a different order.
Here is the dataframe
Goss got
timestamp
16-01-21 10:00:09 M Item1
04-02-21 20:28:30 T Item2
06-02-21 00:45:57 V Item3
24-07-21 11:07:39 M Item4
26-08-21 16:15:01 T Item5
28-08-21 14:05:13 I Item6
02-09-21 16:57:26 D Item7
15-09-21 15:35:07 M Item8
13-10-21 18:19:42 D Item9
18-10-21 02:39:40 G Item10
31-10-21 22:07:27 D Item11
01-11-21 09:58:21 I Item12
01-11-21 11:37:44 V Item13
28-11-21 10:44:39 K Item14
28-11-21 14:52:39 M Item15
29-11-21 18:40:32 G Item16
01-12-21 23:34:00 G Item17
03-12-21 00:11:11 T Item18
05-12-21 15:55:50 G Item19
We process the above data as follows:
import pandas as pd
import plotly
from plotly import figure_factory as FF
gdf=pd.read_clipboard(sep='\s\s+') #with this the df data above can be copied and read to a df
gdf.reset_index(inplace=True)
gdf.timestamp = pd.to_datetime(gdf.timestamp,format="%d-%m-%y %H:%M:%S")
gdf=pd.concat([gdf.timestamp.shift(),gdf], axis=1)
gdf.columns=['Start', 'Finish', 'Goss', 'Task']
gdf.at[0,'Start'] = pd.Timestamp("2021-01-01-00-00-00")
gfig = FF.create_gantt(gdf, colors=['#FFFF00',
'#A0522D',
'#808000',
'#008000',
'#FF0000',
'#00CED1',
'#00FFFF'],
index_col='Goss', show_colorbar=True,
bar_width=0.2, showgrid_x=True, showgrid_y=True)
plotly.offline.plot(gfig, filename='gorge.html')
Read the data from clipboard using
gdf=pd.read_clipboard(sep='\s\s+')
My data gets improperly colored. I tried few reorder of the colors, didn't come out correctly. The correct order should be as given in the dict
below, item followed by the color it should have.
{'T':'#FFFF00','G':'#A0522D','M':'#808000','D':'#008000','V':'#FF0000', 'K':'#00CED1',"I':'#00FFFF'}
You can update the colors after the figure is created.
cmap = {'T':'#FFFF00','G':'#A0522D','M':'#808000','D':'#008000','V':'#FF0000', 'K':'#00CED1',"I":'#00FFFF'}
gfig.for_each_trace(lambda t: t.update(fillcolor=cmap[t.name]) if t.name in cmap.keys() else t)