通过python确定一条与穿过某一点的另一条直线垂直的直线

通过python确定一条与穿过某一点的另一条直线垂直的直线,python,matplotlib,Python,Matplotlib,第1步。我对一组点进行线性回归,得到一条直线。 第二步。我通过平均这一组点得到一个中心点 第3步。我想找到一条与第1步中的回归线垂直的线,并穿过第2步中提到的中心点 我的想法:假设回归线y1=k1*x1+b1,垂直线y2=k2*x2+b2,那么,k2=-1/k1 问题:从图表上看,这两条直线似乎不垂直。为什么?发生了什么事 我的python代码:` import numpy as np import scipy.linalg import matplotlib.pyplot as plt fro

第1步。我对一组点进行线性回归,得到一条直线。 第二步。我通过平均这一组点得到一个中心点 第3步。我想找到一条与第1步中的回归线垂直的线,并穿过第2步中提到的中心点

我的想法:假设回归线y1=k1*x1+b1,垂直线y2=k2*x2+b2,那么,k2=-1/k1

问题:从图表上看,这两条直线似乎不垂直。为什么?发生了什么事

我的python代码:`

import numpy as np
import scipy.linalg
import matplotlib.pyplot as plt
from openpyxl import load_workbook


x = [140.0, 139.0, 145.0, 151.0, 156.0, 160.0, 162.0, 162.0, 156.0, 139.0, 129.0, 124.0, 125.0, 128.0, 127.0, 126.0, 126.0, 132.0, 139.0, 145.0, 150.0, 156.0, 155.0, 148.0, 135.0, 129.0, 125.0, 127.0, 128.0, 126.0, 123.0, 124.0, 130.0, 135.0, 142.0, 151.0, 158.0, 155.0, 148.0, 140.0, 126.0, 124.0, 127.0, 127.0, 126.0, 126.0, 126.0, 131.0, 138.0, 141.0, 159.0, 167.0, 162.0, 155.0, 147.0, 141.0, 138.0, 140.0, 140.0, 140.0, 138.0, 143.0, 150.0, 155.0, 163.0, 173.0, 178.0, 173.0]
y  = [621.0, 621.0, 630.0, 638.0, 636.0, 635.0, 635.0, 634.0, 628.0, 623.0, 617.0, 614.0, 618.0, 622.0, 617.0, 610.0, 612.0, 622.0, 626.0, 623.0, 622.0, 624.0, 625.0, 618.0, 613.0, 607.0, 608.0, 614.0, 614.0, 608.0, 603.0, 612.0, 616.0, 617.0, 616.0, 618.0, 622.0, 622.0, 615.0, 609.0, 607.0, 610.0, 612.0, 607.0, 606.0, 601.0, 611.0, 617.0, 615.0, 614.0, 619.0, 624.0, 625.0, 619.0, 615.0, 613.0, 618.0, 622.0, 616.0, 610.0, 614.0, 628.0, 625.0, 624.0, 628.0, 638.0, 646.0, 645.0]

def get_center(x,y):
    x = x[:-1]
    y = y[:-1]
    x0 = np.mean(x)
    y0 = np.mean(y)
    return x0, y0

#center point
x0,y0 = get_center(x,y)

#linear regression 
# fit_fn is now a function which takes in x and returns an estimate for y
fit = np.polyfit(x,y,1)
fit_fn = np.poly1d(fit) 

plt.plot(x0,y0,'bo')
#get the linear regression line
plt.plot(x,y, 'ro', x, fit_fn(x), '--k')

#get the perpendicular line going through the center point
plt.plot(x,(-1.0/fit[0])*(x-x0)+y0,'--b')
plt.grid(True)
plt.show()


`因为
x
y
轴的刻度不同


例如,添加线
plt.轴([120180590650])
,使
x
轴和
y
轴显示相同数量的标签(60)

,因为
x
y
轴没有相同的刻度


例如,添加线
plt.轴([120、180、590、650])
,以使
x
轴和
y
轴显示相同数量的标签(60)

x和y尺寸的刻度不同,因此线会扭曲。x和y尺寸的刻度不同,因此线会扭曲。