我使用 matplotlib 的方法 hexbin
计算数据的二维直方图。
但是我想得到六边形中心的坐标,以便进一步处理结果。
我在结果上使用 get_array()
方法获得了值,但我不知道如何获得 bin 坐标。
我尝试根据给定的 bin 数量和我的数据范围来计算它们,但我不知道每个方向上 bin 的确切数量。 gridsize=(10,2)
应该可以解决问题,但它似乎不起作用。
有什么想法吗?
最佳答案
我认为这可行。
from __future__ import division
import numpy as np
import math
import matplotlib.pyplot as plt
def generate_data(n):
"""Make random, correlated x & y arrays"""
points = np.random.multivariate_normal(mean=(0,0),
cov=[[0.4,9],[9,10]],size=int(n))
return points
if __name__ =='__main__':
color_map = plt.cm.Spectral_r
n = 1e4
points = generate_data(n)
xbnds = np.array([-20.0,20.0])
ybnds = np.array([-20.0,20.0])
extent = [xbnds[0],xbnds[1],ybnds[0],ybnds[1]]
fig=plt.figure(figsize=(10,9))
ax = fig.add_subplot(111)
x, y = points.T
# Set gridsize just to make them visually large
image = plt.hexbin(x,y,cmap=color_map,gridsize=20,extent=extent,mincnt=1,bins='log')
# Note that mincnt=1 adds 1 to each count
counts = image.get_array()
ncnts = np.count_nonzero(np.power(10,counts))
verts = image.get_offsets()
for offc in xrange(verts.shape[0]):
binx,biny = verts[offc][0],verts[offc][1]
if counts[offc]:
plt.plot(binx,biny,'k.',zorder=100)
ax.set_xlim(xbnds)
ax.set_ylim(ybnds)
plt.grid(True)
cb = plt.colorbar(image,spacing='uniform',extend='max')
plt.show()
关于python - 在 matplotlib 中使用 hexbin 获取 bins 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12951065/