5

有没有办法用 显示对相关值seaborn.pairplot(),如下例所示(用ggpairs()in创建R)?我可以使用附加的代码制作图,但不能添加相关性。谢谢

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

在此处输入图像描述

4

1 回答 1

8

如果您使用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)

在此处输入图像描述

于 2020-08-16T05:35:03.790 回答