python用matplotlib画点到直线的投影的问题

import numpy as np
import matplotlib.pyplot as plt
#直线y=x
m = 1
b = 0
plt.plot([0, 1], [0,1], label='y')
#点1,2
x0 = 0
y0 = 1
x2 = 1
y2 = 0
#计算点在直线上投影点
x1 = (m*y0+x0-m*b)/(m**2+1)
y1 = (m**2*y0+m*x0+b)/(m**2+1)
#画出点并连线
plt.scatter(x0,y0)
plt.scatter(x1,y1)
plt.scatter(x2,y2)
plt.plot([x0,x1],[y0,y1])
plt.plot([x1,x2],[y1,y2])
plt.axis([0,1,0,1])
plt.legend()
plt.show()

得到结果如下:


得到图形后发现居然(0,1)(1,0)在y=x直线上的投影连线和y=x不垂直,后来细细一看发现原来是坐标刻度表示不一样的问题,百度了半天没能解决,先码在这,解决了回来填坑。

猜你喜欢

转载自blog.csdn.net/A993852/article/details/80099144