Search
[Python] Show labels on maps in Seaborn heatmap
- M.R

- Oct 16, 2021
- 1 min read
Overview
When creating a heatmap with seaborn, you may want to display a label on the map.
This time, I will introduce such a method.
Method
When displaying the value as it is that creates the heatmap
If you set annot = True, you can display the value of the data passed to the argument of heatmap on the map.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x=np.random.randint(0, 10, (8, 8))
fig, ax=plt.subplots()
ax=sns.heatmap(x, annot=True)
plt.show()
When displaying a label different from the value that creates the heatmap
You may want to display a label different from the data that creates the heatmap.
For example, for each number, 1 is displayed if a certain condition is met, 0 is displayed if it is not met, and so on.
In such a case, you can pass an array of the same size as the data to annot.
y=np.zeros(x.shape)
for i in range(x.shape[0]):
for j in range(x.shape[1]):
if x[i][j]%2==0:
y[i][j]=1 //If x is even, y is 1. //omit
ax=sns.heatmap(x, annot=y)
//omit






Comments