【问题标题】:Correlation values in pairplot()pairplot() 中的相关值
【发布时间】:2020-12-04 14:02:47
【问题描述】:

有没有办法用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()

【问题讨论】:

标签: python r matplotlib seaborn ggpairs


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 2018-05-05
    • 1970-01-01
    • 2018-12-16
    • 2021-02-04
    • 1970-01-01
    相关资源
    最近更新 更多