0

我有一个二维数组,其中包含每个单元的热量。用正方形单位绘制 2D 热图很容易,但我怎样才能用六边形绘制一张。


为什么我需要这个?SOM(一种学习算法)输出一个六边形神经元网络。我可以从经过训练的模型中获得距离图(U-Matrix,2D 矩阵)。


matpyplothexbinjointplot(kind="hex")seaborn 中的函数只计算每个点的频率。输入参数是xy。但是我拥有的是一个带有权重的二维数组(或者说,我想绘制的颜色深度)。


例如,我不知道他是如何实现的 例如,我不知道他是如何实现的

4

1 回答 1

1

简而言之,您需要提供二维数组映射到 matplotlibhexbin函数的网格坐标。您可以通过多种方式制作这些网格,包括编写自己的函数,但也许最好的方式是使用np.meshgrid. 请注意,传递给hexbin函数的 X、Y 和 C 参数都必须是一维数组。

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()
im = ax.hexbin(
    X.reshape(-1), 
    Y.reshape(-1), 
    C=A.reshape(-1), 
    gridsize=int(A.shape[0]/2)
)

# 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.colorbar(im)
plt.show()

这给出了:

在此处输入图像描述

于 2021-04-09T09:30:57.453 回答