1 import numpy as np, matplotlib.pyplot as plt, matplotlib.colors as mc
   2 import colorsys
   3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   4 setup_style()
   5 
   6 categories = ['成本', '效率', '可靠性', '安全性', '灵活性', '可扩展性']
   7 N = len(categories)
   8 angles = np.linspace(0, 2*np.pi, N, endpoint=False).tolist() + [0]
   9 
  10 methods = {
  11     '本文方法': [0.95, 0.88, 0.92, 0.90, 0.85, 0.91],
  12     '方案A':   [0.80, 0.82, 0.78, 0.85, 0.70, 0.75],
  13     '方案B':   [0.85, 0.75, 0.88, 0.72, 0.90, 0.68],
  14     '方案C':   [0.70, 0.90, 0.65, 0.80, 0.60, 0.82],
  15 }
  16 
  17 fig, ax = plt.subplots(figsize=(6.5, 6.5), subplot_kw=dict(polar=True))
  18 highlight_index = 0  # 重点系列的位置,与显示名称无关
  19 ax.set_facecolor('white')
  20 ax.set_ylim(0, 1.12)  # 留出标注空间
  21 
  22 # ★ 渐变环色背景 + 自定义同心圆网格
  23 ring_levels = [0.2, 0.4, 0.6, 0.8, 1.0]
  24 theta_fill = np.linspace(0, 2*np.pi, 100)
  25 for k, r in enumerate(ring_levels):
  26     r_prev = ring_levels[k-1] if k > 0 else 0
  27     if k % 2 == 0:
  28         ax.fill_between(theta_fill, r_prev, r, alpha=0.025, color=PALETTE[0], zorder=0)
  29     ax.plot(theta_fill, [r]*len(theta_fill), color='#E5E5E5', linewidth=0.5, zorder=1)
  30 ax.set_yticks(ring_levels)
  31 ax.set_yticklabels(['0.2','0.4','0.6','0.8','1.0'], fontsize=7, color='#C0C0C0')
  32 
  33 ax.set_xticks(angles[:-1])
  34 ax.set_xticklabels(categories, fontsize=10.5, color=COLORS['text'])
  35 ax.tick_params(axis='x', pad=18)
  36 
  37 for i, (name, vals) in enumerate(methods.items()):
  38     values = vals + [vals[0]]
  39     is_ours = i == highlight_index
  40     lw = 2.5 if is_ours else 1.2
  41     alpha_line = 1.0 if is_ours else 0.5
  42     alpha_fill = 0.15 if is_ours else 0.04
  43     ax.plot(angles, values, color=PALETTE[i], linewidth=lw, label=name, alpha=alpha_line, zorder=3)
  44     ax.fill(angles, values, color=PALETTE[i], alpha=alpha_fill, zorder=1)
  45     # 本文方法顶部数值标注
  46     if is_ours:
  47         for j, (a, v) in enumerate(zip(angles[:-1], vals)):
  48             r_label = v + 0.08
  49             ax.text(a, r_label, f'{v:.2f}', ha='center', va='bottom', fontsize=7.5,
  50                     fontweight='bold', color=PALETTE[i],
  51                     bbox=dict(facecolor='white', alpha=0.9, edgecolor='none', pad=1))
  52 
  53 ax.legend(loc='best', bbox_to_anchor=(1.28, 1.06),
  54           frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=9, facecolor='white')
  55 fig.tight_layout()
  56 save_fig(fig, 'figures/fig_radar.pdf')