Show correlation values in pairplot using seaborn in python

Shu*_*Das 6 python correlation 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 
Run Code Online (Sandbox Code Playgroud)

and I am doing a pairplot as below:

sns.pairplot(data)
plt.show()
Run Code Online (Sandbox Code Playgroud)

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?

uke*_*emi 10

据我所知,没有开箱即用的功能可以执行此操作,您必须创建自己的函数:

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()
    # Unicode for lowercase rho (?)
    rho = '\u03C1'
    ax.annotate(f'{rho} = {r:.2f}', xy=(.1, .9), xycoords=ax.transAxes)
Run Code Online (Sandbox Code Playgroud)

使用您的输入的示例:

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.1, 0.04, 0.22], 
        'prop_12m':    [0.0, 0.0, 0.06, 0.38, 0.61, 0.01, 0.1, 0.04, 0.22], 
        'prop_6m':     [0.0, 0.0, 0.1, 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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 非常感谢@ukemi ..这很漂亮。 (2认同)
  • 非常感谢!这是非常有用的。 (2认同)