数据来自python实现GA求解基于多层编码的柔性作业车间调度问题
42.0 15
best se:[0. 3. 1. 2. 1. 5. 4. 0. 4. 5. 1. 2. 3. 1. 3. 2. 0. 0. 3. 5. 4. 1. 4. 0.
5. 1. 5. 3. 0. 2. 5. 2. 3. 2. 4. 4. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.
0. 0. 1. 0. 0. 1. 0. 1. 0. 0. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
best pt:[[ 0. 0. 0. 3. 6. 6. 0. 3. 6. 8. 14. 12. 13. 15. 18. 20. 13. 24.
24. 12. 18. 20. 25. 28. 23. 23. 24. 29. 31. 25. 29. 30. 30. 41. 33. 38.]
[ 3. 7. 6. 4. 14. 8. 6. 13. 18. 12. 15. 17. 17. 20. 24. 25. 22. 28.
27. 21. 25. 23. 33. 31. 24. 26. 29. 30. 41. 30. 32. 39. 33. 42. 38. 42.]]
best ma:[3. 4. 2. 3. 3. 2. 5. 1. 7. 4. 5. 4. 1. 6. 7. 6. 2. 7. 2. 9. 3. 1. 9. 8.
1. 4. 5. 5. 5. 1. 3. 2. 6. 5. 1. 4.]
将数据变成create_gantt中的df格式,task加工机器,start每个操作的开始时间,finish每个操作的结束时间,resource作业编号
task = list(best_ma.astype(int))
start = list(best_pt[0].astype(int))
finish = list(best_pt[1].astype(int))
resource = list(best_se[:36].astype(int))
import pandas as pd
Resource = pd.Series(resource)
Task = pd.Series(task)
Start = pd.Series(start)
Finish = pd.Series(finish)
df = pd.DataFrame(list(zip(Task,Start,Finish,Resource)))
df.columns = ['Task','Start','Finish','Resource']
此时的df中Start和Finish是用整数表示的时间,因此需要转换成时间格式,可任取某时间戳数据作为基准,加上Start和Finish(假设为分钟)即可。注意此处的作业编号resource要变成字符类型。
datetime = pd.Timestamp('20190627 19:00:00')
for i in range(len(df)):
df['Start'].loc[i] = datetime + pd.Timedelta(minutes = df['Start'].loc[i])
df['Finish'].loc[i] = datetime + pd.Timedelta(minutes = df['Finish'].loc[i])
df['Resource'] = df['Resource'].astype(str)
df.head()
转换后的数据如下:
使用下文介绍的create_gantt绘制甘特图
from plotly.figure_factory import create_gantt
fig = create_gantt(df,colors=['rgb(200,50,25)',(1,0,1),'#6c4774','rgb(114, 44, 121)','rgb(255,0,0)',(1,1,0.2)],
index_col = 'Resource',reverse_colors=True,show_colorbar=True,group_tasks = True)
fig.show()
plotly库中甘特图绘制最开始是plotly.figure_factory.create_gantt,现已更新成为plotly.express.timeline
Plotly是一个非常著名且强大的开源数据可视化框架,它通过构建基于浏览器显示的web形式的可交互图表来展示信息,可创建多达数十种精美的图表和地图
plotly中绘制甘特图的函数:plotly.figure_factory.create_gantt
from plotly.figure_factory import create_gantt
# 创建数据
df = [dict(Task="Job A", Start='2009-01-01', Finish='2009-03-30'),
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15'),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30')]
# 创建甘特图
fig=create_gantt(df)
fig.show()
列索引为Complete(10,60,95)
from plotly.figure_factory import create_gantt
df = [dict(Task='job A', Start='2009-01-01',Finish='2009-03-30', Complete=10),
dict(Task="Job B", Start='2009-03-05',Finish='2009-04-15', Complete=60),
dict(Task="Job C", Start='2009-02-20',Finish='2009-05-30', Complete=95)]
fig = create_gantt(df,colors='Blues',index_col='Complete',show_colorbar=True,
bar_width=0.5,showgrid_x=True,showgrid_y=True)
fig.show()
列索引为Resource(Apple,Grape,Banana)
from plotly.figure_factory import create_gantt
df = [dict(Task="Job A", Start='2009-01-01',Finish='2009-02-15', Resource='Apple'),
dict(Task="Job B", Start='2009-03-05',Finish='2009-04-15', Resource='Grape'),
dict(Task="Job C", Start='2009-02-20',Finish='2009-05-30', Resource='Banana')]
fig = create_gantt(df,colors=['rgb(200,50,25)',(1,0,1),'#6c4774'],
index_col='Resource',reverse_colors=True,show_colorbar=True)
fig.show()
from plotly.figure_factory import create_gantt
df = [dict(Task="Job A", Start='2009-01-01',Finish='2009-02-15', Resource='Apple'),
dict(Task="Job B", Start='2009-03-05',Finish='2009-04-15', Resource='Grape'),
dict(Task="Job C", Start='2009-02-20',Finish='2009-05-30', Resource='Banana')]
colors = {'Apple':'rgb(255,0,0)',
'Grape':'rgb(170,14,200)',
'Banana':(1,1,0.2)}
fig = create_gantt(df,colors=colors,index_col='Resource',show_colorbar=True)
fig.show()
from plotly.figure_factory import create_gantt
import pandas as pd
df = pd.DataFrame([['Run', '2010-01-01', '2011-02-02', 10],
['Fast', '2011-01-01', '2012-06-05', 55],
['Eat', '2012-01-05', '2013-07-05', 94]],
columns=['Task', 'Start', 'Finish', 'Complete'])
fig = create_gantt(df,colors='Blues',index_col='Complete',show_colorbar=True,
bar_width=0.5,showgrid_x=True, showgrid_y=True)
fig.show()
在timeline甘特图中,data_frame的每行都表示为日期类型在x轴上的矩形标记,范围从x_start到x_end
from plotly.express import timeline
df = pd.DataFrame([dict(Task="项目1", Start='2021-02-01', Finish='2021-03-25',Manager='Lemon',Completion_pct=90),
dict(Task="项目2", Start='2021-03-05', Finish='2021-04-15',Manager='Lee',Completion_pct=60),
dict(Task="项目3", Start='2021-02-20', Finish='2021-05-30',Manager='Zhang',Completion_pct=70),
dict(Task="项目4", Start='2021-04-20', Finish='2021-09-30',Manager='Bob',Completion_pct=20),])
fig = timeline(df, x_start="Start", x_end="Finish", y="Task",color='Manager')
fig.show()
设置颜色和数字比例
import plotly.express as px
from plotly.express import timeline
df = pd.DataFrame([dict(Task="项目1", Start='2021-02-01', Finish='2021-03-25',Manager='Lemon',Completion_pct=90),
dict(Task="项目2", Start='2021-03-05', Finish='2021-04-15',Manager='Lee',Completion_pct=60),
dict(Task="项目3", Start='2021-02-20', Finish='2021-05-30',Manager='Zhang',Completion_pct=70),
dict(Task="项目4", Start='2021-04-20', Finish='2021-09-30',Manager='Bob',Completion_pct=20),])
fig = timeline(df, x_start="Start", x_end="Finish", y="Task",color='Completion_pct',
color_continuous_scale=px.colors.sequential.Turbo)
fig.show()
import plotly.express as px
px.colors.sequential.swatches()
import plotly.express as px
px.colors.qualitative.swatches()