从 matplotlib 获取 XY 值

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 数据:[-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 数据:[-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]

inter 是对数据进行插值的函数,我得到了输出数组。

如何从拟合曲线中获取特定值。假设 X = -2.31 那么我想从拟合曲线中获取 Y 值。

您使用 numpy.searchsorted.

获得离 x = -2.31 最近点的索引
n = np.searchsorted(xdata, -2.31)
y = ydata[n]

请记住,要使其正常工作,您需要 xdata 进行排序,并且由于两个索引之间的 dx,结果会略有偏差。您的数据点越多,您就越能将这种不准确性降低到无关紧要的程度。