Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn Correlation Coefficient on PairGrid

Tags:

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

like image 302
wblack Avatar asked Jun 19 '15 16:06

wblack


People also ask

Does Pairplot show correlation?

Correlation. The seaborn library allows to draw a correlation matrix through the pairplot() function. The parameters to create the example graphs are: data : dataframe.

What is Seaborn PairGrid?

Advertisements. PairGrid allows us to draw a grid of subplots using the same plot type to visualize data. Unlike FacetGrid, it uses different pair of variable for each subplot. It forms a matrix of sub-plots.

What is correlation matrix Seaborn?

Seaborn allows you to make a correlogram or correlation matrix really easily. Correlogram is awesome for exploratory analysis: it makes you quickly observe the relationship between every variable of your matrix. It is easy to do it with seaborn: just call the pairplot() function!

How do you make a correlation matrix in python?

Method 1: Creating a correlation matrix using Numpy libraryNumpy library make use of corrcoef() function that returns a matrix of 2×2. The matrix consists of correlations of x with x (0,0), x with y (0,1), y with x (1,0) and y with y (1,1).


1 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

like image 134
mwaskom Avatar answered Sep 28 '22 12:09

mwaskom