固定区域子图
plt.subplots(rows, cols) 一次生成规则网格,通过 ax[i, j] 索引访问每个子图。
自定义区域
GridSpec + add_subplot 可让某个子图占据多行多列,实现不等分的杂志式排版。
共享坐标轴
sharex / sharey 让子图共用刻度,tight_layout 自动消除紧凑布局的重叠。
子图布局调整
subplots_adjust 手动微调间距;suptitle 为整个画布添加总标题。
◈ 子图组合示例
# 固定区域 2×2 子图
fig, ax = plt.subplots(2, 2, figsize=(12, 8), sharex=True)
ax[0,0].plot(x, y); ax[0,1].bar(cats, vals)
ax[1,0].scatter(x, y); ax[1,1].hist(data, bins=15)
fig.suptitle("组合视图", fontsize=16)
fig.tight_layout()
# 自定义区域:上宽下窄
gs = fig.add_gridspec(2, 2)
ax_main = fig.add_subplot(gs[0, :]) # 占满第一行
ax_side = fig.add_subplot(gs[1, 0]) # 左下
ax_side2 = fig.add_subplot(gs[1, 1]) # 右下