Python pairplot()中的相关值

Python pairplot()中的相关值,python,r,matplotlib,seaborn,ggpairs,Python,R,Matplotlib,Seaborn,Ggpairs,是否有一种方法可以使用seaborn.pairplot()显示配对相关性值,如下面的示例所示(使用R中的ggpairs()创建)?我可以使用附带的代码绘制图,但不能添加相关性。谢谢 import numpy as np import seaborn as sns import matplotlib.pyplot as plt iris = sns.load_dataset('iris') g = sns.pairplot(iris, kind='scatter', diag_kind='kde

是否有一种方法可以使用
seaborn.pairplot()
显示配对相关性值,如下面的示例所示(使用
R
中的
ggpairs()
创建)?我可以使用附带的代码绘制图,但不能添加相关性。谢谢

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset('iris')
g = sns.pairplot(iris, kind='scatter', diag_kind='kde')
# remove upper triangle plots
for i, j in zip(*np.triu_indices_from(g.axes, 1)):
    g.axes[i, j].set_visible(False)
plt.show()

如果使用
PairGrid
而不是
pairplot
,则可以传递一个自定义函数,该函数将计算相关系数并将其显示在图形上:

from scipy.stats import pearsonr
def reg_coef(x,y,label=None,color=None,**kwargs):
    ax = plt.gca()
    r,p = pearsonr(x,y)
    ax.annotate('r = {:.2f}'.format(r), xy=(0.5,0.5), xycoords='axes fraction', ha='center')
    ax.set_axis_off()

iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map_diag(sns.distplot)
g.map_lower(sns.regplot)
g.map_upper(reg_coef)

这是否回答了您的问题?