我有5个点(x,y),并使用matplotlib的histogram2d函数创建一个显示不同颜色的热图,表示每个垃圾箱的密度。怎样才能获得回收箱中点数的频率?
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。
发布于 2014-03-07 21:26:53
你用的是4x4 =16个垃圾桶。如果您想要四个总垃圾箱,请使用2x2:
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
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 ]))发布于 2014-03-07 21:30:08
heatmap, xedges, yedges = np.histogram2d(x, y, bins=4)
heatmap /= heatmap.sum()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是一个分布,但它们不是您所要求的频率。
https://stackoverflow.com/questions/22260881
复制相似问题