You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在for循环中创建共享X轴、无垂直间距的seaborn单幅组合图

问题原因
  • 原循环代码每次调用plt.figure()都会生成全新独立画布,因此会输出多张分散图表,无法对齐坐标轴、统一保存
  • 原FacetGrid写法存在逻辑错误:map方法中直接传入循环最后一列的固定y值,没有做分面维度映射,因此只会渲染最后一列数据
实现代码

直接通过matplotlib创建垂直排列的子图网格,配合seaborn在对应子图上绘图即可,完美适配1-30列动态变化、共享X轴、无垂直间距的需求:

import matplotlib.pyplot as plt
import seaborn as sns

# 保留原有数据预处理逻辑
comp_relflux = measurements.filter(like='rel_flux_C', axis=1)
comp_relflux = comp_relflux.reindex(comp_relflux.mean().sort_values().index, axis=1)
plot_col_count = comp_relflux.shape[1]  # 动态获取待绘制列数
x_data = (bjd) % 1  # 提前计算X轴数值,避免循环重复计算

# 创建垂直排列的子图网格
fig, axes = plt.subplots(
    nrows=plot_col_count,
    ncols=1,
    figsize=(12, plot_col_count * 1),  # 单张子图高度1,总高度随列数动态调整
    sharex=True,  # 所有子图共享X轴,自动对齐坐标
    gridspec_kw={"hspace": 0}  # 子图垂直间距设为0,消除冗余空白
)

# 兼容单列场景:单列子图时axes不是可迭代数组,手动转为列表
if plot_col_count == 1:
    axes = [axes]

# 逐列绘制散点图
for ax, col_name in zip(axes, comp_relflux.columns):
    sns.scatterplot(x=x_data, y=comp_relflux[col_name], color="b", marker=".", ax=ax)
    # 优化紧凑度:非最底部子图隐藏X轴刻度标签
    if ax != axes[-1]:
        ax.set_xticklabels([])
        ax.set_xlabel("")
    # 可选:隐藏多余边框,调整y轴刻度大小
    sns.despine(ax=ax, top=True, right=True)
    ax.tick_params(axis="y", labelsize=8)

# 自动调整布局,支持直接保存为单张图片
plt.tight_layout()
plt.savefig("comp_relflux_plot.png", dpi=150, bbox_inches="tight")
plt.show()
关键配置说明
  • sharex=True:强制所有子图共用X轴范围与刻度,彻底解决坐标轴错位问题
  • gridspec_kw={"hspace":0}:将子图垂直间距设置为0,消除图表间冗余空白
  • 绘图时显式传入ax=ax参数,指定绘图目标为当前子图,不会生成新的独立画布
  • 单列场景做了兼容处理,完全适配列数1-30动态变化的需求

内容的提问来源于stack exchange,提问作者msalis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 02:36:15