我有个问题。
我已经使用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()
现在,我想使用宽度值作为参数来查询高度值。我似乎找不到解决方法。请帮忙!提前致谢!
plot()返回一个有用的对象:[<matplotlib.lines.Line2D object at 0x38c9910>] 从中,我们可以获得x和y轴的值:
plot()
[<matplotlib.lines.Line2D object at 0x38c9910>]
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():
get_xydata()
>>> xy = line2d[0].get_xydata() >>> xy[-2] array([ 179.39799331, 315.53469 ])