1

I have data for which I can make a heatmap in the form of a triangle.

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)

I have map looking like this:

1

And I would like to create a mirror image and merge it with orginal heatmap:

2

JohanC
  • 71,591
  • 8
  • 33
  • 66
Kate
  • 51
  • 3
  • 3
    Does this mirror give any additional information? Or makes it something easier to verify? Or is it just a fancy visualization? Do you really want to leave out the labeling? – JohanC Jan 04 '22 at 17:33
  • In my case it easier to read data, when this heatmap is much bigger then that. And of course, I want add labeling, but only for x-axis. Until I create map looking like that ​I didn't thing about labeling. – Kate Jan 04 '22 at 17:49

1 Answers1

2

You could stack the matrix together with its mirror image. Use np.hstack([c[:, :0:-1], c]) if you only want the central column once.

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

heatmap together with its mirror image

JohanC
  • 71,591
  • 8
  • 33
  • 66