ema*_*max -2 python geometry patch point
我有一个数据框df,其中包含IDs系统中所有点 ( )之间的距离。所以df看起来像下面这样:
df
radius ID1 ID2 x1 y1 x2 y2
0 0.454244 100 103 103.668919 1.335309 103.671812 1.332424
1 1.016734 100 123 103.668919 1.335309 103.677598 1.332424
2 0.643200 103 123 103.671812 1.332424 103.677598 1.332424
3 1.605608 100 124 103.668919 1.335309 103.677598 1.346851
4 1.728349 103 124 103.671812 1.332424 103.677598 1.346851
Run Code Online (Sandbox Code Playgroud)
我想计算所有点之间的圆,然后检查女巫点是否在该圆内。对于每个点,我都有一个单独的数据帧中的坐标coordinates。
coordinates
ID x y
0 100 103.668919 1.335309
1 103 103.671812 1.332424
2 124 103.677598 1.346851
3 125 103.677598 1.349737
4 134 103.680491 1.341080
5 135 103.680491 1.343966
6 136 103.680491 1.346851
7 137 103.680491 1.349737
8 138 103.680491 1.352622
9 146 103.683384 1.341080
Run Code Online (Sandbox Code Playgroud)
这里的代码
from matplotlib.patches import Circle
for i in df.index:
x = df.x1[i]
y = df.y1[i]
circ = Circle((x, y), radius = df.radius)
## it works until here: from now I need to understand what to do
## and in particular I need to find which points are inside the circle
points = circ.contains_point([coordinates.x, coordinates.y])
Run Code Online (Sandbox Code Playgroud)
返回错误
ValueError: setting an array element with a sequence.
Run Code Online (Sandbox Code Playgroud)
当我遇到这样的问题时,我总是做一个小的健全性测试:
from matplotlib.patches import Circle
circ = Circle((0, 0), radius = 1)
print(circ.contains_point([0.5,0.5]))
print(circ.contains_point([2,2]))
Run Code Online (Sandbox Code Playgroud)
我得到(如预期)
True
False
Run Code Online (Sandbox Code Playgroud)
所以coordinates.x和coordinates.y可能是数组,它解释了消息。
contains_points适用于tuple或list2 个标量。
要生成列表,您可以在列表理解中执行循环:
points = [(x,y) for x,y in zip(coordinates.x, coordinates.y) if circ.contains_point(x,y)]
Run Code Online (Sandbox Code Playgroud)