简体   繁体   English

PairGrid上的Seaborn相关系数

[英]Seaborn Correlation Coefficient on PairGrid

Is there a matplotlib or seaborn plot I could use with g.map_lower or g.map_upper to get the correlation coefficient displayed for each bivariate plot like shown below? 是否有matplotlib或seaborn图我可以使用g.map_lower或g.map_upper来获取每个双变量图的相关系数,如下所示? plt.text was manually mapped to get the below example which is a tedious process. 手动映射plt.text以获取下面的示例,这是一个繁琐的过程。

在此输入图像描述

You can pass any function to the map_* methods as long as it follows a few rules: 1) it should plot onto the "current" axes, 2) it should take two vectors as positional arguments, and 3) it should accept a color keyword argument (optionally using it, if you want to be compatible with the hue option). 您可以将任何函数传递给map_*方法,只要它遵循一些规则:1)它应该绘制到“当前”轴上,2)它应该采用两个向量作为位置参数,3)它应该接受color关键字参数(如果要与hue选项兼容,可选择使用它)。

So in your case you just need to define a little corrfunc function and then map it across the axes you want to have annotated: 因此,在您的情况下,您只需要定义一个小的corrfunc函数,然后将其映射到您想要注释的轴上:

import numpy as np
from scipy import stats
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="white")

mean = np.zeros(3)
cov = np.random.uniform(.2, .4, (3, 3))
cov += cov.T
cov[np.diag_indices(3)] = 1
data = np.random.multivariate_normal(mean, cov, 100)
df = pd.DataFrame(data, columns=["X", "Y", "Z"])

def corrfunc(x, y, **kws):
    r, _ = stats.pearsonr(x, y)
    ax = plt.gca()
    ax.annotate("r = {:.2f}".format(r),
                xy=(.1, .9), xycoords=ax.transAxes)

g = sns.PairGrid(df, palette=["red"])
g.map_upper(plt.scatter, s=10)
g.map_diag(sns.distplot, kde=False)
g.map_lower(sns.kdeplot, cmap="Blues_d")
g.map_lower(corrfunc)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM