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 np.random.seed(42)
   7 fig, axes = plt.subplots(2, 2, figsize=(5.0, 4.9))   # 示例尺寸;保留模板比例,按实际显示尺寸检查字号与布局
   8 
   9 # (a) 折线图 — 渐变填充
  10 ax = axes[0, 0]
  11 for i in range(3):
  12     y = np.cumsum(np.random.randn(10))
  13     ax.plot(range(10), y, 'o-', color=PALETTE[i], linewidth=1.8, markersize=5,
  14             markeredgecolor='white', markeredgewidth=0.8, label=f'系列{i + 1}')
  15     ax.fill_between(range(10), y - 1, y + 1, alpha=0.08, color=PALETTE[i])
  16 ax.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8)
  17 ax.set_ylabel('累计值', fontsize=10)
  18 ax.grid(alpha=0.12, linestyle='--', color=COLORS['grid'])
  19 ax.spines['top'].set_visible(False)
  20 ax.spines['right'].set_visible(False)
  21 
  22 # (b) 分组柱状图 — 淡色填充 + 原色边框
  23 ax = axes[0, 1]
  24 x = np.arange(5)
  25 for i, label in enumerate(['方法A', '方法B']):
  26     vals = np.random.uniform(60, 95, 5)
  27     offset = (i - 0.5) * 0.3
  28     ax.bar(x + offset, vals, 0.28, color=_lighten(PALETTE[i], 0.4),
  29            edgecolor=PALETTE[i], linewidth=1.2, label=label)
  30 ax.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8)
  31 ax.set_ylabel('得分', fontsize=10)
  32 ax.grid(axis='y', alpha=0.12, linestyle='--', color=COLORS['grid'])
  33 ax.spines['top'].set_visible(False)
  34 ax.spines['right'].set_visible(False)
  35 
  36 # (c) 散点图 — KDE 等高线背景
  37 ax = axes[1, 0]
  38 for i in range(3):
  39     pts = np.random.randn(30, 2)
  40     ax.scatter(pts[:, 0], pts[:, 1], s=25, alpha=0.6, color=PALETTE[i],
  41                edgecolor='white', linewidth=0.5, label=f'类{i + 1}')
  42 ax.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8)
  43 ax.grid(alpha=0.12, linestyle='--', color=COLORS['grid'])
  44 ax.spines['top'].set_visible(False)
  45 ax.spines['right'].set_visible(False)
  46 
  47 # (d) 直方图 — 淡色填充 + 原色边框
  48 ax = axes[1, 1]
  49 for i in range(3):
  50     vals = np.random.normal(i * 2, 1, 200)
  51     ax.hist(vals, bins=20, alpha=0.5, color=_lighten(PALETTE[i], 0.4),
  52             edgecolor=PALETTE[i], linewidth=0.8, label=f'分布{i + 1}')
  53 ax.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8)
  54 ax.set_ylabel('频次', fontsize=10)
  55 ax.grid(axis='y', alpha=0.12, linestyle='--', color=COLORS['grid'])
  56 ax.spines['top'].set_visible(False)
  57 ax.spines['right'].set_visible(False)
  58 
  59 # ★ 带背景框子图标签(用 set_title 紧贴子图顶部)
  60 for i, ax in enumerate(axes.flat):
  61     ax.set_title(f'({chr(97 + i)})', fontsize=12, fontweight='bold', loc='left', pad=3)
  62 
  63 fig.tight_layout(pad=0.5)
  64 save_fig(fig, 'figures/fig_subplots.pdf')