2017-08-27 198 views
3

我试过拟合波士顿数据集的OLS。我的图形如下所示。Seaborn:注释线性回归方程

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

我对这方面比较陌生。从现在开始探索python。如果有人能帮助我,这会加快我的学习曲线。

非常感谢!

OLS fit

我想这一点。

enter image description here

我的问题是 - 如何注释以上在等式格式图形?

回答

5

您可以在这个例子中使用线性拟合系数,使一个传说,如:

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() 

enter image description here

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

+0

谢谢你非常。通过添加等式作为标题,我达到了这样的效果。 –

+0

你怎么知道regplot行反映了scipy.stats回归参数?令人惊讶的是,seaborn并没有提供它计算的参数来制作他们的地块...... – joaquin