How to add text in a heatmap cell annotations using seaborn in Python?


Heatmaps are useful for identifying patterns and trends in data and can be further customized by adding annotations to the cells, such as text labels or numerical values, which can provide additional information about the data. In this article, we will discuss how to add text in heatmap cell annotations using Seaborn in Python. We will explore different methods and options available in Seaborn to customize the text annotations, such as changing the font size, color, and format of the text.

Heatmap

A heat map (or heatmap) is a kind of data visualisation in which the intensity of a phenomena is represented by different colours on a two-dimensional graph. Colors may vary in tone or saturation to show the reader where and how a phenomenon clusters or changes across time and place. There are two main categories of heat maps: cluster heat maps and spatial heat maps.

Cluster heat maps organise magnitudes in a fixed-size cell matrix where phenomena and categories are shown as rows and columns. Clusters are intended to be suggested or shown as identified by statistical research, hence the ordering of rows and columns is deliberate and somewhat random. The cell's dimensions are completely random, yet large enough to be legible. There is no idea of cells in a spatial heat map, and the phenomena is seen as constantly variable, therefore the position of a magnitude is dictated by its location in that space.

Seaborn

Seaborn is a Python package for making charts and graphs from data. It uses matplotlib as its foundation and works well with the pandas library.

Seaborn aids in data discovery, visualization, and comprehension. Dataframes and arrays containing full datasets are processed and visualized by using plotting methods, which then do the necessary semantic mapping and statistical aggregation in order to produce insightful graphs. With its declarative, dataset-centric API, you can focus on the meaning of your graph's components rather than the technicalities of rendering them.

Syntax of a heatmap using seaborn

sns.heatmap(dt, *, vmin=None, vmax=None, cmap=None, center=None, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, **kwargs)

Explanation

S.No

Attributes

Definition

1.

dt

It is used to convert a 2D dataset into a ndarray. If a DataFrame using pandas is supplied, the column data can be used to designate rows and columns.

2.

vamx and vmin

They provide values to use as a starting point for the colormap. If not given, these values are inferred from the data and other keyword arguments.

3.

cmap

It defines the way that data values are mapped to a color space. If you don't give a default value, it will depend on whether the center is set or not.

4.

center

When plotting different sets of data, this is the value at which to center the colormap. If no other parameter is given, this one will change the default cmap.

5.

annot

If the value of annot parameter is true then write the data in each heatmap cell.

6.

annot_kws

It defines the arguments of the function matplotlib.axes.Axes.text() only when the value of the annot parameter is true.

7.

linewidhts

This parameter defines that what will be the width of the particular line that will divide each cell.

8.

linecolor

This parameter defines that what will be the color of the particular line that will divide each cell.

9.

cbar

It defines whether we need to draw a colorbar or not.

Adding text in a heatmap cell annotations

The rows and columns of a heatmap may be annotated to provide extra context. The annot option is often set to True to display data values on top of a heatmap.

Annot and fmt parameter

  • Annot − The sns.heatmap() annot (annotation) feature allows you to show the numerical value associated with each cell in a python seaborn heatmap. We may show the original number of a selected cell or communicate different numbers per your instructions. Passing True to annot causes the value to be shown in each heatmap cell.

  • Fmt − The annot parameter only allows the addition of numeric values to a python heatmap cell, whereas the fmt parameter permits the addition of string (text) values.

Here, a 2D numpy array containing string values is created and passed to annot. Additionally, the string value "s" is passed to fmt.

Example

import numpy as n
import matplotlib.pyplot as p
import seaborn as s

# creating random data using numpy
df = n.array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]])

# creating text array using numpy
tx = n.array([['Amy', 'Bryn', 'Calis', 'Daisy', 'Eagel'], ['Fin', 'Garry', 'Hary', 'Ingleis', 'Jack'],
   ['Kim', 'Lasy', 'Mia', 'Nia', 'Olivia']])

# creating subplot
figure, axx = p.subplots()
 
# defining heatmap on current axes using seaborn 
axx = s.heatmap(df, annot=tx, fmt="")

Output

Conclusion

In this article, we learned that Heatmaps show us the magnitude of a phenomenon in a 2-dimensional graph and can be used for data visualization. We have seen how Seaborn which is a library of Python is used to define a heatmap, its syntax and what are the parameters that define a heatmap. Finally, we have seen how to declare the attribute annot and fmt using which we can add text in a heatmap cell annotations.

Updated on: 31-May-2023

976 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements