带有方程和 R2 文本的 Seaborn 嵌入 [英] Seaborn implot with equation and R2 text
问题描述
在我的常规数据分析工作中,自从seaborn软件包可用以来,我已切换为使用100%python.非常感谢这个精美的包装.但是,我错过的一个excel图表功能是在使用lmplot()函数时显示polyfit方程和/或R2值.有谁知道添加它的简单方法吗?
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?
推荐答案
使用 lmplot
不能自动完成,因为尚不确定在存在多个回归拟合时该值应对应的含义(即使用 hue
、row
或 col
变量.
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.
但这是类似的 jointplot
函数的一部分.默认情况下,它显示相关系数和p值:
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")
但是你可以传递任何函数.如果您想要R ^ 2,则可以执行以下操作:
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)
这篇关于带有方程和 R2 文本的 Seaborn 嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!