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

标签 python seaborn

我有以下数据:

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

enter image description here

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

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

最佳答案

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

  • 使用for i, ax in enumerate(axes)可以让你循环遍历axes中的每个轴,并获得与列号相对应的数字,但你必须添加 .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

关于python - 在Python中使用seaborn在分布图上显示峰度、偏度等指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50940283/

相关文章:

python - 使用参数 'LIKE' 搜索日期

python - Pandas 中的多个直方图

python - tf.keras.layers.pop() 不起作用,但 tf.keras._layers.pop() 起作用

python - Seaborn.despine() 消除将 y 轴移动到图右侧的效果

python - 统计每月 Pandas 的分类数据

Python 2.5.4 - ImportError : No module named etree. ElementTree

python - 神经网络中 DataFrame 的批量输入

python - seaborn clustermap 不会重新排列注释数据

python - 如何使用 matplotlib/seaborn 和 pandas 数据框创建具有共享 x 轴的倒置条形图

python - 从 seaborn 中保存情节