目录

一、甘特图的应用

二、甘特图的实现

2.1 matplotlib实现

2.2 plotly实现


一、甘特图的应用

甘特图(gantt)多用于工作中对于任务完成情况以及使车间作业问题排程更加直观。作者在项目中需要使用甘特图对调度排程问题进行可视化展示,如文章中有不对之处欢迎指出。

二、甘特图的实现

2.1 matplotlib实现

# coding=utf-8
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
add=[[14,6,23],[18,7,35],[10,30,21],[18,5,30],[10,25,20],[12,35,20],[10,32,17],[10,20,16]]
left=[[0,14,20],[14,32,43],[32,42,78],[42,72,99],[60,77,129],[70,102,149],[82,137,169],[92,169,189]]
m = range(len(add))
n=range(len(add[0]))
color = ['b','g','r','y','c','m','k']

#画布设置,大小与分辨率
plt.figure(figsize=(20,8),dpi=80)
#barh-柱状图换向,循坏迭代-层叠效果
for i in m:
    for j in n:
        plt.barh(m[i]+1, add[i][j], left=left[i][j],color=color[j])
plt.title("流水加工甘特图")
labels =[''] *len(add[0])
for f in n:
    labels[f] = "工序%d"%(f+1)
#图例绘制
patches = [ mpatches.Patch(color=color[i], label="{:s}".format(labels[i]) ) for i in range(len(add[0])) ]
plt.legend(handles=patches,loc=4)
#XY轴标签
plt.xlabel("加工时间/s")
plt.ylabel("工件加工优先级")
#网格线,此图使用不好看,注释掉
#plt.grid(linestyle="--",alpha=0.5)
plt.show()

 效果图:

2.2 plotly实现

import pandas as pd
import plotly.figure_factory as ff
import random

r = lambda: random.randint(0, 255)
# print('#%02X%02X%02X' % (r(),r(),r()))
colors = ['#%02X%02X%02X' % (r(), r(), r())]

for i in range(1, 100):
        colors.append('#%02X%02X%02X' % (r(),r(),r()))

df = pd.DataFrame(\
    data=[("machine1","2023-12-26 08:12:10.827977","2023-12-26 08:27:10.827977",37),\
    ("machine2","2023-12-26 01:12:10.827977","2023-12-26 05:27:10.827977",19),\
    ("machine1","2023-12-26 08:50:10.827977","2023-12-26 13:27:10.827977",40),\
    ("machine1","2023-12-26 01:12:10.827977","2023-12-26 03:27:10.827977",80),\
    ("machine2","2023-12-26 10:12:10.827977","2023-12-26 20:27:10.827977",90)],\
columns=["Task","Start","Finish","status"])

fig = ff.create_gantt(df, colors=colors, show_colorbar=True, index_col='Task',group_tasks=True)
fig.show()

效果图:

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐