【问题标题】:show metrics like kurtosis, skewness on distribution plot using seaborn in python在 python 中使用 seaborn 在分布图上显示峰度、偏度等指标
【发布时间】:2019-02-20 05:35:47
【问题描述】:

我有以下数据:

coll_prop_tenure    coll_prop_12m   coll_prop_6m    coll_prop_3m
0.04                0.04            0.06            0.08
0                   0               0               0
0                   0               0               0
0.06                0.06            0.1             0
0.38                0.38            0.25            0
0.61                0.61            0.66            0.61
0.01                0.01            0.02            0.02
0.1                 0.1             0.12            0.16
0.04                0.04            0.04            0.09
0.22                0.22            0.22            0.22
0.72                0.72            0.73            0.72
0.39                0.39            0.45            0.64

我正在使用 seaborn 的 distplot 来绘制如下分布:

######################## density plot #########################################
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.distplot( data[cols_viz[0]] , color="skyblue", ax=axes[0, 0])
print("Skewness: %f" % data[cols_viz[0]].skew())
print("Kurtosis: %f" % data[cols_viz[0]].kurt())

sns.distplot( data[cols_viz[1]] , color="olive", ax=axes[0, 1])
print("Skewness: %f" % data[cols_viz[1]].skew())
print("Kurtosis: %f" % data[cols_viz[1]].kurt())
sns.distplot( data[cols_viz[2]] , color="gold", ax=axes[1, 0])
sns.distplot( data[cols_viz[3]] , color="teal", ax=axes[1, 1])
plt.show()

这确实给了我值,但我希望它们出现在相应的图中。

我该怎么做?有人可以帮我解决这个问题吗!

【问题讨论】:

    标签: python seaborn


    【解决方案1】:

    您可以使用ax.text() 将文本直接打印到绘图上。 我将您的 DF 作为代码导入并进行了一些调整:

    • 使用for i, ax in enumerate(axes) 可以让您循环遍历轴中的每个轴并获得与列号对应的数字,但您必须添加.reshape(-1) 以折叠ndarray 中的一个级别才能获得@987654326 @ 范围为 1-4。
    • 使用.iloc[:,i] 可以让您为每个子图引用正确的列
    • 使用'transform=ax.transAxes 作为ax.text() 命令的参数将允许您缩放轴,以便文本框的位置始终相同;我用 x=0.97 和 y=0.91 大致把它放在右上角

    这里是 DF:

    data = pd.DataFrame({'coll_prop_tenure': {0: 0.04, 1: 0.0, 2: 0.0, 3: 0.06, 4: 0.38, 5: 0.61, 6: 0.01, 7: 0.1, 8: 0.04, 9: 0.22, 10: 0.72, 11: 0.39}, \
                        'coll_prop_12m': {0: 0.04, 1: 0.0, 2: 0.0, 3: 0.06, 4: 0.38, 5: 0.61, 6: 0.01, 7: 0.1, 8: 0.04, 9: 0.22, 10: 0.72, 11: 0.39}, \
                        'coll_prop_6m': {0: 0.06, 1: 0.0, 2: 0.0, 3: 0.1, 4: 0.25, 5: 0.66, 6: 0.02, 7: 0.12, 8: 0.04, 9: 0.22, 10: 0.73, 11: 0.45}, \
                        'coll_prop_3m': {0: 0.08, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.61, 6: 0.02, 7: 0.16, 8: 0.09, 9: 0.22, 10: 0.72, 11: 0.64}})
    

    这里是代码:

    f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
    sns.distplot(data.iloc[:,0], color="skyblue", ax=axes[0,0])
    sns.distplot(data.iloc[:,1], color="olive", ax=axes[0,1])
    sns.distplot(data.iloc[:,2], color="gold", ax=axes[1,0])
    sns.distplot(data.iloc[:,3], color="teal", ax=axes[1,1])
    for i, ax in enumerate(axes.reshape(-1)):
        ax.text(x=0.97, y=0.97, transform=ax.transAxes, s="Skewness: %f" % data.iloc[:,i].skew(),\
            fontweight='demibold', fontsize=10, verticalalignment='top', horizontalalignment='right',\
            backgroundcolor='white', color='xkcd:poo brown')
        ax.text(x=0.97, y=0.91, transform=ax.transAxes, s="Kurtosis: %f" % data.iloc[:,i].kurt(),\
            fontweight='demibold', fontsize=10, verticalalignment='top', horizontalalignment='right',\
            backgroundcolor='white', color='xkcd:dried blood')
    plt.tight_layout()
    

    【讨论】:

    • @MMelmicki 你能解释一下代码的第二部分吗?我正在尝试创建类似的结果,但我不断得到值小于 1 的 y 轴,我不确定我是否理解它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2011-06-27
    • 2017-02-16
    • 2020-04-16
    • 2016-08-22
    • 2012-06-04
    • 2019-09-11
    相关资源
    最近更新 更多