1 import numpy as np, matplotlib.pyplot as plt
   2 import matplotlib.colors as mc
   3 import colorsys
   4 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   5 setup_style()
   6 
   7 params = ['学习率 η', '批量 B', '种群大小 N', '交叉概率 Pc', '变异概率 Pm', '初始温度 T₀']
   8 low_impact = np.array([-12.1, -8.5, -6.4, -5.2, -3.8, -2.1])
   9 high_impact = np.array([15.3, 7.2, 5.9, 4.8, 3.1, 1.8])
  10 
  11 # 按影响幅度排序
  12 total_range = high_impact - low_impact
  13 sort_idx = np.argsort(total_range)
  14 params = [params[i] for i in sort_idx]
  15 low_impact, high_impact = low_impact[sort_idx], high_impact[sort_idx]
  16 total_range = total_range[sort_idx]
  17 max_range = total_range.max() if total_range.max() > 0 else 1
  18 
  19 # ★ 正负方向用不同色系
  20 color_pos = '#5B8DB8'   # 蓝色系(正向影响)
  21 color_neg = '#D4896A'   # 橙色系(负向影响)
  22 
  23 n = len(params)
  24 _fig_h = max(3, n * 0.7 + 1.2)
  25 fig, ax = plt.subplots(figsize=(8, _fig_h))
  26 y = np.arange(n)
  27 
  28 ax.grid(axis='x', alpha=0.12, linestyle='-', color=COLORS['grid'])
  29 ax.set_axisbelow(True)
  30 
  31 # ★ 交替行背景
  32 for i in range(n):
  33     if i % 2 == 0:
  34         ax.axhspan(y[i] - 0.4, y[i] + 0.4, alpha=0.03, color=PALETTE[0], zorder=0)
  35 
  36 # ★ 条形(淡色填充 + 原色边框,颜色深浅映射影响幅度比例)
  37 max_abs = max(abs(low_impact).max(), abs(high_impact).max())
  38 for i in range(n):
  39     intensity = total_range[i] / max_range if max_range > 0 else 0.5
  40     lighten_amt = 0.5 * (1 - intensity)
  41 
  42     if high_impact[i] > 0:
  43         c = _lighten(color_pos, lighten_amt)
  44         ax.barh(y[i], high_impact[i], height=0.52,
  45                 color=_lighten(c, 0.3), edgecolor=c, linewidth=1.3, zorder=3)
  46     if low_impact[i] < 0:
  47         c = _lighten(color_neg, lighten_amt)
  48         ax.barh(y[i], low_impact[i], height=0.52,
  49                 color=_lighten(c, 0.3), edgecolor=c, linewidth=1.3, zorder=3)
  50 
  51     # 连接线
  52     if low_impact[i] < 0 and high_impact[i] > 0:
  53         ax.plot([low_impact[i], high_impact[i]], [y[i], y[i]],
  54                 color=COLORS['ref_line'], linewidth=0.5, zorder=1, alpha=0.4)
  55 
  56     # 数值标签
  57     margin = max_abs * 0.05
  58     if high_impact[i] > 0:
  59         ax.text(high_impact[i] + margin, y[i], f'+{high_impact[i]:.1f}%',
  60                 va='center', ha='left', fontsize=8.5, fontweight='bold',
  61                 color=_lighten(color_pos, lighten_amt * 0.5))
  62     if low_impact[i] < 0:
  63         ax.text(low_impact[i] - margin, y[i], f'{low_impact[i]:.1f}%',
  64                 va='center', ha='right', fontsize=8.5, fontweight='bold',
  65                 color=_lighten(color_neg, lighten_amt * 0.5))
  66 
  67 # 基准线 + 标注
  68 ax.axvline(0, color=COLORS['text'], linewidth=1.0, zorder=2)
  69 ax.text(0, n - 0.1, '基准值', ha='center', va='bottom', fontsize=8,
  70         color=COLORS['ref_line'],
  71         bbox=dict(boxstyle='round,pad=0.2', facecolor='white',
  72                   edgecolor=COLORS['grid'], alpha=0.85))
  73 
  74 # 方向标注
  75 ax.text(max_abs * 0.6, -0.7, '参数增大 →', fontsize=8, color=color_pos,
  76         ha='center', fontstyle='italic', alpha=0.6)
  77 ax.text(-max_abs * 0.6, -0.7, '← 参数减小', fontsize=8, color=color_neg,
  78         ha='center', fontstyle='italic', alpha=0.6)
  79 
  80 ax.set_yticks(y); ax.set_yticklabels(params, fontsize=10)
  81 ax.set_xlabel('目标函数变化 (%)', fontsize=11)
  82 xlim_max = max_abs * 1.35
  83 ax.set_xlim(-xlim_max, xlim_max)
  84 ax.set_ylim(-1.0, n - 0.5)
  85 ax.spines['top'].set_visible(False)
  86 ax.spines['right'].set_visible(False)
  87 fig.tight_layout()
  88 save_fig(fig, 'figures/fig_tornado.pdf')