繁体   English   中英

在python中使用seaborn在分布图上显示峰度,偏度等指标

[英]show metrics like kurtosis, skewness on distribution plot using seaborn in python

我有以下数据:

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()

在此处输入图片说明

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

我怎样才能做到这一点? 有人可以帮我这个忙!

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

  • 通过使用for i, ax in enumerate(axes)将使您循环for i, ax in enumerate(axes)每个ax,并获得一个与列号相对应的数字,但是您必须添加.reshape(-1)来折叠ndarray中的某个级别为了让i的范围从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()

plotOutputResult

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM