A = np.random.random((10, 10))
X, Y = np.meshgrid(range(A.shape[0]), range(A.shape[-1]))
X, Y = X*2, Y*2
# Turn this into a hexagonal grid
for i, k in enumerate(X):
if i % 2 == 1:
X[i] += 1
Y[:,i] += 1
fig, ax = plt.subplots()
ax.hexbin(
X.reshape(-1),
Y.reshape(-1),
C=A.reshape(-1),
gridsize=A.shape[0]
)
# the rest of the code is adjustable for best output
ax.set_aspect(0.8)
ax.set(xlim=(-4, X.max()+4,), ylim=(-4, Y.max()+4))
ax.axis(False)
plt.show()
简而言之,您需要提供二维数组映射到matplotlib的
hexbin
函数的网格坐标。您可以通过多种方式创建这些网格,包括编写自己的函数,但最好的方法可能是只使用np.meshgrid
。请注意,传递给hexbin
函数的X、Y和C参数都必须是1d数组这使得:
相关问题 更多 >
编程相关推荐