matplotlib 中的错误原点和回归线

False origin and regression line in matplotlib

import matplotlib.pyplot as plt

Independant_variable = [5.08, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]

Dependant_variable = [74.47, 71.61, 67.66, 68.07, 67.32, 67.46, 67.17, 66.33, 65.63, 65.51, 65.56, 65.15, 65.79, 65.36, 70.94, 65.53, 66.38, 66.71, 66.44, 66.07, 68.48, 73.70, 70.91, 66.17, 71.10, 71.18, 75.57, 79.13]

plt.plot(Independant_variable, Dependant_variable, ".")
plt.grid(True)

plt.xlabel("Independant variable", fontsize=13)
plt.ylabel("Dependant variable", fontsize=13)

如何向 x 轴和 y 轴添加错误的原点?

此外,我如何添加回归线,以便找到给定自变量值的因变量的最小值?

import numpy as np
import matplotlib.pyplot as plt

Independant_variable = [5.08, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
Dependant_variable = [74.47, 71.61, 67.66, 68.07, 67.32, 67.46, 67.17, 66.33, 65.63, 65.51, 65.56, 65.15, 65.79, 65.36, 70.94, 65.53,
                      66.38, 66.71, 66.44, 66.07, 68.48, 73.70, 70.91, 66.17, 71.10, 71.18, 75.57, 79.13]

#linear regression is calculated here
N = len(Independant_variable)
regress = np.zeros(N)
sumy = 0.0
sumxy = 0.0
sumx = 0
sumx2 = 0

for i in range(0, N):
    sumy += Dependant_variable[i]
    sumxy += Dependant_variable[i] * Independant_variable[i]
    sumx += Independant_variable[i]
    sumx2 += Independant_variable[i] * Independant_variable[i]
c = sumx2 * N - sumx * sumx
b = (sumxy * N - sumx * sumy) / c
a = (sumy - sumx * b) / N
for i in range(0, N):
    regress[i] = a + b * Independant_variable[i]
#linear regression is calculated here

plt.plot(Independant_variable, Dependant_variable, ".")
plt.plot(Independant_variable, regress)
plt.grid(True)
plt.axis([0,35,65,80])
plt.xlabel("Independant variable", fontsize=13)
plt.ylabel("Dependant variable", fontsize=13)

plt.show()

我手动计算了回归线。我想您会对它的计算方式感兴趣。如果我对你的理解正确的话,费用轴由这条线调节:plot.axis([0,35,65,80]).

如果我已经回答了你的问题,请打一个绿色复选标记并单击三角形。也感谢我花费的时间)