我有一个二维柱状图,柱状图的柱状图大小为10。我想知道是否有numpy函数(或任何更快的方法)来获得在二维网格的每个柱状图中的点。有没有办法访问bin元素?
发布于 2020-01-16 23:42:52
我希望这能解决你的问题。然而,我相信其他人可以改进我的代码,因为我是python的新手。
使用matplotlib创建直方图
import matplotlib.pyplot as plt
rng = np.random.RandomState(10) # deterministic random data
a = np.hstack((rng.normal(size=100), rng.normal(loc=5, scale=2, size=1000)))
n ,bins ,patches = plt.hist(a, bins=10) # arguments are passed to np.histogram
plt.title("Histogram with '10' bins")
plt.show()重塑数组并..
newbin = np.repeat(np.reshape(bins,(-1, len(bins))), a.shape, axis=0)
newa = np.repeat(np.reshape(a,(len(a),-1)),len(bins),axis=1)
#index_bin = (np.where(newbin[:,0] >np.reshape(a,(1,-1))[:,0] ) )[0][0]
index_bin = (newbin>newa).argmax(axis=1).T测试
print(a[0] , bins)
print(index_bin[0])输出
1.331586504129518 [-2.13171211 -0.88255884 0.36659444 1.61574771 2.86490098 4.11405425
5.36320753 6.6123608 7.86151407 9.11066734 10.35982062]
3https://stackoverflow.com/questions/59771471
复制相似问题