palloc
palloc

Reputation: 333

Get XY value from mathplotlib

x = [-5,-4.19,-3.54,-3.31,-2.56,-2.31,-1.66,-0.96,-0.22,0.62,1.21]
y = [0.01,0.01,0.03,0.04,0.07,0.09,0.16,0.28,0.45,0.65,0.77]
plt.scatter(x, y)

new = np.linspace(-6, 2)
line, = plt.plot(new, inter(new, x, y))

xdata = line.get_xdata()
ydata = line.get_ydata()
print("X-datas: {}\n Y-datas: {}".format(xdata, ydata))

plt.show()

X-datas: [-6. -5.83673469 -5.67346939 -5.51020408 -5.34693878 -5.18367347 -5.02040816 -4.85714286 -4.69387755 -4.53061224 -4.36734694 -4.20408163 -4.04081633 -3.87755102 -3.71428571 -3.55102041 -3.3877551 -3.2244898 -3.06122449 -2.89795918 -2.73469388 -2.57142857 -2.40816327 -2.24489796 -2.08163265 -1.91836735 -1.75510204 -1.59183673 -1.42857143 -1.26530612 -1.10204082 -0.93877551 -0.7755102 -0.6122449 -0.44897959 -0.28571429 -0.12244898 0.04081633 0.20408163 0.36734694 0.53061224 0.69387755 0.85714286 1.02040816 1.18367347 1.34693878 1.51020408 1.67346939 1.83673469 2. ]

Y-datas: [-0.01204394 -0.00302114 0.00328497 0.00731081 0.00949282 0.01026743 0.01007108 0.0093402 0.00851122 0.00802059 0.00830473 0.00980007 0.01270645 0.01709244 0.02276363 0.02951097 0.03669359 0.04295611 0.04814326 0.0535818 0.06028353 0.06926028 0.08176299 0.09625576 0.11210497 0.12895267 0.14776008 0.16925751 0.19376669 0.22132663 0.25163393 0.28441688 0.31953663 0.35641761 0.39468924 0.43398094 0.47401019 0.51426535 0.55412183 0.59301685 0.63038764 0.66578449 0.69938197 0.73201447 0.7646666 0.79832295 0.83396812 0.87258672 0.91516335 0.96268261]

pic

inter is function which is interpolates the datas, with that I get the outputed arrays.

How could I get a specific value from the fitted curve. Let's assume that X = -2.31 then I would like to get Y value from the fitted curve.

Upvotes: 1

Views: 205

Answers (1)

Guimoute
Guimoute

Reputation: 4629

You get the index of the closest point to x = -2.31 using numpy.searchsorted.

n = np.searchsorted(xdata, -2.31)
y = ydata[n]

Remember that for this to work, you need xdata to be sorted, and the result will be slightly off because of the dx between two indices. The more points your data has, the more you reduce this inaccuracy to a point where it doesn't matter.

Upvotes: 2

Related Questions