Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn implot 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?

like image 556
user3287545 Avatar asked Aug 30 '14 05:08

user3287545


People also ask

What is the difference between Regplot and Lmplot?

The two functions that can be used to visualize a linear fit are regplot() and lmplot() . These functions draw similar plots, but :func:regplot` is an axes-level function, and lmplot() is a figure-level function.

What is the use of Seaborn Regplot () method?

regplot() : This method is used to plot data and a linear regression model fit. There are a number of mutually exclusive options for estimating the regression model.

Which parameter of the Seaborn method Lmplot () allows for wrapping column variables into multiple rows?

col_wrap : (optional) This parameter is of int type, “Wrap” the column variable at this width, so that the column facets span multiple rows.

How do you display the linear correlation of two data sets using Seaborn?

Functions to Draw Linear Regression Models There are two main functions in Seaborn to visualize a linear relationship determined through regression. These functions are regplot() and lmplot().


2 Answers

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

like image 127
mwaskom Avatar answered Oct 12 '22 15:10

mwaskom


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

like image 32
Marcos Avatar answered Oct 12 '22 15:10

Marcos