python求两条直线的交点_在matplotlib中求两个线图的交点

本文展示了如何在Python中使用numpy和matplotlib找出两条线的交点。通过绘制线图并找到交点的范围,然后计算对应点,最终在图形上标记出交点。
摘要由CSDN通过智能技术生成

下面是一个丑陋的解决方案(改进的版本在底部)。绘制之后,我们知道两个线图在(6, 7)范围内形成一个十字

现在,我们用下面的源代码绘制这个交叉点import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

x1 = [1,2,3,4,5,6,7,8]

y1 = [20,100,50,120,55,240,50,25]

x2 = [3,4,5,6,7,8,9]

y2 = [25,35,14,67,88,44,120]

ax.plot(x1, y1, color='lightblue',linewidth=3)

ax.plot(x2, y2, color='darkgreen', marker='^')

# Plot the cross point

x3 = np.linspace(6, 7, 1000) # (6, 7) intersection range

y1_new = np.linspace(240, 50, 1000) # (6, 7) corresponding to (240, 50) in y1

y2_new = np.linspace(67, 88, 1000) # (6, 7) corresponding to (67, 88) in y2

idx = np.argwhere(np.isclose(y1_new, y2_new, atol=0.1)).reshape(-1)

ax.plo

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值