给我一个多工厂多机器的流水车间调度绘制的甘特图的python代码,并且对甘特图进行美化,并将甘特图中的矩形改为圆角矩形
时间: 2024-04-30 19:21:16 浏览: 187
遗传算法 车间调度 甘特图 代码.zip_officerf89_understandingapr_调度_调度 甘特图_遗传
5星 · 资源好评率100%
以下是一个基于 Python 的多工厂多机器的流水车间调度绘制甘特图的示例代码:
```python
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# 定义车间数量
num_workshops = 2
# 定义机器数量
num_machines = 4
# 定义工序数量
num_operations = 6
# 生成随机数据
data = np.random.randint(1, 20, size=(num_operations, num_machines, num_workshops))
# 格式化数据为 DataFrame
df = pd.DataFrame(data.reshape(num_operations, -1), columns=[f"Workshop {i}, Machine {j}" for i in range(1, num_workshops+1) for j in range(1, num_machines+1)])
# 定义甘特图颜色
colors = plt.get_cmap("Set1")(np.linspace(0, 1, num_operations))
# 定义甘特图标题
plt.title("Multi-Factory Multi-Machine Flow Shop Scheduling Gantt Chart")
# 绘制甘特图
for i, row in df.iterrows():
plt.barh(y=i, width=row, left=df.iloc[:i].sum(), color=colors[i], height=0.8, alpha=0.8, edgecolor="black", linewidth=0.5)
# 设置 Y 轴标签
plt.yticks(df.index, [f"Operation {i+1}" for i in range(num_operations)])
# 设置 X 轴标签
plt.xlabel("Time (hours)")
# 设置图例
plt.legend([f"Workshop {i}, Machine {j}" for i in range(1, num_workshops+1) for j in range(1, num_machines+1)], loc="upper right")
# 美化甘特图
plt.rcParams["font.family"] = "Arial"
plt.rcParams["font.size"] = 12
plt.grid(axis="x", color="white", linestyle="-", linewidth=0.5)
plt.box(False)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.tick_params(axis="both", which="both", length=0)
# 将矩形改为圆角矩形
for patch in plt.gca().patches:
patch.set_edgecolor("black")
if isinstance(patch.get_facecolor(), tuple):
patch.set_facecolor(patch.get_facecolor()[:3] + (0.8,))
patch.set_linewidth(0.5)
patch.set_joinstyle("round")
patch.set_capstyle("round")
# 显示甘特图
plt.show()
```
这段代码会生成一个多工厂多机器的流水车间调度绘制的甘特图,并对甘特图进行美化,并将甘特图中的矩形改为圆角矩形。你可以根据需要修改变量的值以生成不同的甘特图。
阅读全文