如何将热图与其镜像合并

How to merge a heatmap with its mirror image

我有数据可以制作三角形热图。

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

c = np.array([[1, np.nan, np.nan, np.nan, np.nan, np.nan],
              [-0.140898, 1, np.nan, np.nan, np.nan, np.nan],
              [0.0867051, -0.0934162, 1, np.nan, np.nan, np.nan],
              [0.117242, -0.0332325, 0.0414388, 1, np.nan, np.nan],
              [-0.120879, 0.00294446, -0.11504, -0.101007, 1, np.nan],
              [-0.696967, 0.0913504, -0.0823251, -0.0598827, 0.127752, 1]])

fig,ax = plt.subplots(1,1,sharex=True)

sns.heatmap(c,cmap='jet',
            vmin = -1,
            vmax = 1,
            ax = ax,
            annot = True,
            fmt = '.1f',
            annot_kws={"fontsize":5},
            cbar = True)

我有这样的地图:

我想创建一个镜像并将其与原始热图合并:

您可以将矩阵与其镜像堆叠在一起。如果您只想要中央列一次,请使用 np.hstack([c[:, :0:-1], c])

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

c = np.array([[1, np.nan, np.nan, np.nan, np.nan, np.nan],
              [-0.140898, 1, np.nan, np.nan, np.nan, np.nan],
              [0.0867051, -0.0934162, 1, np.nan, np.nan, np.nan],
              [0.117242, -0.0332325, 0.0414388, 1, np.nan, np.nan],
              [-0.120879, 0.00294446, -0.11504, -0.101007, 1, np.nan],
              [-0.696967, 0.0913504, -0.0823251, -0.0598827, 0.127752, 1]])

labels = ['A', 'B', 'C', 'D', 'E', 'F']
fig, ax = plt.subplots(1, 1)
sns.heatmap(np.hstack([c[:, ::-1], c]),
            xticklabels=labels[::-1] + labels,
            yticklabels=[],
            cmap='turbo',
            vmin=-1,
            vmax=1,
            ax=ax,
            annot=True,
            fmt='.1f',
            annot_kws={"fontsize": 5},
            cbar=True)
# optionally add labels for the rows
for i, label in enumerate(labels):
    ax.text(len(labels) - i - 1.2, i + 0.5, label, ha='right', va='center')
plt.show()