Seaborn:注释线性回归方程 [英] Seaborn: annotate the linear regression equation

查看:646
本文介绍了Seaborn:注释线性回归方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为波士顿数据集安装OLS.我的图如下所示.

I tried fitting an OLS for Boston data set. My graph looks like below.

如何在直线上方或图形中的某处注释线性回归方程式?如何在Python中打印方程式?

How to annotate the linear regression equation just above the line or somewhere in the graph? How do I print the equation in Python?

我是这个领域的新手.到目前为止,探索python.如果有人可以帮助我,那将加快我的学习速度.

I am fairly new to this area. Exploring python as of now. If somebody can help me, it would speed up my learning curve.

非常感谢!

我也尝试过这个.

我的问题是-如何以方程式格式在图形中注释以上内容?

My problem is - how to annotate the above in the graph in equation format?

推荐答案

您可以使用线性拟合系数来制作图例,如下例所示:

You can use coefficients of linear fit to make a legend like in this example:

import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats

tips = sns.load_dataset("tips")

# get coeffs of linear fit
slope, intercept, r_value, p_value, std_err = stats.linregress(tips['total_bill'],tips['tip'])

# use line_kws to set line label for legend
ax = sns.regplot(x="total_bill", y="tip", data=tips, color='b', 
 line_kws={'label':"y={0:.1f}x+{1:.1f}".format(slope,intercept)})

# plot legend
ax.legend()

plt.show()

如果您使用更复杂的拟合函数,则可以使用乳胶通知: https://matplotlib.org/users /usetex.html

If you use more complex fitting function you can use latex notification: https://matplotlib.org/users/usetex.html

这篇关于Seaborn:注释线性回归方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆