我有一个问题。
我使用Matplotlib绘制了一个图形,如下所示:
from matplotlib import pyplot
import numpy
from scipy.interpolate import spline
widths = numpy.array([0, 30, 60, 90, 120, 150, 180])
heights = numpy.array([26, 38.5, 59.5, 82.5, 120.5, 182.5, 319.5])
xnew = numpy.linspace(widths.min(),widths.max(),300)
heights_smooth = spline(widths,heights,xnew)
pyplot.plot(xnew,heights_smooth)
pyplot.show()
现在我想使用宽度值作为参数查询高度值。我似乎无法找到如何做到这一点。请帮忙!提前谢谢!
答案 0 :(得分:7)
plot()
会返回一个有用的对象:[<matplotlib.lines.Line2D object at 0x38c9910>]
由此我们可以获得x轴和y轴值:
import matplotlib.pyplot as plt, numpy as np
...
line2d = plt.plot(xnew,heights_smooth)
xvalues = line2d[0].get_xdata()
yvalues = line2d[0].get_ydata()
然后我们可以获得其中一个宽度值的索引:
idx = np.where(xvalues==xvalues[-2]) # this is 179.3979933110368
# idx is a tuple of array(s) containing index where value was found
# in this case -> (array([298]),)
和相应的高度:
yvalues[idx]
# -> array([ 315.53469])
要检查我们是否可以使用get_xydata()
:
>>> xy = line2d[0].get_xydata()
>>> xy[-2]
array([ 179.39799331, 315.53469 ])
答案 1 :(得分:0)
您可以将数组转换为列表:
>>> heights[list(widths).index(30)]
38.5
用于插值结果:
s = xnew[56]
print s, heights_smooth[list(xnew).index(s)]
33.7123745819, 40.9547542163
由于xnew
是一个有序列表,您可以使用bisect module查找查询宽度的最接近宽度值,然后以类似的方式找到相应的高度:
....
import bisect
pyplot.plot(xnew,heights_smooth)
#33.1222 is a queried value which does not exist in xnew.
index_of_nearest_width = bisect.bisect_left(xnew, 33.1222)
width_val = xnew[index_of_closest_width]
print width_val, heights_smooth[list(xnew).index(width_val)]
#prints the nearest width to 33.1222 then the corresponding height.
33.7123745819 40.9547542163
答案 2 :(得分:0)
如果您愿意使用不同的样条函数,这是另一个选项:
from matplotlib import pyplot
import numpy
from scipy import interpolate
widths = numpy.array([0, 30, 60, 90, 120, 150, 180])
heights = numpy.array([26, 38.5, 59.5, 82.5, 120.5, 182.5, 319.5])
xnew = numpy.linspace(widths.min(),widths.max(),300)
heights_smooth = interpolate.splrep(widths,heights) #Use splrep instead of spline
#Select desired width values
width_vals = [0, 80.5, 38.98743]
#splev returns the value of your spline evaluated at the width values.
heights = interpolate.splev(width_vals, heights_smooth)
然后
In[]: heights
Out[]: array([ 26. , 74.1721985 , 44.47929453])
或者评估一下:
w = 167.2
heights = interpolate.splev(w, heights_smooth)
height = heights.item()
In[]: height
Out[]: 247.8396196684303
.item()
功能是必需的,因为splev
会返回array()