Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create mirror image of heatmap and megre it in Seaborn in Python?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

2

>Solution :

You could stack the matrix together with its mirror image. But it is unclear what you want to do with the labeling. (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)

heatmap together with its mirror image

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading