python - 使用 matplotlib 的热图等距方向

标签 python matplotlib seaborn isometric

假设我们有如下的热图

enter image description here

使用代码构建

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 或任何其他包渲染成等轴测图 对齐方式如下。

enter image description here

最佳答案

使用 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()

drawing heatmap in 3D planes

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()

3D planes of heatmaps

关于python - 使用 matplotlib 的热图等距方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69153035/

相关文章:

python - 使用 Py2app 时出现导入错误

Python - 使用 pyplot 将二维数组映射到网格?

python - matplotlib的 "barh"中height变量的单位是什么?

python - 将每个 seaborn 图形附加到 for 循环内的 PDF

Python 类型提示和 linter

python - 如何在装饰器中捕获异常但允许调用者也捕获它?

python - struct.unpack 和 struct.pack 是如何工作的?

python - 从多个数据帧创建单个箱线图

pandas - 使用seaborn jointplot绘制2D numpy数组

python - 从 Seaborn Boxplot 中提取异常值