【问题标题】:Seaborn histplot uses weird y axis limits?Seaborn histplot 使用奇怪的 y 轴限制?
【发布时间】:2021-08-29 18:34:02
【问题描述】:

我正在遍历我的 df 的所有列以绘制它们的密度,以查看我是否需要以及如何转换/规范化我的数据。我正在使用 Seaborn 和这段代码:

fig, axes = plt.subplots(nrows=n_rows, ncols=n_cols, figsize=(16,40))
fig.tight_layout() #othwerwise the plots overlapped each other and I couldn't see the column names

for i, column in enumerate(df.columns):
    sns.histplot(df[column],ax=axes[i//n_cols,i%n_cols], kde=True, legend=True, fmt='g')

这会产生一个基本正常的图形,但是在某些情况下 y 轴的缩放比例太大了:

城市 3 和 4 还不错,但是,城市 4 的最高 Count 大约是 200,但绘图的比例为 y 直到 10 000,这使得数据难以解释。 x 轴也超出了应有的位置,因为最高成本约为 1000000,但情节一直持续到 25000000。当我单独绘制 City 4 并强制 ylim 为 200 和 xlim 为 1000000 我得到一个更容易理解的情节:

为什么 y 轴(实际上也是 x 轴)缩放得如此奇怪,我怎样才能更改我的代码以缩小它,以免ylim 比实际显示的数据高很多?

谢谢!

【问题讨论】:

    标签: python plot seaborn


    【解决方案1】:

    shared_yaxis 设置为False

    这将使子图绘制在相应数据的各自最大值处。

    例子:

    fig, axes = plt.subplots(nrows=n_rows, ncols=n_cols, figsize=(16,40), sharey=False)
    

    【讨论】:

    • 我将代码更改为fig, axes = plt.subplots(nrows=n_rows, ncols=n_cols, figsize=(16,40), sharey=False, sharex=False),这根本没有任何影响......我在这里错过了什么?
    • 为查询提供样本数据将有助于重现问题。届时解决方案将更容易提供。
    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 2021-05-10
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多