修复 plotly 甘特图中的颜色顺序
Fix the color order in a plotly gantt chart
尝试使用 plotly 和 pandas 数据框绘制甘特图。情节很好,除了颜色顺序不同。
这是数据框
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
我们对以上数据进行如下处理:
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')
使用从剪贴板读取数据
gdf=pd.read_clipboard(sep='\s\s+')
我的数据颜色不正确。我尝试了几次颜色重新排序,但结果不正确。正确的顺序应如下面 dict
所示,项目后跟它应有的颜色。
{'T':'#FFFF00','G':'#A0522D','M':'#808000','D':'#008000','V':'#FF0000', 'K':'#00CED1',"I':'#00FFFF'}
您可以在创建图形后更新颜色。
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)
尝试使用 plotly 和 pandas 数据框绘制甘特图。情节很好,除了颜色顺序不同。
这是数据框
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
我们对以上数据进行如下处理:
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')
使用从剪贴板读取数据
gdf=pd.read_clipboard(sep='\s\s+')
我的数据颜色不正确。我尝试了几次颜色重新排序,但结果不正确。正确的顺序应如下面 dict
所示,项目后跟它应有的颜色。
{'T':'#FFFF00','G':'#A0522D','M':'#808000','D':'#008000','V':'#FF0000', 'K':'#00CED1',"I':'#00FFFF'}
您可以在创建图形后更新颜色。
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)