【问题标题】:What is the unit of the y-axis when using distplot to plot a histogram?使用 distplot 绘制直方图时,y 轴的单位是什么?
【发布时间】:2015-04-22 05:12:09
【问题描述】:

使用 distplot 绘制直方图时,y 轴的单位是什么?我用正常拟合绘制了不同的直方图,我发现在一种情况下,它的范围为 0 到 0.9,而在另一种情况下,范围为 0 到 4.5。

【问题讨论】:

    标签: python seaborn


    【解决方案1】:

    来自help(sns.distplot)

    norm_hist: 布尔型,可选 如果为 True,则直方图高度显示密度而不是计数。 如果绘制了 KDE 或拟合密度,则暗示这一点。

    密度被缩放,使得曲线下的面积为 1,因此没有单个 bin 永远高于 1(整个数据集)。但kde 默认为True 并覆盖norm_hist,因此norm_hist 仅在您将kde 显式设置为False 时才会更改y 单位:

    import matplotlib.pyplot as plt
    import numpy as np
    import seaborn as sns
    
    fig, axs = plt.subplots(figsize=(6,6), ncols=2, nrows=2)
    data = np.random.randint(0,20,40)
    
    for row in (0,1):
        for col in (0,1):
            sns.distplot(data, kde=row, norm_hist=col, ax=axs[row, col])
    
    axs[0,0].set_ylabel('NO kernel density')
    axs[1,0].set_ylabel('KDE on')
    axs[1,0].set_xlabel('norm_hist=False')
    axs[1,1].set_xlabel('norm_hist=True')
    

    【讨论】:

    • 这很有帮助,但我认为最好明确说明密度被缩放以使曲线下面积为 1。
    • 没问题,哈利。如果它回答了您的问题,请将其勾选为已完成。
    猜你喜欢
    • 2019-01-11
    • 2013-12-22
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2015-04-04
    • 2013-01-12
    • 2021-10-07
    相关资源
    最近更新 更多