【问题标题】:Seaborn Correlation Coefficient on PairGridPairGrid 上的 Seaborn 相关系数
【发布时间】:2015-09-05 16:45:51
【问题描述】:

是否有一个 matplotlib 或 seaborn 图我可以与 g.map_lower 或 g.map_upper 一起使用来获得为每个二元图显示的相关系数,如下所示? plt.text 被手动映射到下面的例子,这是一个繁琐的过程。

【问题讨论】:

标签: python seaborn


【解决方案1】:

您可以将任何函数传递给 map_* 方法,只要它遵循一些规则:1) 它应该绘制在“当前”轴上,2) 它应该采用两个向量作为位置参数,以及 3) 它应该接受 color 关键字参数(如果您想与 hue 选项兼容,可以选择使用它)。

因此,在您的情况下,您只需要定义一个小 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)

【讨论】:

  • 这个等高线图代表什么?
  • 完美。由于 distplot 已弃用,您可能需要将 distplot 更新为 histplot。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多