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 groups = {
   6     'By Region': {'color': PALETTE[0],
   7         'items': [('East',-.035,.006,True,15.2),('Central',-.022,.008,True,12.8),('West',-.015,.009,False,10.5)],
   8         'pooled': (-.025,.004), 'I2': 38.5, 'Q_p': 0.12},
   9     'By Size': {'color': PALETTE[1],
  10         'items': [('Large',-.028,.005,True,16.1),('Medium',-.019,.007,True,13.4),('Small',-.008,.010,False,8.9)],
  11         'pooled': (-.020,.004), 'I2': 52.1, 'Q_p': 0.04},
  12     'By Ownership': {'color': PALETTE[2],
  13         'items': [('SOE',-.012,.008,False,11.2),('Private',-.031,.006,True,14.7),('Foreign',-.025,.007,True,12.2)],
  14         'pooled': (-.023,.005), 'I2': 45.3, 'Q_p': 0.08},
  15 }
  16 
  17 # ★ 自适应高度
  18 _total_items = sum(len(gd['items']) for gd in groups.values()) + len(groups) * 2
  19 _fig_h = max(6, _total_items * 0.65 + 2)
  20 fig, ax = plt.subplots(figsize=(10, _fig_h)); yp = 0
  21 for gn, gd in groups.items():
  22     gc = gd['color']; yp -= 0.5
  23     ax.axhspan(yp-0.4, yp+0.4, color=gc, alpha=0.08, zorder=0)
  24     ax.text(-0.065, yp, f'■ {gn}', ha='left', va='center', fontsize=10.5, fontweight='bold', color=gc)
  25     yp -= 1.2
  26     for lb, c, s, sig, wt in gd['items']:
  27         lo, hi = c-1.96*s, c+1.96*s
  28         ax.plot([lo, hi], [yp, yp], color=COLORS['text'], linewidth=1.0, zorder=2)
  29         ax.plot([lo, lo], [yp-.12, yp+.12], color=COLORS['text'], linewidth=1.0)
  30         ax.plot([hi, hi], [yp-.12, yp+.12], color=COLORS['text'], linewidth=1.0)
  31         ms = 4 + wt/3; fc = gc if sig else 'white'; ec = gc if sig else COLORS['ref_line']
  32         ax.plot(c, yp, 'o', color=fc, markersize=ms, markeredgecolor=ec, markeredgewidth=1.2, zorder=3)
  33         ax.text(-0.065, yp, f'  {lb}', ha='left', va='center', fontsize=9, color=COLORS['text'])
  34         ax.text(0.065, yp, f'{wt:.1f}%', ha='right', va='center', fontsize=8, color=COLORS['ref_line'])
  35         yp -= 1.3
  36     pc, ps = gd['pooled']; plo, phi = pc-1.96*ps, pc+1.96*ps; dh = 0.3
  37     ax.fill([plo,pc,phi,pc], [yp,yp+dh,yp,yp-dh], color=gc, alpha=0.7, zorder=4)
  38     ax.plot([plo,pc,phi,pc,plo], [yp,yp+dh,yp,yp-dh,yp], color=gc, linewidth=0.8, zorder=5)
  39     ax.text(-0.065, yp, '  Pooled', ha='left', va='center', fontsize=9, fontweight='bold', color=gc)
  40     ax.text(0.065, yp, f'I²={gd["I2"]:.1f}%, Q p={gd["Q_p"]:.3f}', ha='right', va='center',
  41             fontsize=7.5, color=COLORS['ref_line'], fontstyle='italic')
  42     yp -= 1.8
  43 
  44 ax.axvline(x=0, color=COLORS['down'], linestyle='--', linewidth=1.5, alpha=0.7, zorder=1)
  45 ax.set_yticks([]); ax.set_xlabel('Coefficient', fontsize=12)
  46 ax.spines['top'].set_visible(False)
  47 ax.spines['right'].set_visible(False)
  48 ax.spines['left'].set_visible(False); ax.set_ylim(yp-0.5, 0.5); ax.invert_yaxis()
  49 ax.grid(axis='x', alpha=0.15, linestyle='--')
  50 fig.tight_layout()
  51 save_fig(fig, 'figures/fig_subgroup.pdf')