检测matplotlib散点图中的循环/交叉点

时间:2015-08-13 08:39:14

标签: python numpy matplotlib scipy

在我工作的某个时刻,我想出了that kind散点图。

我希望我的脚本能够检测到它"循环"并且给出了它所指的那一点(或其近似值):例如,在这种情况下,它将大约为[0.2,0.1]。

我尝试使用一些有代表性的点数,比如规范和/或参数,就像下面的代码一样。

import numpy as np

x,y = np.genfromtxt('points.dat',unpack=True)
norm = np.sqrt(x**2+y**2)
arg = np.arctan2(y,x)

left,right = np.meshgrid(norm,norm)
norm_diff = np.fabs(left - right)
mask = norm_diff == 0.
norm_diff_ma = np.ma.masked_array(norm_diff,mask)

left,right = np.meshgrid(arg,arg)
arg_diff = np.fabs(left - right)
mask = arg_diff == 0.
arg_diff_ma = np.ma.masked_array(arg_diff,mask)

list_of_indices = np.ma.where((norm_diff_ma<1.0e-04)*(arg_diff_ma<1.0e-04))

但是,它没有按预期工作:可能是因为数据集包含太多的点,并且两个对齐点之间的距离无论如何都与&#34;循环簇中的点之间的距离具有相同的数量级& #34; ...

我正在考虑检测群集,甚至可能检测散点图中的线条,然后看看两行之间是否有任何交叉点,但我担心我的图像处理技能只会到目前为止。

有没有算法,任何你能想到的任何技巧都可以在这里工作?

可以找到代表性数据样本here

编辑08/13/2015 16h18 :在与@DrBwts进行简短讨论之后,我仔细研究了在pyplot.contour()调用后获得的数据。使用以下例程提取所有顶点:

def contour_points(contour, steps=1):
try:
    loc_arr = np.row_stack([path.interpolated(steps).vertices for linecol in contour.collections for path in linecol.get_paths()])
except ValueError:
    loc_arr = np.empty((0,2))
finally:
    return loc_arr
y,x = contour_points(CS,steps=1).T

事实证明坐标(x,y)的点是有序的,因为调用pyplot.plot()会正确连接点。

0 个答案:

没有答案