matplotlib-从轮廓线提取数据 [英] matplotlib - extracting data from contour lines
问题描述
我想从均匀分布的2D数据(类似图像的数据)的单个轮廓中获取数据.
I would like to get data from a single contour of evenly spaced 2D data (an image-like data).
基于在类似问题中找到的示例:如何获取绘制线的(x,y)值通过等高线图(matplotlib)?
Based on the example found in a similar question: How can I get the (x,y) values of the line that is ploted by a contour plot (matplotlib)?
>>> import matplotlib.pyplot as plt
>>> x = [1,2,3,4]
>>> y = [1,2,3,4]
>>> m = [[15,14,13,12],[14,12,10,8],[13,10,7,4],[12,8,4,0]]
>>> cs = plt.contour(x,y,m, [9.5])
>>> cs.collections[0].get_paths()
此呼叫cs.collections[0].get_paths()
的结果是:
[Path([[ 4. 1.625 ]
[ 3.25 2. ]
[ 3. 2.16666667]
[ 2.16666667 3. ]
[ 2. 3.25 ]
[ 1.625 4. ]], None)]
基于绘图,此结果有意义,并且似乎是轮廓线的(y,x)对的集合.
Based on the plots, this result makes sense and appears to be collection of (y,x) pairs for the contour line.
除了手动循环返回值,提取坐标和装配线的数组之外,还有更好的方法从matplotlib.path
对象取回数据吗?从matplotlib.path
提取数据时是否有陷阱要注意?
Other than manually looping over this return value, extracting the coordinates and assembling arrays for the line, are there better ways to get data back from a matplotlib.path
object? Are there pitfalls to be aware of when extracting data from a matplotlib.path
?
或者,在matplotlib
或更好的numpy
/scipy
中是否有替代品可以做类似的事情?理想的做法是获得一个描述这条线的(x,y)对的高分辨率矢量,该矢量可以用于进一步的分析,因为通常我的数据集并不像上面的示例那样小或简单.
Alternatively, are there alternatives within matplotlib
or better yet numpy
/scipy
to do a similar thing? Ideal thing would be to get a high resolution vector of (x,y) pairs describing the line, which could be used for further analysis, as in general my datasets are not a small or simple as the example above.
推荐答案
对于给定的路径,您可以得到如下几点:
For a given path, you can get the points like this:
p = cs.collections[0].get_paths()[0]
v = p.vertices
x = v[:,0]
y = v[:,1]
这篇关于matplotlib-从轮廓线提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!