- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to annotate a heatmap with text in Matplotlib?
To annotate a heatmap with text in matplotlib, we can take the following steps −
Create random data with 4×4 dimension array.
Create a pseudocolor plot with a non-regular rectangular grid, using pcolor() method.
To put text in the pixels, we can use text() method.
To display the figure, use show() method.
Example
import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(4, 4) heatmap = plt.pcolor(data, cmap="PuBuGn_r") for y in range(data.shape[0]): for x in range(data.shape[1]): plt.text(x + 0.5, y + 0.5, '%.4f' % data[y, x], horizontalalignment='center', verticalalignment='center', ) plt.show()
Output
- Related Articles
- How to annotate several points with one text in Matplotlib?
- How to annotate each cell of a heatmap in Seaborn?
- Creating a Heatmap in matplotlib with pcolor
- How To Annotate Bars in Bar Plot with Matplotlib in Python?
- How to Annotate Matplotlib Scatter Plots?
- Annotate Subplots in a Figure with A, B, C using Matplotlib
- How to annotate a range of the X-axis in Matplotlib?
- How to annotate the points on a scatter plot with automatically placed arrows in Matplotlib?
- How to make a heatmap square in Seaborn FacetGrid using Matplotlib?
- How to animate a Seaborn heatmap or correlation matrix(Matplotlib)?
- Annotate Time Series plot in Matplotlib
- How to annotate the end of lines using Python and Matplotlib?
- How to refresh text in Matplotlib?
- How to animate text in Matplotlib?
- Plotting only the upper/lower triangle of a heatmap in Matplotlib

Advertisements