首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为histogram2d获取每个垃圾箱中的频率

为histogram2d获取每个垃圾箱中的频率
EN

Stack Overflow用户
提问于 2014-03-07 21:20:17
回答 2查看 277关注 0票数 1

我有5个点(x,y),并使用matplotlib的histogram2d函数创建一个显示不同颜色的热图,表示每个垃圾箱的密度。怎样才能获得回收箱中点数的频率?

代码语言:javascript
复制
    import numpy as np
    import numpy.random
    import pylab as pl
    import matplotlib.pyplot as plt

    x = [.3, -.3, -.3, .3, .3]
    y = [.3, .3, -.3, -.3, -.4]

    heatmap, xedges, yedges = np.histogram2d(x, y, bins=4)
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

    plt.clf()
    plt.imshow(heatmap, extent=extent)
    plt.show()

    pl.scatter(x,y)
    pl.show()

因此,使用4个回收箱,我希望每个垃圾箱中的频率是.2、.2、.2和.4。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-07 21:26:53

你用的是4x4 =16个垃圾桶。如果您想要四个总垃圾箱,请使用2x2:

代码语言:javascript
复制
In [45]: np.histogram2d(x, y, bins=2)
Out[45]: 
(array([[ 1.,  1.],
       [ 2.,  1.]]),
 array([-0.3,  0. ,  0.3]),
 array([-0.4 , -0.05,  0.3 ]))

可以使用tuple:bins=(2,2)指定输出的完整形状。

如果要使输出正常化,请使用normed=True

代码语言:javascript
复制
In [50]: np.histogram2d(x, y, bins=2, normed=True)
Out[50]: 
(array([[ 1.9047619 ,  1.9047619 ],
       [ 3.80952381,  1.9047619 ]]),
 array([-0.3,  0. ,  0.3]),
 array([-0.4 , -0.05,  0.3 ]))
票数 1
EN

Stack Overflow用户

发布于 2014-03-07 21:30:08

代码语言:javascript
复制
heatmap, xedges, yedges = np.histogram2d(x, y, bins=4)
heatmap /= heatmap.sum()

代码语言:javascript
复制
In [57]: heatmap, xedges, yedges = np.histogram2d(x, y, bins=4)

In [58]: heatmap
Out[58]: 
array([[ 1.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 2.,  0.,  0.,  1.]])

In [59]: heatmap /= heatmap.sum()

In [60]: heatmap
Out[60]: 
array([[ 0.2,  0. ,  0. ,  0.2],
       [ 0. ,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  0. ,  0. ],
       [ 0.4,  0. ,  0. ,  0.2]])

请注意,如果您使用normed=True,那么heatmap.sum()一般不会等于1,相反,heatmap乘以bin的面积等于1,这使得heatmap是一个分布,但它们不是您所要求的频率。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22260881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档