2016-03-02 65 views
3

我已经用matplotlib绘制了这两个系列的2000点。从图片看来,第一个2000分的占用面积似乎比第二个2000分小。但是如果我想定量计算2000点的第一和第二连续点占用了多少区域,我该怎么办?如何计算蟒蛇二维散点占用区域

enter image description here enter image description here

我真的很感激任何帮助,建议或意见。

非常感谢。

+0

看看'scipy''ConvexHull',http://stackoverflow.com/questions/35664675/in-scipys-convexhull-what-does-area-measure – hpaulj

回答

6

此问题与matplotlib无关,还需要定义“占用区域”,根据您拥有的数据类型的不同,这可能会有所不同。如果你想要一种非严格逼近,这里是做到这一点的一种方法:

首先,一些测试数据:

import matplotlib 
import matplotlib.pyplot as plt 

import numpy 


x = numpy.random.normal(size=10000) 
y = numpy.random.normal(size=10000) 

fig = plt.figure() 
s = fig.add_subplot(1, 1, 1, aspect=1) 
s.set_xlim(-4, 4) 
s.set_ylim(-4, 4) 
s.scatter(x, y) 
fig.savefig('t1.png') 

enter image description here

计算二维直方图来估算点的密度。 注意:垃圾箱数量和范围是您必须针对您的数据进行调整的。

hist, xedges, yedges = numpy.histogram2d(x, y, bins=20, range=[[-4, 4], [-4, 4]]) 

fig = plt.figure() 
s = fig.add_subplot(1, 1, 1) 
s.set_xlim(-4, 4) 
s.set_ylim(-4, 4) 
s.imshow(
    hist, interpolation='nearest', 
    extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], 
    cmap=matplotlib.cm.viridis) 
fig.savefig('t2.png') 

enter image description here

最后,找到地方计数的数量比一些预定义的值。 注:你必须得调整这个门槛,让与“占领”和“非占领”地区所需的区别:

over_threshold = hist > 10 

fig = plt.figure() 
s = fig.add_subplot(1, 1, 1) 
s.set_xlim(-4, 4) 
s.set_ylim(-4, 4) 
s.imshow(
    over_threshold, interpolation='nearest', 
    extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]], 
    cmap=matplotlib.cm.viridis) 
fig.savefig('t3.png') 

area = over_threshold.sum() * (xedges[1] - xedges[0]) * (yedges[1] - yedges[0]) 
print(area) 

enter image description here

所有绘图,当然,是纯粹是说明性的,对算法来说不是必需的。

+0

非常感谢!这正是我想要的! –