使用 matplotlib 的 3D toolkit 和 numpy 的 triu_indices,您可以从三角矩阵创建条形图:
import numpy as np
import matplotlib.pyplot as plt
ax = plt.figure().add_subplot(projection='3d')
N = 26
data = np.random.randn(3, N, N)
for i, (plane, cmap) in enumerate(zip(data, ['Reds', 'Greens', 'Blues'])):
indices = np.triu_indices(N, 1)
norm = plt.Normalize(plane.min(), plane.max())
ax.bar(left=indices[0], bottom=indices[1], height=0.9,
zs=i, zdir='y',
color=plt.get_cmap(cmap)(norm(plane[indices])))
plt.show()
PS:为了得到完整的矩形,np.indices 的子数组需要被做成一维的:
import numpy as np
import matplotlib.pyplot as plt
ax = plt.figure().add_subplot(projection='3d')
N = 26
data = np.random.randn(3, N, N)
for i, (plane, cmap) in enumerate(zip(data, ['Reds', 'Greens', 'Blues'])):
indices = np.indices((N,N))
norm = plt.Normalize(plane.min(), plane.max())
ax.bar(left=indices[0].ravel(), bottom=indices[1].ravel(), height=0.9,
zs=i, zdir='y',
color=plt.get_cmap(cmap)(norm(plane).ravel()))
plt.show()