'Seaborn lmplot with equation and R2 text

In my regular data analysis work, I have switched to use 100% python since the seaborn package becomes available. Big thanks to this wonderful package. However, One excel-chart feature I miss is to display the polyfit equation and/or R2 value when use the lmplot() function. Does anyone know an easy way to add that?



Solution 1:[1]

It can't be done automatically with lmplot because it's undefined what that value should correspond to when there are multiple regression fits (i.e. using a hue, row or col variable.

But this is part of the similar jointplot function. By default it shows the correlation coefficient and p value:

import seaborn as sns
import numpy as np

x, y = np.random.randn(2, 40)
sns.jointplot(x, y, kind="reg")

But you can pass any function. If you want R^2, you could do:

from scipy import stats
def r2(x, y):
    return stats.pearsonr(x, y)[0] ** 2
sns.jointplot(x, y, kind="reg", stat_func=r2)

enter image description here

Solution 2:[2]

This now can be done using FacetGrid methods .map() or .map_dataframe():

import seaborn as sns
import scipy as sp

tips = sns.load_dataset('tips')
g = sns.lmplot(x='total_bill', y='tip', data=tips, row='sex',
               col='time', height=3, aspect=1)

def annotate(data, **kws):
    r, p = sp.stats.pearsonr(data['total_bill'], data['tip'])
    ax = plt.gca()
    ax.text(.05, .8, 'r={:.2f}, p={:.2g}'.format(r, p),
            transform=ax.transAxes)
    
g.map_dataframe(annotate)
plt.show()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mwaskom
Solution 2 Oren