Python 如何在交点处绘制垂直线

Python 如何在交点处绘制垂直线,python,matplotlib,Python,Matplotlib,在下面的代码中,我使用axhline命令沿x轴绘制一条水平线。现在我想在两条线已经相交的点上画一条垂直线。我不能使用axvline,因为我不知道交叉点的值。对于axhline,我以50%的概率值绘制这条线 x=[1, 1.5, 2, 5, 5.5, 6 ] y=[1, 1, 0.89189189, 0.01086957, 0.01190476, 0] fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x,y,marker='o') plt.

在下面的代码中,我使用axhline命令沿x轴绘制一条水平线。现在我想在两条线已经相交的点上画一条垂直线。我不能使用axvline,因为我不知道交叉点的值。对于axhline,我以50%的概率值绘制这条线

x=[1, 1.5, 2, 5, 5.5, 6 ]
y=[1, 1, 0.89189189, 0.01086957, 0.01190476, 0]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y,marker='o')
plt.axhline(0.5, color='r')

这里有一种方法,通过找到连接第三点和第四点的直线方程。这不是一个通用的解决方案,而是为您的特定问题和数据集量身定制的

x=[1, 1.5, 2, 5, 5.5, 6 ]
y=[1, 1, 0.89189189, 0.01086957, 0.01190476, 0]

# Compute the equation of the line connecting 3rd and 4th point
m = (y[3]-y[2])/(x[3]-x[2]) # Slope
c = y[3]-m*x[3] # y-Intercept
x_inter = (0.5-c)/m # Desired intersection point

# Your plotting commands here
plt.axvline(x_inter, color='g')
输出