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 labels = ['低固定资产组', '高固定资产组', '低负债组', '高负债组', 6 '低资产负债率组', '高资产负债率组', '低流动性组', '高流动性组'] 7 coefs = np.array([-0.028,-0.042,-0.030,-0.031,-0.035,-0.025,-0.032,-0.033]) 8 ci_lo = np.array([-0.048,-0.058,-0.055,-0.052,-0.052,-0.010,-0.048,-0.046]) 9 ci_hi = np.array([-0.008,-0.026,-0.005,-0.010,-0.018,-0.040,-0.016,-0.020]) 10 weights = np.array([12.8, 14.2, 10.5, 11.3, 13.1, 9.7, 14.6, 13.8]) 11 significant = [True, True, False, False, True, True, False, False] 12 13 pooled_coef = np.average(coefs, weights=weights) 14 pooled_se = 0.004 15 pooled_lo = pooled_coef - 1.96 * pooled_se 16 pooled_hi = pooled_coef + 1.96 * pooled_se 17 18 # ★ 自适应高度:变量多时不会过度拉伸 19 _fig_h = max(5, len(labels) * 0.7 + 2) 20 fig, ax = plt.subplots(figsize=(11, _fig_h)) 21 y = np.arange(len(labels)) * 1.4 22 23 # 交替行阴影 24 for i in range(len(labels)): 25 if i % 2 == 0: 26 ax.axhspan(y[i] - 0.55, y[i] + 0.55, color=COLORS['bg_box'], zorder=0) 27 28 # 零线参考 29 ax.axvline(x=0, color=COLORS['down'], linestyle='--', linewidth=1.5, alpha=0.7, zorder=1) 30 31 for i, (c, lo, hi, lbl, sig, wt) in enumerate(zip(coefs, ci_lo, ci_hi, labels, significant, weights)): 32 # CI 线 + 端点帽 33 ax.plot([lo, hi], [y[i], y[i]], color=COLORS['text'], linewidth=1.2, zorder=2, solid_capstyle='round') 34 ax.plot([lo, lo], [y[i]-0.12, y[i]+0.12], color=COLORS['text'], linewidth=1.0) 35 ax.plot([hi, hi], [y[i]-0.12, y[i]+0.12], color=COLORS['text'], linewidth=1.0) 36 37 # 权重比例圆点(显著=实心,不显著=空心) 38 fc = PALETTE[0] if sig else 'white' 39 ec = PALETTE[0] if sig else COLORS['ref_line'] 40 ms = 5 + wt / 4 41 ax.plot(c, y[i], 'o', color=fc, markersize=ms, markeredgecolor=ec, 42 markeredgewidth=1.3, zorder=3) 43 44 # 左侧标签 45 ax.text(-0.065, y[i], lbl, ha='right', va='center', fontsize=9, color=COLORS['text']) 46 47 # 右侧数值列:系数 [95% CI] + 显著性★ 48 sig_mark = '***' if sig else '' 49 ax.text(0.085, y[i], f'{c:.3f} [{lo:.3f}, {hi:.3f}]{sig_mark}', 50 ha='left', va='center', fontsize=8, fontfamily='monospace', 51 color=PALETTE[0] if sig else COLORS['ref_line'], 52 fontweight='bold' if sig else 'normal') 53 54 # 权重列 55 ax.text(0.16, y[i], f'{wt:.1f}%', ha='right', va='center', 56 fontsize=8, color=COLORS['text']) 57 58 # 列标题 59 ax.text(-0.065, max(y) + 1.0, '分组', ha='right', va='center', 60 fontsize=9.5, fontweight='bold', color=COLORS['text']) 61 ax.text(0.085, max(y) + 1.0, '系数 [95% CI]', ha='left', va='center', 62 fontsize=9.5, fontweight='bold', color=COLORS['text']) 63 ax.text(0.16, max(y) + 1.0, '权重', ha='right', va='center', 64 fontsize=9.5, fontweight='bold', color=COLORS['text']) 65 66 # 菱形 pooled estimate 67 diamond_y = -1.5 68 dh = 0.35 69 diamond_x = [pooled_lo, pooled_coef, pooled_hi, pooled_coef] 70 diamond_yy = [diamond_y, diamond_y + dh, diamond_y, diamond_y - dh] 71 ax.fill(diamond_x, diamond_yy, color=PALETTE[1], alpha=0.85, zorder=4) 72 ax.plot(diamond_x + [diamond_x[0]], diamond_yy + [diamond_yy[0]], 73 color=COLORS['text'], linewidth=0.8, zorder=5) 74 ax.text(-0.065, diamond_y, 'Pooled', ha='right', va='center', 75 fontsize=9.5, fontweight='bold', color=PALETTE[1]) 76 ax.text(0.085, diamond_y, f'{pooled_coef:.3f} [{pooled_lo:.3f}, {pooled_hi:.3f}]***', 77 ha='left', va='center', fontsize=8, fontfamily='monospace', 78 fontweight='bold', color=PALETTE[1]) 79 80 # I² 异质性标注框 81 i2_text = 'I² = 42.3%, Q = 13.8 (p = 0.055)' 82 ax.text(0.0, diamond_y - 1.0, i2_text, ha='center', va='top', fontsize=8.5, 83 bbox=dict(boxstyle='round,pad=0.4', facecolor=_lighten(COLORS['highlight'], 0.85), 84 edgecolor=_lighten(COLORS['highlight'], 0.4), alpha=0.95), 85 color=COLORS['text']) 86 87 ax.set_yticks([]) 88 ax.set_xlabel('回归系数', fontsize=12) 89 ax.spines['top'].set_visible(False) 90 ax.spines['right'].set_visible(False) 91 ax.spines['left'].set_visible(False) 92 ax.set_ylim(diamond_y - 1.8, max(y) + 1.5) 93 ax.set_xlim(-0.07, 0.17) 94 ax.invert_yaxis() 95 ax.grid(axis='x', alpha=0.15, linestyle='--') 96 fig.tight_layout() 97 save_fig(fig, 'figures/fig_forest.pdf')