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?
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.
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.
col_wrap : (optional) This parameter is of int type, “Wrap” the column variable at this width, so that the column facets span multiple rows.
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().
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)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With