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 5 setup_style() 6 np.random.seed(42) 7 8 # === 模拟时序:历史 24 个月 + 预测 12 个月 === 9 n_hist = 24; n_pred = 12 10 t = np.arange(n_hist + n_pred) 11 hist = 100 + np.cumsum(np.random.normal(0, 1.5, n_hist)) 12 # 预测:均值随机游走,方差随时间扩散 13 pred_mean = hist[-1] + np.cumsum(np.random.normal(0.3, 0.5, n_pred)) 14 pred_std = np.sqrt(np.arange(1, n_pred + 1)) * 1.6 # 不确定性随时间增长 15 16 # 预测分位数 17 quantiles = { 18 'p05': pred_mean - 1.96 * pred_std, 'p95': pred_mean + 1.96 * pred_std, 19 'p10': pred_mean - 1.28 * pred_std, 'p90': pred_mean + 1.28 * pred_std, 20 'p25': pred_mean - 0.67 * pred_std, 'p75': pred_mean + 0.67 * pred_std, 21 } 22 23 fig, ax = plt.subplots(figsize=(8, 4.5)) 24 25 # === 历史段:实线 === 26 ax.plot(t[:n_hist], hist, color=COLORS['text'], linewidth=1.8, zorder=4, label='历史值') 27 ax.scatter(t[n_hist-1], hist[-1], s=50, color=COLORS['text'], zorder=5, 28 edgecolor='white', linewidth=1) 29 30 # === 预测段:多层 CI 带(外到内 alpha 递增)=== 31 t_pred = t[n_hist-1:] 32 # 拼接:起点用历史最后值,让带子接续 33 pred_full = np.concatenate(([hist[-1]], pred_mean)) 34 quant_full = {k: np.concatenate(([hist[-1]], v)) for k, v in quantiles.items()} 35 36 # 5%-95%(外层,最浅) 37 ax.fill_between(t_pred, quant_full['p05'], quant_full['p95'], 38 color=_lighten(PALETTE[0], 0.65), alpha=0.55, linewidth=0, 39 label='95% 区间', zorder=1) 40 # 10%-90% 41 ax.fill_between(t_pred, quant_full['p10'], quant_full['p90'], 42 color=_lighten(PALETTE[0], 0.4), alpha=0.7, linewidth=0, 43 label='80% 区间', zorder=2) 44 # 25%-75%(内层,最深) 45 ax.fill_between(t_pred, quant_full['p25'], quant_full['p75'], 46 color=_lighten(PALETTE[0], 0.2), alpha=0.8, linewidth=0, 47 label='50% 区间', zorder=3) 48 # 中位数预测线(虚线区别历史) 49 ax.plot(t_pred, pred_full, color=PALETTE[0], linewidth=1.8, linestyle='--', zorder=4, 50 label='中位数预测') 51 52 # === 历史/预测分隔线 === 53 ax.axvline(n_hist - 1, color=COLORS['ref_line'], linestyle=':', linewidth=1.1, alpha=0.6) 54 ax.text(n_hist - 1, ax.get_ylim()[1] * 0.97 if False else hist.max() * 1.02, 55 '预测起点', fontsize=8, ha='center', va='bottom', 56 color=COLORS['ref_line'], style='italic', 57 bbox=dict(boxstyle='round,pad=0.2', facecolor='white', 58 edgecolor=COLORS['grid'], alpha=0.9, linewidth=0.4)) 59 60 # === 终点不确定性范围标注 === 61 y_min = quant_full['p05'][-1]; y_max = quant_full['p95'][-1] 62 ax.annotate('', xy=(t[-1] + 0.2, y_min), xytext=(t[-1] + 0.2, y_max), 63 arrowprops=dict(arrowstyle='<->', color=COLORS['highlight'], lw=1.2)) 64 ax.text(t[-1] + 0.8, (y_min + y_max) / 2, 65 f'95% 跨度\n{y_max - y_min:.1f}', fontsize=8, va='center', 66 color=COLORS['highlight'], fontweight='bold', 67 bbox=dict(boxstyle='round,pad=0.2', facecolor='white', 68 edgecolor=COLORS['highlight'], alpha=0.9, linewidth=0.5)) 69 70 ax.set_xlabel('时间(月)', fontsize=10) 71 ax.set_ylabel('指标值', fontsize=10) 72 ax.legend(loc='upper left', frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8, ncol=2) 73 ax.grid(axis='y', alpha=0.12, linestyle='--', color=COLORS['grid']) 74 ax.spines['top'].set_visible(False) 75 ax.spines['right'].set_visible(False) 76 ax.set_xlim(0, t[-1] + 3) 77 fig.tight_layout() 78 save_fig(fig, 'figures/fig_fan_chart.pdf')