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

如何让Seaborn两个regplot并排展示且Y轴刻度比例保持一致

方案1:创建子图时开启Y轴共享(推荐)

在调用plt.subplots时添加sharey=True参数,两个子图会自动使用完全一致的Y轴刻度范围和比例,天然满足数值对齐的需求。
对应修改后的代码:

# 增加sharey=True参数强制共享Y轴
fig, ax = plt.subplots(1,2, figsize=(16,8), sharey=True)
sb.regplot(x="V", y="Matrix Time", data=df_1, ax=ax[0], scatter_kws={"color": "b"}, line_kws={"color": "red"})
sb.regplot(x="V", y="List Time", data=df_1, ax=ax[1], scatter_kws={"color": "g"}, line_kws={"color": "red"})

# 若需要右侧子图也显示Y轴刻度,可添加以下代码
plt.setp(ax[1].get_yticklabels(), visible=True)

方案2:手动统一设置Y轴范围

如果需要自定义Y轴的上下限而非完全自动匹配,可以先计算两个Y列的全局极值,再给两个子图设置相同的Y轴范围:

fig, ax = plt.subplots(1,2, figsize=(16,8))
sb.regplot(x="V", y="Matrix Time", data=df_1, ax=ax[0], scatter_kws={"color": "b"}, line_kws={"color": "red"})
sb.regplot(x="V", y="List Time", data=df_1, ax=ax[1], scatter_kws={"color": "g"}, line_kws={"color": "red"})

# 计算全局Y轴极值,可根据需求额外加偏移量
y_min = min(df_1["Matrix Time"].min(), df_1["List Time"].min())
y_max = max(df_1["Matrix Time"].max(), df_1["List Time"].max())
# 统一设置两个子图的Y轴范围
ax[0].set_ylim(y_min, y_max)
ax[1].set_ylim(y_min, y_max)

内容的提问来源于stack exchange,提问作者Harsh Rao Dhanyamraju

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 07:36:03