Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to understand Seaborn's heatmap annotation format?
Seaborn's heatmap annotation format controls how numeric values are displayed on each cell of the heatmap. The fmt parameter accepts Python string formatting codes to customize the appearance of annotations.
Basic Heatmap with Default Annotations
Let's start with a simple heatmap to see default annotation behavior ?
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
data = pd.DataFrame(np.random.random((4, 4)),
columns=['A', 'B', 'C', 'D'])
# Basic heatmap with annotations
plt.figure(figsize=(8, 6))
sns.heatmap(data, annot=True)
plt.title('Default Annotation Format')
plt.show()
Common Annotation Formats
The fmt parameter controls how numbers are displayed. Here are the most commonly used formats ?
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
data = pd.DataFrame([[0.1234, 0.5678], [0.9012, 0.3456]],
columns=['X', 'Y'], index=['Row1', 'Row2'])
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
# Percentage format
sns.heatmap(data, annot=True, fmt='.2%', ax=axes[0,0])
axes[0,0].set_title('Percentage Format (fmt=".2%")')
# Two decimal places
sns.heatmap(data, annot=True, fmt='.2f', ax=axes[0,1])
axes[0,1].set_title('Two Decimals (fmt=".2f")')
# Integer format
data_int = data * 100
sns.heatmap(data_int, annot=True, fmt='d', ax=axes[1,0])
axes[1,0].set_title('Integer Format (fmt="d")')
# Scientific notation
sns.heatmap(data, annot=True, fmt='.2e', ax=axes[1,1])
axes[1,1].set_title('Scientific Notation (fmt=".2e")')
plt.tight_layout()
plt.show()
Customizing Annotation Appearance
Use annot_kws to customize font size, color, and other properties ?
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create correlation matrix
data = pd.DataFrame(np.random.randn(5, 5),
columns=['Feature1', 'Feature2', 'Feature3', 'Feature4', 'Feature5'])
correlation_matrix = data.corr()
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix,
annot=True,
fmt='.2f',
annot_kws={'size': 12, 'weight': 'bold', 'color': 'white'},
cmap='coolwarm',
center=0)
plt.title('Correlation Matrix with Custom Annotations')
plt.show()
Format Code Reference
| Format Code | Description | Example Input | Example Output |
|---|---|---|---|
.2f |
2 decimal places | 0.12345 | 0.12 |
.2% |
Percentage with 2 decimals | 0.12345 | 12.35% |
d |
Integer | 12.78 | 13 |
.2e |
Scientific notation | 0.00123 | 1.23e-03 |
g |
General format | 1234.5 | 1234.5 |
Practical Example: Sales Data Heatmap
Here's a real-world example showing sales performance across regions and months ?
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create sales data
np.random.seed(42)
sales_data = pd.DataFrame(
np.random.randint(50000, 200000, size=(4, 3)),
columns=['Q1', 'Q2', 'Q3'],
index=['North', 'South', 'East', 'West']
)
plt.figure(figsize=(8, 6))
sns.heatmap(sales_data,
annot=True,
fmt='d', # Integer format for currency
annot_kws={'size': 14},
cmap='YlOrRd',
linewidths=0.5)
plt.title('Quarterly Sales by Region ($)')
plt.show()
Conclusion
Seaborn's annotation format is controlled by the fmt parameter using Python string formatting codes. Use .2% for percentages, .2f for decimals, and annot_kws to customize appearance. Choose the format that best represents your data type and audience needs.
