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 np.random.seed(42)
   7 n_studies = 30
   8 effects = np.random.normal(0.5, 0.3, n_studies)
   9 se = np.random.uniform(0.05, 0.4, n_studies)
  10 effects += np.random.normal(0, se)
  11 study_names = [f'Study {i+1}' for i in range(n_studies)]
  12 
  13 fig, ax = plt.subplots(figsize=(7, 5.5))
  14 
  15 # Subtle grid
  16 ax.grid(alpha=0.15, linestyle='-', color=COLORS['grid'])
  17 ax.set_axisbelow(True)
  18 
  19 mean_eff = np.mean(effects)
  20 se_range = np.linspace(0, 0.45, 200)
  21 
  22 # Gradient CI bands (99%, 95%, 90%)
  23 ci_levels = [
  24     (2.576, '99% CI', PALETTE[2], 0.06),
  25     (1.960, '95% CI', PALETTE[1], 0.08),
  26     (1.645, '90% CI', PALETTE[0], 0.10),
  27 ]
  28 for z_val, label, color, alpha in ci_levels:
  29     ax.fill_betweenx(se_range,
  30                       mean_eff - z_val * se_range,
  31                       mean_eff + z_val * se_range,
  32                       alpha=alpha, color=color, label=label)
  33 
  34 # Pooled effect line
  35 ax.axvline(mean_eff, color=PALETTE[0], linestyle='-', linewidth=1.8, alpha=0.6)
  36 
  37 # Study points
  38 ax.scatter(effects, se, color=PALETTE[0], s=50, alpha=0.8, edgecolors='white',
  39            linewidths=0.8, zorder=4)
  40 
  41 # Study labels for outliers (outside 95% CI)
  42 for i in range(n_studies):
  43     if abs(effects[i] - mean_eff) > 1.96 * se[i] * 1.5:
  44         ax.annotate(study_names[i],
  45                     xy=(effects[i], se[i]),
  46                     xytext=(effects[i] + 0.08, se[i] - 0.03),
  47                     fontsize=7, color=COLORS['down'],
  48                     arrowprops=dict(arrowstyle='->', color=COLORS['down'], lw=0.7),
  49                     bbox=dict(boxstyle='round,pad=0.15', facecolor=COLORS['bg_box'],
  50                               edgecolor=COLORS['down'], alpha=0.7))
  51 
  52 # Egger's test annotation
  53 ax.annotate("Egger's test: t = 1.42, p = 0.167\nNo significant asymmetry",
  54             xy=(0.02, 0.02), xycoords='axes fraction',
  55             fontsize=8, va='bottom',
  56             bbox=dict(boxstyle='round,pad=0.4', facecolor=_lighten(PALETTE[0], 0.7),
  57                       edgecolor=PALETTE[0], alpha=0.9))
  58 
  59 ax.set_xlabel('Effect size', fontsize=11)
  60 ax.set_ylabel('Standard error', fontsize=11)
  61 ax.invert_yaxis()
  62 ax.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8, loc='best',
  63           fancybox=True)
  64 ax.spines['top'].set_visible(False)
  65 ax.spines['right'].set_visible(False)
  66 fig.tight_layout()
  67 save_fig(fig, 'figures/fig_funnel.pdf')