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 fea
Solution 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
defr2(x, y):
return stats.pearsonr(x, y)[0] ** 2
sns.jointplot(x, y, kind="reg", stat_func=r2)
Solution 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()
Post a Comment for "Seaborn Implot With Equation And R2 Text"