[Python] Show labels on maps in Seaborn heatmap
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
Recent Posts
See AllSummary Data analysis is performed using python. The analysis itself is performed using pandas, and the final results are stored in...
Phenomenon I get a title error when trying to import firestore with raspberry pi. from from firebase_admin import firestore ImportError:...
Overview If you want to do fitting, you can do it with scipy.optimize.leastsq etc. in python. However, when doing fitting, there are many...
Comments