在python中使用seaborn在pairplot中显示相关值 [英] Show correlation values in pairplot using seaborn in python

查看:74
本文介绍了在python中使用seaborn在pairplot中显示相关值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

  prop_tenure prop_12m prop_6m0.00 0.00 0.000.00 0.00 0.000.06 0.06 0.100.38 0.38 0.250.61 0.61 0.660.01 0.01 0.020.10 0.10 0.120.04 0.04 0.040.22 0.22 0.22 

我正在做如下一对图:

  sns.pairplot(数据)plt.show() 

但是,我想显示变量之间的相关系数,并尽可能显示每个变量的偏度和峰度.我不确定如何在seaborn中做到这一点.有人可以帮我吗?

解决方案

据我所知,没有开箱即用的功能可以执行此操作,您必须

I have the below data:

prop_tenure  prop_12m  prop_6m  
0.00         0.00      0.00   
0.00         0.00      0.00   
0.06         0.06      0.10   
0.38         0.38      0.25   
0.61         0.61      0.66   
0.01         0.01      0.02   
0.10         0.10      0.12   
0.04         0.04      0.04   
0.22         0.22      0.22 

and I am doing a pairplot as below:

sns.pairplot(data)
plt.show()

However I would like to display the correlation coefficient among the variables and if possible the skewness and kurtosis of each variable. I am not sure how to do that in seaborn. Can someone please help me with this?

解决方案

As far as I'm aware, there is no out of the box function to do this, you'll have to create your own:

from scipy.stats import pearsonr
import matplotlib.pyplot as plt 

def corrfunc(x, y, ax=None, **kws):
    """Plot the correlation coefficient in the top left hand corner of a plot."""
    r, _ = pearsonr(x, y)
    ax = ax or plt.gca()
    ax.annotate(f'ρ = {r:.2f}', xy=(.1, .9), xycoords=ax.transAxes)

Example using your input:

import seaborn as sns; sns.set(style='white')
import pandas as pd

data = {'prop_tenure': [0.0, 0.0, 0.06, 0.38, 0.61, 0.01, 0.10, 0.04, 0.22], 
        'prop_12m':    [0.0, 0.0, 0.06, 0.38, 0.61, 0.01, 0.10, 0.04, 0.22], 
        'prop_6m':     [0.0, 0.0, 0.10, 0.25, 0.66, 0.02, 0.12, 0.04, 0.22]}

df = pd.DataFrame(data)

g = sns.pairplot(df)
g.map_lower(corrfunc)
plt.show()

这篇关于在python中使用seaborn在pairplot中显示相关值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆