Open In App

How to increase the size of the annotations of a seaborn heatmap in Python?

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Seaborn

Seaborn is a Python library that is based on matplotlib and is used for data visualization. It provides a medium to present data in a statistical graph format as an informative and attractive medium to impart some information. A heatmap is one of the components supported by seaborn where variation in related data is portrayed using a color palette. 

Annotations are text that appears on a heatmap cell which represents what that cell represents. The annotations follow a default font size but it can be changed using the annot_kws parameter of heatmap() function, annot_kws is a dictionary type parameter that accepts value for the key named size. The value set to this key defines the size of the annotations, but there are certain conditions that has to met to increase the size of the annotations:

  • annot parameter of the heatmap() function must be set to True.
  • annot_kws parameter must be set with required size.

Syntax: seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, **kwargs)

Important Parameters:

  • data: 2D dataset that can be coerced into an ndarray.
  • vmin, vmax: Values to anchor the colormap, otherwise they are inferred from the data and other keyword arguments.
  • cmap: The mapping from data values to color space.
  • center: The value at which to center the colormap when plotting divergent data.
  • annot: If True, write the data value in each cell.
  • fmt: String formatting code to use when adding annotations.
  • linewidths: Width of the lines that will divide each cell.
  • linecolor: Color of the lines that will divide each cell.
  • cbar: Whether to draw a colorbar.

All the parameters except data are optional.

Returns: An object of type matplotlib.axes._subplots.AxesSubplot 

Approach

  • Import module
  • Create or load data
  • Call heatmap() function with annot set to True.
  • Set size to annot_kws parameter.
  • Display plot

Implementation using this approach is given below:

Dataset usedBestsellers

Example 1:

Python3




# Importing Required Libraries
import pandas as pd
import numpy as np
import seaborn as sb
import matplotlib.pyplot as mtb
 
data = pd.read_csv("bestsellers.csv")
 
sb.heatmap(data.corr(), annot=True, annot_kws={'size': 15})
 
mtb.show()


Output:

For setting the size, one must be careful while setting size value. Providing a very large number will magnify annotations way too much making them difficult to read, understand, they may even fall over each other making the heatmap unreadable.

Implementation of this is shown below:

Example 2:

Python3




# Importing Required Libraries
import pandas as pd
import numpy as np
import seaborn as sb
import matplotlib.pyplot as mtb
 
data = pd.read_csv("bestsellers.csv")
 
sb.heatmap(data.corr(), annot=True, annot_kws={'size': 25})
 
mtb.show()


Output:



Last Updated : 30 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads