0
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.

  • @kubatucka yes, that did. thank alot! if you want, you can enter your ans and ill mark it as correct. I was also able to get it from https://stackoverflow.com/questions/24749172/scipy-interpolate-get-x-values-that-produces-certain-y-value-2-lists-of-numbers – heretolearn Oct 20 '21 at 13:41
  • I'd rather you close this one as a duplicate :). – kubatucka Oct 20 '21 at 13:42

0 Answers0