gpt4 book ai didi

python - 带有方程式和 R2 文本的 Seaborn lmplot

转载 作者:太空狗 更新时间:2023-10-29 20:37:09 28 4
gpt4 key购买 nike

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

最佳答案

它不能用 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)

enter image description here

关于python - 带有方程式和 R2 文本的 Seaborn lmplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25579227/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com