1 import numpy as np 2 import matplotlib.pyplot as plt 3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS 4 setup_style() 5 6 np.random.seed(42) 7 n = 20 8 labels = ['Before', 'After'] 9 before = np.random.normal(75, 8, n) 10 after = before + np.random.normal(5, 4, n) # general improvement with variance 11 12 fig, ax = plt.subplots(figsize=(5, 6)) 13 14 # Individual paired lines 15 for i in range(n): 16 color = PALETTE[0] if after[i] > before[i] else PALETTE[1] 17 ax.plot([0, 1], [before[i], after[i]], color=color, alpha=0.35, 18 linewidth=1.2, zorder=2) 19 ax.scatter([0, 1], [before[i], after[i]], color=color, s=30, 20 edgecolors='white', linewidths=0.5, zorder=3, alpha=0.6) 21 22 # Mean markers (large, prominent) 23 mean_before, mean_after = before.mean(), after.mean() 24 ax.scatter(0, mean_before, s=200, color=PALETTE[1], marker='D', 25 edgecolors='white', linewidths=2, zorder=5, label=f'Mean Before: {mean_before:.1f}') 26 ax.scatter(1, mean_after, s=200, color=PALETTE[0], marker='D', 27 edgecolors='white', linewidths=2, zorder=5, label=f'Mean After: {mean_after:.1f}') 28 29 # Mean shift arrow 30 ax.annotate('', xy=(1, mean_after), xytext=(0, mean_before), 31 arrowprops=dict(arrowstyle='->', color=COLORS['text'], lw=2.5, 32 connectionstyle='arc3,rad=0.15')) 33 delta = mean_after - mean_before 34 ax.text(0.5, (mean_before + mean_after) / 2 + 2, 35 f'Δ = +{delta:.1f}', ha='center', fontsize=10, fontweight='bold', 36 color=PALETTE[0], 37 bbox=dict(boxstyle='round,pad=0.3', facecolor='white', 38 edgecolor=PALETTE[0], alpha=0.9)) 39 40 # Significance annotation 41 from scipy import stats 42 t_stat, p_val = stats.ttest_rel(after, before) 43 sig_text = f'p = {p_val:.4f}' if p_val >= 0.001 else 'p < 0.001' 44 ax.text(0.5, max(max(before), max(after)) + 4, sig_text, 45 ha='center', fontsize=9, fontstyle='italic', color=COLORS['text']) 46 47 ax.set_xticks([0, 1]) 48 ax.set_xticklabels(labels, fontsize=12, fontweight='bold') 49 ax.set_ylabel('Score', fontsize=11) 50 ax.set_xlim(-0.4, 1.4) 51 ax.legend(loc='lower right', fontsize=8, frameon=False, edgecolor=COLORS['grid']) 52 ax.spines['top'].set_visible(False) 53 ax.spines['right'].set_visible(False) 54 ax.grid(axis='y', alpha=0.1, linestyle='--') 55 fig.tight_layout() 56 save_fig(fig, 'figures/fig_paired_dot.pdf')