[Python] Show labels on maps in Seaborn heatmap
top of page

[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 All

[Python] Output pandas.DataFrame as json

Summary Data analysis is performed using python. The analysis itself is performed using pandas, and the final results are stored in pandas.DataFrame format. I want to output this result to a file in j

[Python] Conditionally fitting

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 cases where you want to condition the fitting parameters. For

Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page