Python/Matplotlib:在给定截距和斜率的情况下向图中添加回归线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43152529/
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-19 22:35:28  来源:igfitidea点击:

Python/Matplotlib: adding regression line to a plot given its intercept and slope

pythonmatplotlibplotlinear-regression

提问by Jonathan

Using the following small dataset:

使用以下小数据集:

bill = [34,108,64,88,99,51]
tip =  [5,17,11,8,14,5]  

I calculated a best-fit regression line (by hand).

我计算了一个最合适的回归线(手工)。

yi = 0.1462*x - 0.8188 #yi = slope(x) + intercept

I've plotted my original data using Matplotlib like this:

我已经使用 Matplotlib 绘制了我的原始数据,如下所示:

scatter(bill,tip, color="black")
plt.xlim(20,120) #set ranges
plt.ylim(4,18)

#plot centroid point (mean of each variable (74,10))
line1 = plt.plot([74, 74],[0,10], ':', c="red")
line2 = plt.plot([0,74],[10,10],':', c="red")

plt.scatter(74,10, c="red")

#annotate the centroid point
plt.annotate('centroid (74,10)', xy=(74.1,10), xytext=(81,9),
        arrowprops=dict(facecolor="black", shrink=0.01),
        )

#label axes
xlabel("Bill amount ($)")
ylabel("Tip amount ($)")

#display plot
plt.show()

I am unsure how to get the regression line onto the plot itself. I'm aware that there are plenty of builtin stuff for quickly fitting and displaying best fit lines, but I did this as practice. I know I can start the line at points '0,0.8188' (the intercept), but I don't know how to use the slope value to complete the line (set the lines end points).

我不确定如何将回归线放到图本身上。我知道有很多内置的东西可以快速拟合和显示最佳拟合线,但我这样做是为了练习。我知道我可以在点“0,0.8188”(截距)处开始线,但我不知道如何使用斜率值来完成线(设置线端点)。

Given that for each increase on the x axis, the slope should increase by '0.1462'; for the line coordinates I tried (0,0.8188) for the starting point, and (100,14.62) for the end point. But this line does not pass through my centroid point. It just misses it.

鉴于 x 轴上的每次增加,斜率应增加“0.1462”;对于我尝试的线坐标(0,0.8188)作为起点,(100,14.62)作为终点。但是这条线没有通过我的质心点。它只是想念它。

Cheers, Jon

干杯,乔恩

采纳答案by ImportanceOfBeingErnest

The reasoning in the question partially correct. Having a function f(x) = a*x +b, you may take as first point the interception with the y axis (x=0) as (0, b)(or (0,-0.8188)in this case).
Any other point on that line is given by (x, f(x)), or (x, a*x+b). So looking at the point at x=100 would give you (100, f(100)), plugging in: (100, 0.1462*100-0.8188)= (100,13.8012). In the case you describe in the question you just forgot to take the binto account.

问题中的推理部分正确。有一个函数f(x) = a*x +b,您可以将与 y 轴 (x=0) 的截距作为第一点(0, b)(或(0,-0.8188)在这种情况下)。
该线上的任何其他点由(x, f(x)), 或给出(x, a*x+b)。因此,查看 x=100 处的点会给您(100, f(100)),插入:(100, 0.1462*100-0.8188)= (100,13.8012)。在您在问题中描述的情况下,您只是忘记考虑了b

The following shows how to use that function to plot the line in matplotlib:

下面显示了如何使用该函数在 matplotlib 中绘制线条:

import matplotlib.pyplot as plt
import numpy as np

bill = [34,108,64,88,99,51]
tip =  [5,17,11,8,14,5]  
plt.scatter(bill, tip)

#fit function
f = lambda x: 0.1462*x - 0.8188
# x values of line to plot
x = np.array([0,100])
# plot fit
plt.plot(x,f(x),lw=2.5, c="k",label="fit line between 0 and 100")

#better take min and max of x values
x = np.array([min(bill),max(bill)])
plt.plot(x,f(x), c="orange", label="fit line between min and max")

plt.legend()
plt.show()

enter image description here

在此处输入图片说明

Of course the fitting can also be done automatically. You can obtain the slope and intercept from a call to numpy.polyfit:

当然,拟合也可以自动完成。您可以从调用中获取斜率和截距numpy.polyfit

#fit function
a, b = np.polyfit(np.array(bill), np.array(tip), deg=1)
f = lambda x: a*x + b

The rest in the plot would stay the same.

情节中的其余部分将保持不变。

回答by f5r5e5d

define function fit, get endpoints of data, put in tuples to plot()

定义函数拟合,获取数据端点,将元组放入 plot()

def fit(x):
    return 0.1462*x - 0.8188 #yi = slope(x) - intercept
xfit, yfit = (min(bill), max(bill)), (fit(min(bill)), fit(max(bill)))    
plt.plot(xfit, yfit,'b')

回答by Enzo Calogero

quick note: I think that the formula for the regression should be

快速说明:我认为回归的公式应该是

return 0.1462*x + 0.8188 #yi = slope(x) + intercept

I mean + instead of -.

我的意思是+而不是-。