如何确定哪些点在多边形内而哪些不在多边形内(大量点)? [英] How to determine which points are inside of a polygon and which are not (large number of points)?

查看:120
本文介绍了如何确定哪些点在多边形内而哪些不在多边形内(大量点)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大组数据点(100,000+)存储在二维numpy数组中(第一列:x坐标,第二列:y坐标).我还有几个一维数组,用于存储每个数据点的其他信息.我现在想从这些一维数组的子集中创建图,这些图仅包含给定多边形中的点.

I've got a large set of data points (100,000+) stored in a 2-dimensional numpy array (1st column: x coordinates, 2nd column: y coordinates). I've also got several 1-dimensional arrays storing additional information for each data point. I'd now like to create plots from subsets of these 1D arrays which include only the points which are in a given polygon.

我想出了以下既不优雅也不快捷的解决方案:

I came up with the following solution which is neither elegant nor fast:

#XY is the 2D array.
#A is one of the 1D arrays.
#poly is a matplotlib.patches.Polygon

mask = np.array([bool(poly.get_path().contains_point(i)) for i in XY])

matplotlib.pylab.hist(A[mask], 100)
matplotlib.pylab.show()

能否请您帮助我改进此代码?我尝试使用np.vectorize而不是列表理解,但是无法使其正常工作.

Could you please help me to improve this code? I tried playing around with np.vectorize instead of the list comprehension but could not manage to get it to work.

推荐答案

使用

Use matplotlib.nxutils.points_inside_poly, which implements a very efficient test.

更新:请注意,自1.2.0版的matplotlib开始不推荐使用points_inside_poly.请改用 matplotlib.path.Path.contains_points

Update: Note that points_inside_poly is deprecated since version 1.2.0 of matplotlib. Use matplotlib.path.Path.contains_points instead.

这篇关于如何确定哪些点在多边形内而哪些不在多边形内(大量点)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆