【问题标题】:How to calculate python 2D scatter points occupied area如何计算python 2D散点占用面积
【发布时间】:2016-06-16 01:52:08
【问题描述】:

我使用 matplotlib 绘制了这两个系列的 2000 点。从图片来看,前 2000 点的占用面积似乎小于后 2000 点。但是如果我想定量计算第一、二序列的2000点占用了多少面积,该怎么办呢?

非常感谢任何帮助、建议或 cmets。

非常感谢。

【问题讨论】:

标签: python algorithm numpy matplotlib


【解决方案1】:

此问题与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')

计算二维直方图以估计点的密度。 注意:箱数和范围是您必须根据数据调整的内容。

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')

最后,找到计数大于某个预定义值的位置。 注意:您还必须调整此阈值,以便在“占用”和“非占用”区域之间获得所需的区别:

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)

当然,所有的绘图都只是说明性的,对算法来说并不重要。

【讨论】:

  • 非常感谢!这正是我想要的!
猜你喜欢
  • 2016-12-16
  • 2011-06-07
  • 2018-05-19
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2011-08-12
  • 1970-01-01
相关资源
最近更新 更多