当前位置: 移动技术网 > IT编程>脚本编程>Python > 详解Python使用Plotly绘图工具,绘制甘特图

详解Python使用Plotly绘图工具,绘制甘特图

2019年06月10日  | 移动技术网IT编程  | 我要评论

山炮三人组,烟波浩渺造句,xt615root工具包

今天来讲一下如何使用python 的绘图工具plotly来绘制甘特图的方法

甘特图大家应该了解熟悉,就是通过条形来显示项目的进度、时间安排等相关情况的。

我们今天来学习一下,如何使用ployly来绘制甘特图

绘制甘特图的函数为plotly.figure_factoryz中create_gantt方法

通过参数事件task,开始start,结束finish的时间的数据来绘制甘特图

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')

运行上诉代码,我们可以看到纵坐标为我们的的项目数量。横坐标时间不短增加,越接近纵坐标的,说明事件发生的情况越早。

还可以在甘特图中加入数字索引值

代码如下:

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表示没有进展。

接下来讲解如何按照类别来进行索引

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绘图工具,绘制甘特图详解整合,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网