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 = ['Accuracy', 'Precision', 'Recall', 'F1', 'AUC', 'MCC']
   7 group_a = [92.3, 89.1, 94.5, 91.7, 96.2, 88.4]  # e.g., "Ours"
   8 group_b = [87.5, 84.2, 90.1, 87.0, 93.1, 82.6]  # e.g., "Baseline"
   9 
  10 # ── 自适应高度
  11 _fig_h = max(4, len(categories) * 0.7 + 1)
  12 fig, ax = plt.subplots(figsize=(8, _fig_h))
  13 y_pos = np.arange(len(categories))
  14 
  15 # ── 左侧(负方向)= Group B —— 浅色填充 + 原色边框
  16 bars_b = ax.barh(y_pos, [-v for v in group_b], height=0.55,
  17                  color=_lighten(PALETTE[1], 0.4), edgecolor=PALETTE[1],
  18                  linewidth=1.2, label='Baseline', zorder=3)
  19 # ── 右侧(正方向)= Group A —— 浅色填充 + 原色边框
  20 bars_a = ax.barh(y_pos, group_a, height=0.55,
  21                  color=_lighten(PALETTE[0], 0.4), edgecolor=PALETTE[0],
  22                  linewidth=1.2, label='Ours', zorder=3)
  23 
  24 # Value labels
  25 for i in range(len(categories)):
  26     ax.text(group_a[i] + 0.5, y_pos[i], f'{group_a[i]:.1f}',
  27             va='center', ha='left', fontsize=8.5, color=PALETTE[0], fontweight='bold')
  28     ax.text(-group_b[i] - 0.5, y_pos[i], f'{group_b[i]:.1f}',
  29             va='center', ha='right', fontsize=8.5, color=PALETTE[1], fontweight='bold')
  30     # Gap label in center
  31     gap = group_a[i] - group_b[i]
  32     sign = '+' if gap > 0 else ''
  33     ax.text(0, y_pos[i], f'{sign}{gap:.1f}', va='center', ha='center',
  34             fontsize=7.5, fontweight='bold', color=COLORS['text'],
  35             bbox=dict(boxstyle='round,pad=0.2', facecolor='white',
  36                       edgecolor=COLORS['grid'], alpha=0.9))
  37 
  38 ax.set_yticks(y_pos)
  39 ax.set_yticklabels(categories, fontsize=10)
  40 ax.axvline(0, color=COLORS['text'], linewidth=0.8)
  41 ax.set_xlabel('Score (%)', fontsize=11)
  42 ax.legend(loc='lower right', fontsize=9, frameon=False, edgecolor=COLORS['grid'])
  43 
  44 # Clean up x-axis to show absolute values
  45 ticks = ax.get_xticks()
  46 ax.set_xticklabels([f'{abs(t):.0f}' for t in ticks])
  47 ax.invert_yaxis()
  48 ax.spines['top'].set_visible(False)
  49 ax.spines['right'].set_visible(False)
  50 ax.grid(axis='x', alpha=0.1, linestyle='--')
  51 fig.tight_layout()
  52 save_fig(fig, 'figures/fig_back2back.pdf')