matplotlib - 从等高线提取数据
我想从均匀间隔的二维数据(类似图像的数据)的单个轮廓中获取数据。
基于类似问题中找到的示例: 如何获取轮廓绘制的线的 (x,y) 值绘图 (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) 对表示轮廓线。
除了手动循环此返回值、提取坐标并组装线条数组之外,是否有更好的方法从 matplotlib.path 对象获取数据?从 matplotlib.path 提取数据时是否有需要注意的陷阱?
或者,在 matplotlib
或更好的 numpy
/scipy
中是否有替代方案来执行类似的操作?理想的情况是获得描述该线的 (x,y) 对的高分辨率向量,该向量可用于进一步分析,因为一般来说我的数据集并不像上面的示例那样小或简单。
I would like to get data from a single contour of evenly spaced 2D data (an image-like data).
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()
The result of this call into cs.collections[0].get_paths()
is:
[Path([[ 4. 1.625 ]
[ 3.25 2. ]
[ 3. 2.16666667]
[ 2.16666667 3. ]
[ 2. 3.25 ]
[ 1.625 4. ]], None)]
Based on the plots, this result makes sense and appears to be collection of (y,x) pairs for the contour line.
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
?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于给定的路径,您可以获得如下点:
For a given path, you can get the points like this:
来自: http://matplotlib.org/api/path_api.html#module-matplotlib .路径
否则,我不太确定你的问题是什么。在处理坐标时,[Zip] 有时是一个有用的内置函数。 1
from: http://matplotlib.org/api/path_api.html#module-matplotlib.path
Otherwise, I'm not really sure what your question is. [Zip] is a sometimes useful built in function when working with coordinates. 1
所有路径的顶点可以简单地通过以下方式返回为 float64 的 numpy 数组:
其中
cs
的定义如原始问题中所示:更详细:
浏览集合提取路径和顶点并不是最直接或最快的事情。返回的 Contour 对象实际上通过 cs.allsegs 具有段的属性,它返回形状 [level][element][vertex_coord] 的嵌套列表:
请参阅参考:
https://matplotlib.org/stable/api/contour_api.html
The vertices of an all paths can be returned as a numpy array of float64 simply via:
where
cs
is defined as in the original question as:More detailed:
Going through the collections and extracting the paths and vertices is not the most straight forward or fastest thing to do. The returned Contour object actually has attributes for the segments via
cs.allsegs
, which returns a nested list of shape [level][element][vertex_coord]:See reference:
https://matplotlib.org/stable/api/contour_api.html
我面临着类似的问题,并偶然发现 this matplotlib 列表讨论。
基本上,可以去掉绘图并直接调用底层函数,虽然不是超级方便,但也是可能的。该解决方案也不是像素精确的,因为底层代码中可能正在进行一些插值。
I am facing a similar problem, and stumbled over this matplotlib list discussion.
Basically, it is possible to strip away the plotting and call the underlying functions directly, not super convenient, but possible. The solution is also not pixel precise, as there is probably some interpolation going on in the underlying code.