How to annotate heatmap values in Seaborn (with Code)
The Short Answer (TL;DR)
Set annot=True to print values inside Seaborn heatmap cells, and use fmt to control their display precision. Formatting changes the labels while the colors continue to represent the original numeric values.
import seaborn as sns
ax = sns.heatmap([[0.1234, 0.9876], [0.4567, 0.7654]], annot=True, fmt=".2f", cmap="viridis")
ax.set(xlabel="Column", ylabel="Row", title="Values rounded to two decimals")
Detailed Implementation
This example uses synthetic data and does not need a network connection. Install the recorded package versions in a separate Python environment before running it.
python -m pip install numpy==2.5.4 pandas==3.0.6 matplotlib==3.11.2 seaborn==0.13.2
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
values = pd.DataFrame([[0.1234, 0.9876], [0.4567, 0.7654]],
index=["Group A", "Group B"], columns=["Metric 1", "Metric 2"])
fig, ax = plt.subplots(figsize=(5.5, 3.8), layout="constrained")
sns.heatmap(values, annot=True, fmt=".2f", cmap="viridis",
vmin=0, vmax=1, annot_kws={"fontsize": 12},
cbar_kws={"label": "Synthetic value"}, ax=ax)
ax.set_title("Original values; labels rounded to two decimals")
fig.savefig("annotated-heatmap.png", dpi=160)
plt.show()
The four labels read 0.12, 0.99, 0.46 and 0.77. The stored first value remains 0.1234.
How do number formats work?
Use .2f for two decimal places, .1f for one, and .0% for proportions displayed as percentages. A value of 0.25 becomes 25% with .0%; a value already expressed as 25 must not use that format unless 2500% is intended.
Can labels differ from color values?
Pass a rectangular array to annot and fmt="" for custom text labels. Its shape and order must match the numeric matrix. Seaborn aligns annotation arrays by position, even when both inputs are DataFrames, so reindex a label table explicitly before passing it.
What if labels are crowded?
Increase the figure size or lower annot_kws font size. Avoid treating a rounded label as an exact value, and keep units in the title or colorbar. Dense matrices may be clearer without a label in every cell.
Mathematical Context
For rounding to two decimal places, the rounding interval has width 0.01 and the absolute rounding error is at most approximately 0.005, apart from floating-point representation at ties. The unrounded array still determines the colors. Printed precision is not a confidence interval and does not describe measurement uncertainty.
\[ |x-\operatorname{round}(x,2)|\leq 0.005 \]
Reproducibility
Executed with Python 3.12.14; numpy 2.5.4, pandas 3.0.6, matplotlib 3.11.2, seaborn 0.13.2. Automated checks confirm the stated output properties.