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

30 Jul, 2021
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 
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 used– Bestsellers
Example 1:
Python3
# Importing Required Librariesimport pandas as pdimport numpy as npimport seaborn as sbimport 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 Librariesimport pandas as pdimport numpy as npimport seaborn as sbimport matplotlib.pyplot as mtb data = pd.read_csv("bestsellers.csv") sb.heatmap(data.corr(), annot=True, annot_kws={'size': 25}) mtb.show()
Output:
simranarora5sos
Picked
Python-Seaborn
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Python Dictionary
Taking input in Python
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python