You need to enable JavaScript to run this app. Python中如何将seaborn绘制的所有直方图子图y轴范围统一设为0-50000
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何将seaborn绘制的所有直方图子图y轴范围统一设为0-50000

你可以直接在遍历子图设置标签的代码块中,补充ylim参数指定y轴范围即可,调整后的完整代码如下:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# 假设df为你已加载完成的数据集
fig, axs = plt.subplots(ncols = 2, nrows = 3, figsize = (18, 18))
sns.distplot(df["EXT"], bins = 40, kde = False, 
             ax = axs[0, 0], color = sns.color_palette()[0]).set_title("Extroversion")
sns.distplot(df["EST"], bins = 40, kde = False, 
             ax = axs[0, 1], color = sns.color_palette()[1]).set_title("Neuroticism")
sns.distplot(df["AGR"], bins = 40, kde = False, 
             ax = axs[1, 0], color = sns.color_palette()[2]).set_title("Agreeableness")
sns.distplot(df["CSN"], bins = 40, kde = False, 
             ax = axs[1, 1], color = sns.color_palette()[3]).set_title("Conscientiousness")
sns.distplot(df["OPN"], bins = 40, kde = False, 
             ax = axs[2, 0], color = sns.color_palette()[4]).set_title("Openness")

fig.delaxes(axs[2, 1])
for ax in axs.flat:
    # 过滤已删除的子图避免报错
    if ax in fig.axes:
        ax.set(xlabel = None, ylabel = "Count", ylim=(0, 50000))
plt.show()

你也可以选择在创建子图时添加sharey=True参数,让所有子图自动共享y轴刻度范围,无需手动统一:

# 仅需修改创建子图的第一行代码即可
fig, axs = plt.subplots(ncols = 2, nrows = 3, figsize = (18, 18), sharey=True)

效果示例:
示例图

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 06:06:03