1 import numpy as np
   2 import matplotlib.pyplot as plt
   3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   4 setup_style()
   5 
   6 # === Example data ===
   7 periods = np.arange(1, 21)
   8 # Variance decomposition (rows = components, cols = periods)
   9 own = np.array([100, 95, 90, 87, 85, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69])
  10 var2 = np.array([0, 3, 5, 7, 8, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16])
  11 var3 = 100 - own - var2
  12 names = ['自身冲击', 'GPR指数', 'GSCPI']
  13 
  14 fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 7), height_ratios=[1, 1], sharex=True)
  15 
  16 # Top panel: full stacked area
  17 ax1.stackplot(periods, own, var2, var3,
  18               colors=[_lighten(PALETTE[0], 0.3), _lighten(PALETTE[1], 0.3), _lighten(PALETTE[2], 0.3)],
  19               alpha=0.8, labels=names)
  20 ax1.plot(periods, own, color=PALETTE[0], linewidth=1.5)
  21 ax1.plot(periods, own + var2, color=PALETTE[1], linewidth=1.5)
  22 ax1.set_ylabel('方差贡献比例 (%)', fontsize=10)
  23 ax1.set_ylim(0, 100)
  24 ax1.legend(fontsize=8, frameon=False, labelspacing=0.35, handlelength=1.6, loc='center right')
  25 ax1.text(-0.05, 1.06, '(a) 完整方差分解', transform=ax1.transAxes, fontsize=10, fontweight='bold')
  26 ax1.spines['top'].set_visible(False)
  27 ax1.spines['right'].set_visible(False)
  28 
  29 # Bottom panel: zoom into minor components only
  30 ax2.fill_between(periods, 0, var2, alpha=0.4, color=PALETTE[1], label=names[1])
  31 ax2.fill_between(periods, var2, var2 + var3, alpha=0.4, color=PALETTE[2], label=names[2])
  32 ax2.plot(periods, var2, color=PALETTE[1], linewidth=2, marker='o', markersize=4,
  33          markeredgecolor='white', markeredgewidth=0.8)
  34 ax2.plot(periods, var2 + var3, color=PALETTE[2], linewidth=2, marker='s', markersize=4,
  35          markeredgecolor='white', markeredgewidth=0.8)
  36 
  37 # Value labels at end
  38 ax2.text(periods[-1] + 0.3, var2[-1], f'{var2[-1]:.1f}%', va='center', fontsize=8,
  39          color=PALETTE[1], fontweight='bold')
  40 ax2.text(periods[-1] + 0.3, var2[-1] + var3[-1], f'{var3[-1]:.1f}%', va='center', fontsize=8,
  41          color=PALETTE[2], fontweight='bold')
  42 
  43 ax2.set_ylabel('方差贡献比例 (%)', fontsize=10)
  44 ax2.set_xlabel('预测期', fontsize=11)
  45 ax2.legend(fontsize=8, frameon=False, labelspacing=0.35, handlelength=1.6, loc='upper left')
  46 ax2.text(-0.05, 1.06, '(b) 非自身冲击成分(放大)', transform=ax2.transAxes, fontsize=10, fontweight='bold')
  47 ax2.spines['top'].set_visible(False)
  48 ax2.spines['right'].set_visible(False)
  49 
  50 fig.tight_layout()
  51 save_fig(fig, 'figures/fig_variance_decomp.pdf')