确定并计算用户定义段内的点数

2024-09-30 14:28:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试识别和计算用户定义的“楔形”选择范围内的数据点。每个数据点都有自己的ID号,并从CSV目录文件加载,而“楔形”只是在绘图上绘制的一系列线。在

下面是一个绘图示例:

import matplotlib.pyplot as plt

x = ID['x']
y = ID['y']

plt.figure()
plt.scatter(x, y)
plt.plot([-1, -1, 2, 2, ], [4, 1, -2, 4], color='r', linewidth=1, linestyle='--')
plt.xlim(-4,4)
plt.ylim(-4,4)
plt.show()

示例图:http://i.imgur.com/UENwbks.png

我要找两个输出

1)“楔子”内所有ID(数据点)的列表

2)所有ID(数据点)的计数,一旦完成上述任务,这是一项相当简单的任务!在

我最初的想法是:

^{pr2}$

在哪里???是楔形物上方的面积(也许是线性方程?)。在

如有任何帮助,我们将不胜感激。在


Tags: 文件csv数据用户import目录id绘图
1条回答
网友
1楼 · 发布于 2024-09-30 14:28:55

您可以定义路径并使用其contains_points方法:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mpath

x, y = np.random.random((2, 100))*8 - 4
points = np.column_stack([x, y])
verts = np.array([[-1, -1, 2, 2, ], [4, 1, -2, 4]]).T
path = mpath.Path(verts)
points_inside = points[path.contains_points(points)]

plt.figure()
plt.scatter(x, y)
plt.plot([-1, -1, 2, 2, ], [4, 1, -2, 4], color='r', linewidth=1, linestyle=' ')
plt.scatter(points_inside[:,0], points_inside[:,1], c='r', s=50)
plt.xlim(-4,4)
plt.ylim(-4,4)
plt.show()

enter image description here

相关问题 更多 >