Reputation: 51
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:
And I would like to create a mirror image and merge it with orginal heatmap:
Upvotes: 1
Views: 321
Reputation: 80309
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()
Upvotes: 2