Seaborn Correlation Coefficient on PairGrid

Question:

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? plt.text was manually mapped to get the below example which is a tedious process.

enter image description here

Asked By: wblack

||

Answers:

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

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:

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)

enter image description here

Answered By: mwaskom

have a look here as well if you want to add correlation for each hue level separately.
https://stackoverflow.com/a/75925982/19060883

Answered By: user19060883
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.