简体   繁体   中英

How to find the intersection points between two plotted curves in matplotlib?

I want to find the intersection of two curves. For example below. 在此处输入图片说明 There can be multiple intersection points. Right now I am finding the intersection points by finding the distance between x,y coordinates. But this method not giving accurate points sometimes when point of intersection is in between(17-18 x-axis) as you can see in the plot.

I need to get all the points from the curve to solve this. Is there any method to get all of them?

The curve is simply a series of straight lines connecting each of your points. Therefore, if you want to increase the number of points, you can simply do a linear extrapolation between each pairs of points:

x1,x2 = 17,20
y1,y2 = 1,5

N = 20
x_vals = np.linspace(x1,x2,N)
y_vals = y1+(x_vals-x1)*((y2-y1)/(x2-x1))

fig, ax = plt.subplots()
ax.plot([x1,x2],[y1,y2],'k-')
ax.plot(x_vals,y_vals, 'ro')

在此处输入图片说明

Here is my approach. I first created two test curves, using only 12 sample points, just to illustrate the concept. The exact equations of the curves are lost after creating the arrays with sample points.

Then, the intersections between the two curves are searched. By going through the array point by point, and check when one curve goes from below the other to above (or the reverse), the intersection point can be calculated by solving a linear equation.

Afterwards the intersection points are plotted to visually check the result.

import numpy as np
from matplotlib import pyplot as plt

N = 12
t = np.linspace(0, 50, N)
curve1 = np.sin(t*.08+1.4)*np.random.uniform(0.5, 0.9) + 1
curve2 = -np.cos(t*.07+.1)*np.random.uniform(0.7, 1.0) + 1
# note that from now on, we don't have the exact formula of the curves, as we didn't save the random numbers
# we only have the points correspondent to the given t values

fig, ax = plt.subplots()
ax.plot(t, curve1,'b-')
ax.plot(t, curve1,'bo')
ax.plot(t, curve2,'r-')
ax.plot(t, curve2,'ro')

intersections = []
prev_dif = 0
t0, prev_c1, prev_c2 = None, None, None
for t1, c1, c2 in zip(t, curve1, curve2):
    new_dif = c2 - c1
    if np.abs(new_dif) < 1e-12: # found an exact zero, this is very unprobable
        intersections.append((t1, c1))
    elif new_dif * prev_dif < 0:  # the function changed signs between this point and the previous
        # do a linear interpolation to find the t between t0 and t1 where the curves would be equal
        # this is the intersection between the line [(t0, prev_c1), (t1, c1)] and the line [(t0, prev_c2), (t1, c2)]
        # because of the sign change, we know that there is an intersection between t0 and t1
        denom = prev_dif - new_dif
        intersections.append(((-new_dif*t0  + prev_dif*t1) / denom, (c1*prev_c2 - c2*prev_c1) / denom))
    t0, prev_c1, prev_c2, prev_dif = t1, c1, c2, new_dif
print(intersections)

ax.plot(*zip(*intersections), 'go', alpha=0.7, ms=10)
plt.show()

说明情节

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM