查找在matplotlib中绘制的两个折线图的交点 [英] Finding the point of intersection of two line graphs drawn in matplotlib

查看:58
本文介绍了查找在matplotlib中绘制的两个折线图的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在matplotlib中找到两条线图的交点?

考虑代码

将 numpy 导入为 np导入matplotlib.pyplot作为plt无花果= plt.figure()ax = fig.add_subplot(111)ax.plot([1,2,3,4,5,6,7,8],[20,100,50,120,55,240,50,25],color='lightblue',linewidth=3)ax.plot([3,4,5,6,7,8,9],[25,35,14,67,88,44,120],color ='darkgreen',marker ='^')

我尝试参考

现在,我们使用以下源代码绘制这个交叉点,

将 numpy 导入为 np导入matplotlib.pyplot作为plt无花果= 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', 标记='^')# 绘制交叉点x3 = np.linspace(6, 7, 1000) # (6, 7) 交集范围y1_new = np.linspace(240, 50, 1000) #(6, 7)对应y1中的(240, 50)y2_new = np.linspace(67, 88, 1000) # (6, 7)对应y2中的(67, 88)idx = np.argwhere(np.isclose(y1_new,y2_new,atol = 0.1)).reshape(-1)ax.plot(x3 [idx],y2_new [idx],'ro')plt.show()

<小时>

最终用户不会乐意手动输入交叉范围.这是一个改进的版本,通过每两个段进行循环,但可能会浪费时间.

将 numpy 导入为 np导入matplotlib.pyplot作为plt无花果= 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', 标记='^')#获取公共范围,从`max(x1 [0],x2 [0])`到`min(x1 [-1],x2 [-1])`x_begin = max(x1[0], x2[0]) # 3x_end = min(x1[-1], x2[-1]) # 8points1 = [t for t in zip(x1, y1) if x_begin<=t[0]<=x_end] # [(3, 50), (4, 120), (5, 55), (6, 240)), (7, 50), (8, 25)]points2 = [对于zip(x2,y2)中t的t,如果x_begin <= t [0] <= x_end]#[(3,25),(4,35),(5,14),(6,67),(7,88),(8,44)]idx = 0nrof_points = len(points1)而idx<nrof_points-1:# 遍历两条线段y_min = min(points1[idx][1],points1[idx+1][1])y_max = max(points1 [idx + 1] [1],points2 [idx + 1] [1])x3 = np.linspace(points1[idx][0], points1[idx+1][0], 1000) # 例如,(6, 7) 交集范围y1_new = np.linspace(points1 [idx] [1],points1 [idx + 1] [1],1000)#例如,(6,7)对应于y1中的(240,50)y2_new = np.linspace(points2 [idx] [1],points2 [idx + 1] [1],1000)#例如,(6,7)对应于y2中的(67,88)tmp_idx = np.argwhere(np.isclose(y1_new,y2_new,atol = 0.1)).reshape(-1)如果tmp_idx:ax.plot(x3[tmp_idx], y2_new[tmp_idx], 'ro') # 绘制交叉点idx += 1plt.show()

Is there a way to find the point of intersection of two line graphs in matplotlib?

Consider the code

import numpy as np
import matplotlib.pyplot as plt


fig = plt.figure()


ax = fig.add_subplot(111)

ax.plot([1,2,3,4,5,6,7,8],[20,100,50,120,55,240,50,25],color='lightblue',linewidth=3)
ax.plot([3,4,5,6,7,8,9], [25,35,14,67,88,44,120], color='darkgreen', marker='^')

I tried referring to Python - matplotlib: find intersection of lineplots , but the method seems to be too intricate - it involves advanced maths concepts like Piecewise Polynomial Interpolation, can understand what the API is doing from docs but don't really get the concept behind it, if anyone could provide an easier solution or explain what is going on in the Piecewise polynomial solution, it would be of great help.

解决方案

Here is an ugly solution (an improved version is at the bottom). After plotting, we know that two line graphs make a cross at the range of (6, 7)

Now, we plot this cross point with the following source code,

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.plot(x3[idx], y2_new[idx], 'ro')

plt.show()


The end user would not be happy to input the cross range manually. Here is an improved version by looping over every two segments, but it might be a time consumer.

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

# Get the common range, from `max(x1[0], x2[0])` to `min(x1[-1], x2[-1])`   
x_begin = max(x1[0], x2[0])     # 3
x_end = min(x1[-1], x2[-1])     # 8

points1 = [t for t in zip(x1, y1) if x_begin<=t[0]<=x_end]  # [(3, 50), (4, 120), (5, 55), (6, 240), (7, 50), (8, 25)]
points2 = [t for t in zip(x2, y2) if x_begin<=t[0]<=x_end]  # [(3, 25), (4, 35), (5, 14), (6, 67), (7, 88), (8, 44)]

idx = 0
nrof_points = len(points1)
while idx < nrof_points-1:
    # Iterate over two line segments
    y_min = min(points1[idx][1], points1[idx+1][1]) 
    y_max = max(points1[idx+1][1], points2[idx+1][1]) 

    x3 = np.linspace(points1[idx][0], points1[idx+1][0], 1000)      # e.g., (6, 7) intersection range
    y1_new = np.linspace(points1[idx][1], points1[idx+1][1], 1000)  # e.g., (6, 7) corresponding to (240, 50) in y1
    y2_new = np.linspace(points2[idx][1], points2[idx+1][1], 1000)  # e.g., (6, 7) corresponding to (67, 88) in y2

    tmp_idx = np.argwhere(np.isclose(y1_new, y2_new, atol=0.1)).reshape(-1)
    if tmp_idx:
        ax.plot(x3[tmp_idx], y2_new[tmp_idx], 'ro')                 # Plot the cross point

    idx += 1

plt.show()

这篇关于查找在matplotlib中绘制的两个折线图的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆