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(-4, 5) 6 coefs = np.array([.01, -.02, .01, .00, .12, .25, .31, .38, .42]) 7 se = np.array([.04, .03, .04, .03, .05, .05, .06, .06, .07]) 8 9 fig, ax = plt.subplots(figsize=(8, 5)) 10 ax.axhline(y=0, color=COLORS['ref_line'], linewidth=0.6, alpha=0.5) 11 ax.axvline(x=-0.5, color=COLORS['down'], linestyle='--', linewidth=1.0, alpha=0.6) 12 ax.axvspan(-4.5, -0.5, color=COLORS['bg_box'], alpha=0.3, zorder=0) 13 ax.axvspan(-0.5, 4.5, color=_lighten(COLORS['highlight'], 0.85), alpha=0.3, zorder=0) 14 15 for t_val, c, s in zip(periods, coefs, se): 16 lo, hi = c - 1.96 * s, c + 1.96 * s 17 base_color = COLORS['ref_line'] if t_val < 0 else PALETTE[0] 18 n_layers = 6 19 for k in range(n_layers, 0, -1): 20 frac = k / n_layers 21 ax.plot([t_val, t_val], [c - frac*1.96*s, c + frac*1.96*s], 22 color=base_color, linewidth=3.5 - k*0.3, alpha=0.08 + 0.04*(n_layers-k) + 0.3, 23 solid_capstyle='round') 24 ax.plot([t_val, t_val], [lo, hi], color=base_color, linewidth=2.5, 25 solid_capstyle='round', alpha=0.8) 26 ax.plot(t_val, c, 'o', color=base_color, markersize=8, 27 markeredgecolor='white', markeredgewidth=1.2, zorder=5) 28 if abs(c) > 1.96 * s: 29 star = '***' if abs(c) > 2.576 * s else '**' 30 ax.text(t_val, hi + 0.02, star, ha='center', va='bottom', 31 fontsize=8, color=base_color, fontweight='bold') 32 33 post_mask = periods >= 0 34 cum_effect = np.cumsum(coefs[post_mask]) 35 ax2 = ax.twinx() 36 ax2.plot(periods[post_mask], cum_effect, '--', color=PALETTE[1], linewidth=1.5, 37 alpha=0.7, label='Cumulative Effect') 38 ax2.set_ylabel('Cumulative Effect', fontsize=10, color=PALETTE[1]) 39 ax2.tick_params(axis='y', labelcolor=PALETTE[1]) 40 ax2.legend(loc='upper left', frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8) 41 42 ax.set_xlabel('Period Relative to Treatment', fontsize=11) 43 ax.set_ylabel('Coefficient', fontsize=11); ax.set_xticks(periods) 44 ax.spines['top'].set_visible(False) 45 ax.spines['right'].set_visible(False) 46 ax.grid(alpha=0.15, linestyle='--') 47 fig.tight_layout() 48 save_fig(fig, 'figures/fig_event.pdf')