19

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?

4

2 回答 2

28

它不能自动完成,lmplot因为当有多个回归拟合(即使用hue,rowcol变量)时,该值应该对应于什么是未定义的。

但这是类似jointplot功能的一部分。默认情况下,它显示相关系数和 p 值:

import seaborn as sns
import numpy as np

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

但是你可以传递任何函数。如果你想要 R^2,你可以这样做:

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

在此处输入图像描述

于 2014-08-30T15:22:05.153 回答
12

现在可以使用FacetGrid方法 .map() 或 .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()

在此处输入图像描述

于 2021-02-23T00:04:59.610 回答