1 import numpy as np, matplotlib.pyplot as plt 2 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten 3 setup_style() 4 5 periods = np.arange(-5, 6) 6 treat = np.array([.02, -.01, .03, -.02, .01, .15, .28, .35, .42, .50, .55]) 7 ctrl = np.array([.01, .00, .02, -.01, .00, .02, .03, .01, .02, .00, .01]) 8 se_t = np.full(11, .04) 9 se_c = np.full(11, .03) 10 11 fig, ax = plt.subplots(figsize=(9, 5.5)) 12 13 for x_start, x_end, c_base, lbl in [(-5.5, -0.5, _lighten(COLORS['up'], 0.8), 'Pre-Treatment'), 14 (-0.5, 5.5, _lighten(COLORS['highlight'], 0.8), 'Post-Treatment')]: 15 ax.axvspan(x_start, x_end, color=c_base, alpha=0.25, zorder=0) 16 cx = (x_start + x_end) / 2 17 ax.text(cx, -0.15, lbl, ha='center', va='bottom', fontsize=9, color=COLORS['ref_line'], 18 bbox=dict(boxstyle='round,pad=0.3', facecolor='white', 19 edgecolor=COLORS['grid'], alpha=0.9)) 20 21 ax.axvline(x=-0.5, color=COLORS['down'], linestyle='--', linewidth=1.3, alpha=0.7) 22 ax.text(-0.5, 0.62, 'Treatment', ha='center', va='bottom', fontsize=9, 23 color=COLORS['down'], fontweight='bold', 24 bbox=dict(boxstyle='round,pad=0.25', facecolor=COLORS['bg_box'], 25 edgecolor=COLORS['down'], alpha=0.9)) 26 27 n_layers = 8 28 for k in range(n_layers, 0, -1): 29 frac = k / n_layers 30 ax.fill_between(periods, treat - frac*1.96*se_t, treat + frac*1.96*se_t, 31 alpha=0.04, color=PALETTE[0], linewidth=0) 32 ax.fill_between(periods, ctrl - frac*1.96*se_c, ctrl + frac*1.96*se_c, 33 alpha=0.04, color=PALETTE[1], linewidth=0) 34 35 ax.plot(periods, treat, 'o-', color=PALETTE[0], markersize=7, linewidth=2.2, 36 label='Treatment', markeredgecolor='white', markeredgewidth=1, zorder=5) 37 ax.plot(periods, ctrl, 's--', color=PALETTE[1], markersize=6, linewidth=1.8, 38 label='Control', markeredgecolor='white', markeredgewidth=1, zorder=5) 39 40 ax.annotate(f'ATT = {treat[-1]-ctrl[-1]:.2f}', 41 xy=(periods[-1], treat[-1]), 42 xytext=(periods[-1] - 1.5, treat[-1] + 0.12), 43 fontsize=9, fontweight='bold', color=PALETTE[0], 44 arrowprops=dict(arrowstyle='->', color=PALETTE[0], lw=1.5, 45 connectionstyle='arc3,rad=-0.2'), 46 bbox=dict(boxstyle='round,pad=0.3', facecolor='white', 47 edgecolor=PALETTE[0], alpha=0.9)) 48 49 ax.set_xlabel('Period', fontsize=11); ax.set_ylabel('Outcome', fontsize=11) 50 ax.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=9, loc='upper left') 51 ax.spines['top'].set_visible(False) 52 ax.spines['right'].set_visible(False) 53 ax.grid(alpha=0.15, linestyle='--') 54 fig.tight_layout() 55 save_fig(fig, 'figures/fig_parallel.pdf')