今天来讲一下如何使用python 的绘图工具plotly来绘制甘特图的方法
甘特图大家应该了解熟悉,就是通过条形来显示项目的进度、时间安排等相关情况的。
我们今天来学习一下,如何使用ployly来绘制甘特图
绘制甘特图的函数为plotly.figure_factoryz中create_gantt方法
通过参数事件task,开始start,结束finish的时间的数据来绘制甘特图
1
2
3
4
5
6
7
8
9
10
11
|
import plotly as py import plotly.figure_factory as ff pyplt = py.offline.plot df = [ dict (task = "项目1" , start = '2019-02-01' , finish = '2019-05-28' ), dict (task = "项目2" , start = '2019-03-05' , finish = '2019-04-15' ), dict (task = "项目3" , start = '2019-03-20' , finish = '2019-05-30' )] fig = ff.create_gantt(df) pyplt(fig, filename = 'tmp/1.html' ) |
运行上诉代码,我们可以看到纵坐标为我们的的项目数量。横坐标时间不短增加,越接近纵坐标的,说明事件发生的情况越早。
还可以在甘特图中加入数字索引值
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import plotly as py import plotly.figure_factory as ff pyplt = py.offline.plot df = [ dict (task = "项目1" , start = '2019-01-01' , finish = '2019-02-28' , complete = 10 ), dict (task = "项目2" , start = '2018-12-05' , finish = '2019-04-15' , complete = 10 ), dict (task = "项目3" , start = '2019-02-20' , finish = '2019-05-30' , complete = 50 ), dict (task = "项目4" , start = '2019-03-20' , finish = '2019-06-30' , complete = 50 ), dict (task = "项目5" , start = '2019-01-12' , finish = '2019-04-28' , complete = 100 ), dict (task = "项目6" , start = '2019-03-07' , finish = '2019-08-21' , complete = 100 )] fig = ff.create_gantt(df, index_col = 'complete' , show_colorbar = true) pyplt(fig, filename = 'tmp/1.html' ) |
运行上诉代码,可以看到右边出现了索引条,每项工作后的complete属性即为其对应的索引值,在create_gamtt函数中
设置,index_col = ‘complete',则会出现相同条形对应相同颜色的,可以根据颜色来盘点任务大概对应的索引值是多少。
这个值得范围0-100,用来反映工作完成的进度,100表示全部完成,0表示没有进展。
接下来讲解如何按照类别来进行索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import plotly as py import plotly.figure_factory as ff pyplt = py.offline.plot df = [ dict (task = "项目1" , start = '2019-01-01' , finish = '2019-02-02' , resource = 'complete' ), dict (task = "项目2" , start = '2019-02-15' , finish = '2019-03-15' , resource = 'incomplete' ), dict (task = "项目3" , start = '2019-01-17' , finish = '2019-02-17' , resource = 'not started' ), dict (task = "项目4" , start = '2019-01-17' , finish = '2019-02-17' , resource = 'complete' ), dict (task = "项目5" , start = '2019-03-10' , finish = '2019-03-20' , resource = 'not started' ), dict (task = "项目6" , start = '2019-04-01' , finish = '2019-04-20' , resource = 'not started' ), dict (task = "项目7" , start = '2019-05-18' , finish = '2019-06-18' , resource = 'not started' ), dict (task = "项目8" , start = '2019-01-14' , finish = '2019-03-14' , resource = 'complete' )] colors = { 'not started' : 'rgb(220, 0, 0)' , 'incomplete' : ( 1 , 0.9 , 0.16 ), 'complete' : 'rgb(0, 255, 100)' } fig = ff.create_gantt(df, colors = colors, index_col = 'resource' , group_tasks = true) pyplt(fig, filename = 'tmp/1.html' ) 运行上诉代码,得到如下图所示内容 |
运行上诉代码,得到如下图所示内容
这里我们通过按照不同的颜色,来分别项目的类型。同一颜色,为同一项目。在每一项数据中
resource代表此项工作所属的状态。这里分了三种状态。
通过在设置create_gantt函数中的index_col = 'resource'即可完成。
color属性用于设置不同状态对应的颜色。
以上所述是小编给大家介绍的python使用plotly绘图工具,绘制甘特图详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/u012798683/article/details/88852291