1 import numpy as np
   2 import matplotlib.pyplot as plt
   3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   4 setup_style()
   5 
   6 categories = ['指标A', '指标B', '指标C', '指标D']
   7 groups = {
   8     '本文方法': [85.2, 78.3, 92.1, 88.5],
   9     '方法B':   [82.1, 80.5, 88.7, 85.2],
  10     '方法C':   [79.8, 75.2, 85.3, 82.0],
  11 }
  12 stds = {
  13     '本文方法': [1.2, 1.5, 0.8, 1.0],
  14     '方法B':   [1.8, 2.0, 1.3, 1.5],
  15     '方法C':   [2.1, 2.5, 1.6, 1.8],
  16 }
  17 
  18 fig, ax = plt.subplots(figsize=(9, 5.5))
  19 highlight_index = 0  # 重点系列的位置,与显示名称无关
  20 x = np.arange(len(categories))
  21 n = len(groups)
  22 width = 0.22
  23 
  24 # 淡蓝背景渐变
  25 ax.axhspan(0, max(max(v) for v in groups.values()) * 1.2, alpha=0.03, color=PALETTE[0], zorder=0)
  26 
  27 for i, (name, vals) in enumerate(groups.items()):
  28     offset = (i - n / 2 + 0.5) * width
  29     is_ours = i == highlight_index
  30     # 柱子阴影
  31     ax.bar(x + offset + 0.02, vals, width, color='#cccccc', alpha=0.08, zorder=1)
  32     # ★ 主柱子:淡色填充 + 原色边框
  33     bars = ax.bar(x + offset, vals, width, yerr=stds[name], capsize=3,
  34                   color=_lighten(PALETTE[i], 0.4), edgecolor=PALETTE[i],
  35                   linewidth=1.5 if is_ours else 1.2,
  36                   label=name, zorder=2,
  37                   error_kw={'elinewidth': 0.8, 'capthick': 0.6, 'color': COLORS['text']})
  38 
  39     for j, (bar, v, s) in enumerate(zip(bars, vals, stds[name])):
  40         all_vals_j = [groups[g][j] for g in groups]
  41         is_best = (v == max(all_vals_j))
  42         ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + s + 0.5,
  43                 f'{"★" if is_best else ""}{v:.1f}',
  44                 ha='center', va='bottom', fontsize=7.5,
  45                 fontweight='bold' if is_best else 'normal',
  46                 color=PALETTE[i] if is_best else COLORS['text'],
  47                 bbox=dict(boxstyle='round,pad=0.1', facecolor='white', edgecolor='none', alpha=0.7) if is_best else {})
  48 
  49 # 水平参考线:全局均值
  50 global_mean = np.mean([v for vals in groups.values() for v in vals])
  51 ax.axhline(y=global_mean, color=COLORS['ref_line'], linestyle='--', linewidth=0.8, alpha=0.4)
  52 ax.text(len(categories) - 0.3, global_mean + 0.5, f'均值 {global_mean:.1f}',
  53         fontsize=8, color=COLORS['ref_line'], ha='right', style='italic',
  54         bbox=dict(boxstyle='round,pad=0.2', facecolor='white', edgecolor='none', alpha=0.8))
  55 
  56 ax.set_xticks(x)
  57 ax.set_xticklabels(categories, fontsize=10)
  58 ax.set_ylabel('得分', fontsize=11)
  59 ax.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=9, loc='best')
  60 ax.set_ylim(0, max(max(v) for v in groups.values()) * 1.2)
  61 ax.grid(axis='y', alpha=0.12, linestyle='--', color=COLORS['grid'])
  62 ax.spines['top'].set_visible(False)
  63 ax.spines['right'].set_visible(False)
  64 fig.tight_layout()
  65 save_fig(fig, 'figures/fig_grouped_bar.pdf')