I want to calculate the intersection points between a line and a interpolation function. I can plot the interpolation as well the line in the figure:
import matplotlib.pyplot as plt
import numpy as np
import scipy.interpolate as scp
y = [ 815.97, 815.95, 815.98, ..., 815.9] #60 elements
x = [405383892, 405383894, 405383895, ..., 405383896] #60 elements
mediana = np.median(x)
f = scp.interp1d(x,y,bounds_error=False,fill_value=0.,kind='cubic')
new_x_array = np.arange(min(x),max(x),0.01)
plt.figure()
plt.plot(new_x_array,f(new_x_array),'r-')
plt.plot([x[0],x[59]], [mediana,mediana], 'g-')
plt.plot(x, y, 'mo')

But, I don't find the way to calculate the intersections points between the red function and the green line. What is the usual way to find an intersection between the two?
interp1d does a spline interpolation between your data points. As far as I know, there is no method in scipy which gives you all roots on a domain for this interpolation, but because its spline interpolation you can solve this.
You can do the following.
So for low spline order this should be straight forward.
Thanks for the answer! I will use that solution in the future. Finally I solved it looking for the values close to the interpolation line:
index=[]
for pos in range(0,len(new_x_array)-1):
if (mediana > f(new_x_array[pos])) & (mediana < f(new_x_array[pos+1])):
index.append(new_x_array[pos])
if (mediana < f(new_x_array[pos])) & (mediana > f(new_x_array[pos+1])):
index.append(new_x_array[pos])
The index array takes the position close to the line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With