Python 带有方程和 R2 文本的 Seaborn 嵌入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25579227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:24:09  来源:igfitidea点击:

Seaborn implot with equation and R2 text

pythonmatplotlibseaborn

提问by user3287545

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?

在我的常规数据分析工作中,自从 seaborn 包可用后,我已改用 100% python。非常感谢这个精彩的包裹。但是,我想念的一个 excel 图表功能是在使用 lmplot() 函数时显示 polyfit 方程和/或 R2 值。有谁知道一种简单的方法来添加它?

回答by mwaskom

It can't be done automatically with lmplotbecause it's undefined what that value should correspond to when there are multiple regression fits (i.e. using a hue, rowor colvariable.

它不能自动完成,lmplot因为当存在多重回归拟合时(即使用hue,rowcol变量。

But this is part of the similar jointplotfunction. By default it shows the correlation coefficient and p value:

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

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:

但是你可以传递任何函数。如果你想要 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)

enter image description here

在此处输入图片说明