matplotlib.patches.Polygon函数contains_point()无法解析补丁中的点 [英] matplotlib.patches.Polygon function contains_point() fails to resolve a point in a patch
问题描述
我正在使用matplotlib.patches.Polygon更新图形上的一些彩色色块,并且我在每次代码迭代时都这样做.由于我已经以这种方式在代码中定义了补丁,因此我想使用 函数contains_point()来解析一个点是否在 补丁或在其外部.到目前为止,似乎可以正常使用的内容-但我不愿意这样做,我宁愿继续使用matplotlib来工作.在调用add_patch()之后会出现问题:在这种情况下,补丁将返回相同的坐标,但是函数contains_point()不会返回正确的值
I am using matplotlib.patches.Polygon to update some colored patches on a graph, and I'm doing it at each iteration of the code. Since I already defined the patches in my code this way, I wanted to use the function contains_point() to resolve whether a point is in the patch or outside of it. So Far what seems working is shapely - but I wouldn't like to do so, I'd rather keep working using matplotlib. The problem arises after calling add_patch(): in this case the patch will return the same coordinates but the function contains_point() will not return the correct value
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib import gridspec
import numpy as np
让我们定义一些我们要添加补丁的轴
let's define some axes on which we want to add the patches
gs = gridspec.GridSpec(1,1)
axx = [plt.subplot(g) for g in gs]
让我们看看axx是什么类型:
Let's see what type is axx:
In[84]: axx
Out[84]: [<matplotlib.axes._subplots.AxesSubplot at 0x7fd4b3409cd0>]
现在让我们用顶点v
v =
np.array([[ 6.89 , -10.00907385],
[ 5.11 , -10.00907385],
[ 5.11 , -14.68307385],
[ 6.89 , -14.68307385],
[ 6.89 , -10.00907385]])
b = [patches.Polygon(v, color = (0,0,1))]
我们可以测试一下:
In[86]: b[0].contains_point((5.5,-12))
Out[86]: True
现在让我们将补丁添加到先前初始化的子图中
Let's now add the patch to the subplot previously initialized
axx[0].add_patch(b[0])
如果同一点(5.5,-12)落在补丁b
let's test again if the same point (5.5,-12) falls in patch b
In[88]: b[0].contains_point((5.5,-12))
Out[88]: False
In[89]: b[0].get_xy()
Out[89]:
array([[ 6.89 , -10.00907385],
[ 5.11 , -10.00907385],
[ 5.11 , -14.68307385],
[ 6.89 , -14.68307385],
[ 6.89 , -10.00907385]])
推荐答案
将多边形添加到轴后,contains_point
使用内部坐标来确定多边形中是否包含某个点.
Once the polygon is added to the axes, contains_point
uses the internal coordinates to determine whether some point is contained in the polygon.
因此,您需要先将point
转换为屏幕坐标系,然后查询其是否包含在补丁poly
中,
You hence need to transform your point
to the screen coordinate system first and then query if it is contained in the patch poly
,
poly.contains_point(ax.transData.transform(point))
或者,使用补丁的path的contains_point
方法,而不对其进行任何转换,
Alternatvely, use the patches' path' contains_point
method, without specifying any transform to it,
poly.get_path().contains_point(point)
完整的示例代码:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
a = np.array([[2,6],[4,6],[4,8],[2,8],[2,6]])
poly = patches.Polygon(a)
point = (3,7)
cp1 = poly.contains_point(point)
print (cp1) # prints True
fig,ax = plt.subplots()
ax.add_patch(poly)
cp2 = poly.contains_point(ax.transData.transform(point))
print(cp2) # prints True
cp3 = poly.get_path().contains_point(point)
print(cp3) # prints True
ax.scatter(point[0],point[1], color="crimson", zorder=6)
plt.show()
这篇关于matplotlib.patches.Polygon函数contains_point()无法解析补丁中的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!