1 import numpy as np
   2 import matplotlib.pyplot as plt
   3 import matplotlib.colors as mcolors
   4 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   5 setup_style()
   6 
   7 configs = ['Full Model', '- Attention', '- Residual', '- Augment', '- Attn & Res', 'Baseline']
   8 scores = [92.3, 89.1, 90.5, 88.7, 85.2, 82.0]
   9 drops = [0, 3.2, 1.8, 3.6, 7.1, 10.3]
  10 
  11 fig, ax = plt.subplots(figsize=(8, 5))
  12 
  13 # Gradient coloring: green (high) → red (low)
  14 cmap = plt.cm.YlOrRd
  15 norm = plt.Normalize(min(scores) - 2, max(scores) + 2)
  16 colors = [cmap(norm(s)) for s in scores]
  17 
  18 bars = ax.bar(range(len(configs)), scores, color=[_lighten(c, 0.35) for c in colors],
  19               edgecolor=colors, linewidth=1.2, width=0.65, zorder=3)
  20 
  21 # Waterfall connector lines between bars
  22 for j in range(len(configs) - 1):
  23     ax.plot([j + 0.325, j + 0.675], [scores[j], scores[j]],
  24             color=COLORS['ref_line'], linewidth=0.8, linestyle='--', alpha=0.5, zorder=2)
  25 
  26 # Value labels on top + delta arrows inside bars
  27 for j, (bar, score, drop) in enumerate(zip(bars, scores, drops)):
  28     ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 0.3,
  29             f'{score:.1f}', ha='center', va='bottom', fontsize=9.5, fontweight='bold')
  30     if drop > 0:
  31         # Delta arrow annotation
  32         ax.annotate(f'↓{drop:.1f}',
  33                     xy=(bar.get_x() + bar.get_width() / 2, bar.get_height() - 1),
  34                     fontsize=8, ha='center', va='top', color='white', fontweight='bold',
  35                     bbox=dict(boxstyle='round,pad=0.15', facecolor=COLORS['down'],
  36                               alpha=0.7, edgecolor='none'))
  37 
  38 # Highlight Full Model and Baseline
  39 for idx in [0, len(configs) - 1]:
  40     bars[idx].set_edgecolor(PALETTE[0] if idx == 0 else PALETTE[3])
  41     bars[idx].set_linewidth(2)
  42 
  43 ax.set_xticks(range(len(configs)))
  44 ax.set_xticklabels(configs, rotation=18, ha='right', fontsize=9)
  45 ax.set_ylabel('Accuracy (%)', fontsize=11)
  46 ax.set_ylim(78, 96)
  47 ax.spines['top'].set_visible(False)
  48 ax.spines['right'].set_visible(False)
  49 ax.grid(axis='y', alpha=0.15, linestyle='--')
  50 
  51 # Dashed line connecting Full → Baseline
  52 ax.plot([0, len(configs) - 1], [scores[0], scores[-1]], '--',
  53         color=COLORS['ref_line'], linewidth=1, alpha=0.4, zorder=1)
  54 ax.text(0.98, 0.05, f'Total drop: {scores[0] - scores[-1]:.1f}',
  55         transform=ax.transAxes, fontsize=8, color=COLORS['ref_line'],
  56         ha='right', va='bottom', style='italic',
  57         bbox=dict(boxstyle='round,pad=0.3', facecolor='white',
  58                   edgecolor=COLORS['grid'], alpha=0.9))
  59 
  60 fig.tight_layout()
  61 save_fig(fig, 'figures/fig_ablation.pdf')