Sub*_* S. 6 python numpy matplotlib scipy
我有个问题.
我使用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()
Run Code Online (Sandbox Code Playgroud)
现在我想使用宽度值作为参数查询高度值.我似乎无法找到如何做到这一点.请帮忙!提前致谢!
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()
Run Code Online (Sandbox Code Playgroud)
然后我们可以得到一个宽度值的索引:
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]),)
Run Code Online (Sandbox Code Playgroud)
和相应的高度:
yvalues[idx]
# -> array([ 315.53469])
Run Code Online (Sandbox Code Playgroud)
要检查我们可以使用get_xydata()
:
>>> xy = line2d[0].get_xydata()
>>> xy[-2]
array([ 179.39799331, 315.53469 ])
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19816 次 |
最近记录: |