2017-08-27 199 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