1 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten 2 setup_style() 3 import matplotlib.pyplot as plt 4 import numpy as np 5 6 metrics = ['Accuracy', 'Precision', 'Recall', 'F1', 'AUC'] 7 before = [0.82, 0.79, 0.85, 0.81, 0.88] 8 after = [0.91, 0.88, 0.90, 0.89, 0.94] 9 10 # ── 自适应高度 11 _fig_h = max(3.5, len(metrics) * 0.7 + 1) 12 fig, ax = plt.subplots(figsize=(8, _fig_h)) 13 y = np.arange(len(metrics)) 14 15 # Subtle grid 16 ax.grid(axis='x', alpha=0.15, linestyle='-', color=COLORS['grid']) 17 ax.set_axisbelow(True) 18 19 # Simple connector lines + arrow heads 20 for i in range(len(metrics)): 21 delta = after[i] - before[i] 22 pct_change = delta / before[i] * 100 23 # Simple connector line 24 ax.plot([before[i], after[i]], [y[i], y[i]], 25 color=PALETTE[2], linewidth=2.0, solid_capstyle='round') 26 # Arrow head at the "after" end 27 ax.annotate('', xy=(after[i], y[i]), xytext=(after[i] - 0.012, y[i]), 28 arrowprops=dict(arrowstyle='->', color=PALETTE[0], lw=2.0)) 29 30 # Before / After dots 31 ax.scatter(before[i], y[i], color=PALETTE[3], s=80, zorder=3, 32 edgecolors='white', linewidths=1.0, label='Before' if i == 0 else '') 33 ax.scatter(after[i], y[i], color=PALETTE[0], s=80, zorder=3, 34 edgecolors='white', linewidths=1.0, label='After' if i == 0 else '') 35 36 # % change label 37 ax.text(after[i] + 0.015, y[i] - 0.15, f'+{delta:.2f} ({pct_change:+.1f}%)', 38 va='center', fontsize=8, color=PALETTE[0], fontweight='bold') 39 40 # Significance marker (stars) 41 if pct_change > 8: 42 sig = '***' 43 elif pct_change > 5: 44 sig = '**' 45 else: 46 sig = '*' 47 ax.text(after[i] + 0.015, y[i] + 0.2, sig, va='center', fontsize=9, 48 color=COLORS['down'], fontweight='bold') 49 50 ax.set_yticks(y) 51 ax.set_yticklabels(metrics, fontsize=10) 52 ax.set_xlabel('Score', fontsize=11) 53 ax.legend(loc='lower right', frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=9, 54 fancybox=True, shadow=False) 55 ax.invert_yaxis() 56 ax.spines['top'].set_visible(False) 57 ax.spines['right'].set_visible(False) 58 fig.tight_layout() 59 save_fig(fig, 'figures/fig_dumbbell.pdf')