1 import numpy as np, matplotlib.pyplot as plt 2 from scipy.stats import gaussian_kde, ttest_1samp 3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten 4 setup_style() 5 6 np.random.seed(42) 7 models = ['Ours','LSTM','ARIMA','Prophet'] 8 errors = [np.random.normal(0,1.5,200), np.random.normal(0.5,3,200), 9 np.random.normal(1,4,200), np.random.normal(0.3,2.5,200)] 10 11 # ★ 自适应高度 12 _fig_h = max(4, len(models) * 1.2 + 1) 13 fig, ax = plt.subplots(figsize=(9, _fig_h)) 14 for i, (model, err) in enumerate(zip(models, errors)): 15 y_pos = i * 1.5 16 kde = gaussian_kde(err) 17 x_kde = np.linspace(err.min(), err.max(), 200) 18 y_kde = kde(x_kde); y_kde_norm = y_kde / y_kde.max() * 0.5 19 20 n_layers = 12 21 for k in range(n_layers, 0, -1): 22 frac = k / n_layers 23 ax.fill_between(x_kde, y_pos, y_pos + y_kde_norm*frac, alpha=0.03, color=PALETTE[i], linewidth=0) 24 ax.fill_between(x_kde, y_pos, y_pos + y_kde_norm, alpha=0.2, color=PALETTE[i], linewidth=0) 25 ax.plot(x_kde, y_pos + y_kde_norm, color=PALETTE[i], linewidth=1.2) 26 27 ax.boxplot([err], positions=[y_pos], vert=False, widths=0.18, patch_artist=True, 28 boxprops=dict(facecolor=_lighten(PALETTE[i], 0.4), edgecolor=PALETTE[i], linewidth=1.2), 29 medianprops=dict(color=COLORS['text'], linewidth=1.5), 30 whiskerprops=dict(color=PALETTE[i]), capprops=dict(color=PALETTE[i]), 31 flierprops=dict(marker='.', markersize=2, alpha=0.3)) 32 33 jitter = np.random.uniform(-0.1, -0.4, len(err)) 34 ax.scatter(err[::4], y_pos + jitter[:len(err[::4])], s=5, alpha=0.25, color=PALETTE[i], zorder=2) 35 36 q1, q3 = np.percentile(err, [25, 75]); iqr = q3 - q1 37 outlier_mask = (err < q1-2.5*iqr) | (err > q3+2.5*iqr) 38 outliers = err[outlier_mask] 39 if len(outliers) > 0: 40 ax.scatter(outliers, [y_pos]*len(outliers), s=30, color=COLORS['down'], marker='x', zorder=6, linewidth=1.5) 41 for ov in outliers[:3]: 42 ax.text(ov, y_pos+0.6, f'{ov:.1f}', ha='center', va='bottom', fontsize=7, color=COLORS['down'], fontweight='bold') 43 44 t_stat, p_val = ttest_1samp(err, 0) 45 sig_str = '***' if p_val < 0.001 else ('**' if p_val < 0.01 else ('*' if p_val < 0.05 else 'ns')) 46 ax.text(err.max()+1, y_pos, f't={t_stat:.2f} {sig_str}', ha='left', va='center', fontsize=8, color=COLORS['text'], 47 bbox=dict(boxstyle='round,pad=0.2', facecolor=COLORS['bg_box'], edgecolor=COLORS['grid'], alpha=0.9)) 48 49 ax.axvline(0, color=COLORS['down'], linestyle='--', linewidth=1.0, alpha=0.6) 50 ax.text(0, len(models)*1.5-0.3, 'Zero Error', ha='center', va='bottom', fontsize=8, color=COLORS['down'], 51 bbox=dict(boxstyle='round,pad=0.2', facecolor=COLORS['bg_box'], edgecolor=COLORS['down'], alpha=0.9)) 52 ax.set_yticks([i*1.5 for i in range(len(models))]); ax.set_yticklabels(models, fontsize=10) 53 ax.set_xlabel('Prediction Error', fontsize=11); ax.grid(axis='x', alpha=0.15, linestyle='--') 54 ax.spines['top'].set_visible(False) 55 ax.spines['right'].set_visible(False) 56 fig.tight_layout() 57 save_fig(fig, 'figures/fig_error_raincloud.pdf')