使用 matplotlib 的热图等距方向

Isometric orientation for heatmap with matplotlib

假设我们有如下热图

使用代码构建

import string
import numpy as np
from matplotlib import pyplot as plt
label=list(string.ascii_uppercase)
mdata = np.random.randn(3, len(label), len(label))
data = mdata[0, :, :]
data=np.tril(data,-1)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
plt.show()

是否可以使用Matplotlib、Seaborn或任何其他包渲染成isometric 对齐方式如下。

使用 matplotlib 的 3D toolkit, and using numpy's 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()