How to increase the size of the annotations of a seaborn heatmap in Python?
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)
- 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
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:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course