from scipy.interpolate import interp1d
x = np.linspace(0, 10, num=11, endpoint=True)
y = np.cos(-x**2/9.0)
plt.plot(x, y)
f2 = interp1d(x, y, kind='cubic')
xnew = np.linspace(0, 10, num=41, endpoint=True)
# print(xnew)
import matplotlib.pyplot as plt
plt.plot(x, y, 'o', xnew, f2(xnew), '--')
plt.legend(['data', 'cubic'], loc='best')
plt.show()
Here is my code for producing a graph after interpolating the data.
How can I obtain the x-value using an EXACT value of y-value, say 0.85? I have tried various methods such as using np.where, but I am unable to produce x-value using an EXACT y-value, because the y-values will be around 0.85, but not exactly on 0.85.
For example, I tried the following
line2d = plt.plot(x, y, 'o', xnew, f2(xnew), '--')
xvalues = line2d[0].get_xdata()
yvalues = line2d[0].get_ydata()
idx = np.where(yvalues == 0.85)
print(idx)
But the above will not give me a value, because there is no exact 0.85 yvalue.