#91 Customize seaborn heatmap

The graph #90 explains how to make a heatmap from 3 different input formats. In this post, I describe how to customize the appearance of these heatmaps. These 4 examples start by importing libraries and making a data frame:

# library
import seaborn as sns
import pandas as pd
import numpy as np

# Create a dataset (fake)
df = pd.DataFrame(np.random.random((10,10)), columns=["a","b","c","d","e","f","g","h","i","j"])

1/ Annotate each cell with value

annot=True will add a label into each cell, providing the exact value behind the color.


sns.heatmap(df, annot=True, annot_kws={"size": 7})
#sns.plt.show()

2/ Custom grid lines
sns.heatmap(df, linewidths=2, linecolor='yellow')
#sns.plt.show()
3/ Remove X or Y labels

yticklabels and xticklabels control the presence / abscence of labels for the Y and X axis respectively.

sns.heatmap(df, yticklabels=False)
#sns.plt.show()
4/ Remove color bar

Code is self explanatory.

sns.heatmap(df, cbar=False) 
#sns.plt.show() 
5/ Hide a few axis labels to avoid overlapping

With the xticklabels(x) you can keep one label every x labels:

sns.heatmap(df, xticklabels=4)
#sns.plt.show()

Leave a Reply

Your email address will not be published.