1 import numpy as np, matplotlib.pyplot as plt
   2 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   3 setup_style()
   4 
   5 tasks = ['任务A', '任务B', '任务C', '任务D', '任务E', '任务F', '任务G', '任务H']
   6 starts = [0, 2, 1, 5, 3, 7, 6, 9]
   7 durations = [3, 4, 2, 3, 5, 2, 4, 3]
   8 machines = [0, 1, 0, 2, 1, 0, 2, 1]
   9 machine_names = ['机器M1', '机器M2', '机器M3']
  10 
  11 n = len(tasks)
  12 _fig_h = max(5, n * 0.35 + 1)
  13 fig, ax = plt.subplots(figsize=(10, _fig_h))
  14 ax.grid(axis='x', alpha=0.12, linestyle='--', color=COLORS['grid']); ax.set_axisbelow(True)
  15 
  16 for i in range(n):
  17     c = PALETTE[machines[i] % len(PALETTE)]
  18     ax.barh(i, durations[i], left=starts[i], height=0.6,
  19             color=_lighten(c, 0.3), edgecolor=c, linewidth=1.3, zorder=3)
  20     ax.text(starts[i] + durations[i]/2, i, f'J{i+1}', ha='center', va='center',
  21             fontsize=8, fontweight='bold', color=c)
  22 
  23 # Makespan 标注(放在 axes 内部顶端,避免 tight_layout 压缩导致标注飘远)
  24 makespan = max(s+d for s, d in zip(starts, durations))
  25 ax.axvline(makespan, color=COLORS['down'], linewidth=1.5, linestyle='--', zorder=4)
  26 # ⛔ 不要用 y=1.02 + transform=ax.get_xaxis_transform()(会被 tight_layout 压缩导致标注飘远)
  27 # 改用 axes 内部坐标:放在第一个任务条的上方
  28 ax.text(makespan, -0.8, f'Makespan={makespan}h',
  29         ha='center', va='center', fontsize=9, fontweight='bold', color=COLORS['down'],
  30         bbox=dict(boxstyle='round,pad=0.3', facecolor='white', edgecolor=COLORS['down'], alpha=0.9))
  31 
  32 # 图例
  33 legend_elements = [plt.Rectangle((0,0), 1, 1, facecolor=_lighten(PALETTE[j], 0.3), edgecolor=PALETTE[j], linewidth=1.3)
  34                    for j, name in enumerate(machine_names)]
  35 ax.legend(handles=legend_elements, labels=machine_names, frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8, ncol=len(machine_names), loc='best')
  36 ax.set_yticks(range(n)); ax.set_yticklabels(tasks, fontsize=10)
  37 ax.set_xlabel('时间 (h)', fontsize=11)
  38 ax.invert_yaxis()
  39 ax.spines['top'].set_visible(False); ax.spines['right'].set_visible(False)
  40 fig.tight_layout()
  41 save_fig(fig, 'figures/fig_gantt.pdf')