如何从图中提取点? [英] How to extract points from a graph?
本文介绍了如何从图中提取点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题.
我使用 Matplotlib 绘制了一个图形,如下所示:
from matplotlib import pyplot导入 numpy从 scipy.interpolate 导入样条宽度 = numpy.array([0, 30, 60, 90, 120, 150, 180])高度 = 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 = 样条(宽度,高度,xnew)pyplot.plot(xnew,heights_smooth)pyplot.show()
现在我想使用宽度值作为参数来查询高度值.我似乎无法找到如何做到这一点.请帮忙!提前致谢!
解决方案
plot()
返回一个有用的对象:[
从中我们可以得到 x 轴和 y 轴的值:
导入 matplotlib.pyplot 为 plt,numpy 为 np...line2d = plt.plot(xnew,heights_smooth)xvalues = line2d[0].get_xdata()yvalues = line2d[0].get_ydata()
然后我们可以得到宽度值之一的索引:
idx = np.where(xvalues==xvalues[-2]) # 这是 179.3979933110368# idx 是一个数组元组,其中包含找到值的索引# 在这种情况下 ->(数组([298]),)
和对应的高度:
yvalues[idx]# ->数组([315.53469])
我们可以使用get_xydata()
来检查:
I have a question.
I have plotted a graph using Matplotlib like this:
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()
Now I want to query a height value using width value as an argument. I cannot seem to find how to do that. Please help! Thanks in advance!
解决方案
plot()
returns a useful object: [<matplotlib.lines.Line2D object at 0x38c9910>]
From that we can get x- and y-axis values:
import matplotlib.pyplot as plt, numpy as np
...
line2d = plt.plot(xnew,heights_smooth)
xvalues = line2d[0].get_xdata()
yvalues = line2d[0].get_ydata()
Then we can get the index of one of the width values:
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]),)
And the corresponding height:
yvalues[idx]
# -> array([ 315.53469])
To check we can use get_xydata()
:
>>> xy = line2d[0].get_xydata()
>>> xy[-2]
array([ 179.39799331, 315.53469 ])
这篇关于如何从图中提取点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文