如何在交点处画一条垂直线

2024-09-29 21:38:59 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,我使用axhline命令沿x轴绘制一条水平线。现在我想在两条线已经相交的地方画一条垂直线。我不能用axvline,因为我不知道交集的值。以阿克斯林为例,我用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')

Tags: 代码命令地方fig绘制pltax概率
1条回答
网友
1楼 · 发布于 2024-09-29 21:38:59

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

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')

输出

enter image description here

相关问题 更多 >

    热门问题