Heatmap Plot Gallery¶
The heatmap plot is used for visualizing 2D data structures as color-coded matrices with automatic cell value annotations. It's ideal for displaying relationships, patterns, and distributions across two categorical dimensions.
Heatmap plots excel at:
- Correlation Analysis: Visualize relationships between variables or metrics
- Migration Matrices: Track customer movement between segments over time
- Performance Grids: Compare metrics across categories, regions, or time periods
- Confusion Matrices: Display classification results and model performance
In [ ]:
Copied!
import matplotlib.pyplot as plt
import pandas as pd
from pyretailscience.plots import heatmap
import matplotlib.pyplot as plt
import pandas as pd
from pyretailscience.plots import heatmap
Basic Heatmap¶
Create a simple heatmap from a DataFrame using default formatting. The DataFrame index becomes the y-axis and columns become the x-axis.
In [ ]:
Copied!
# Create sample data for basic heatmap demonstration
# fmt: off
sample_data = pd.DataFrame(
{
"Q1": [125.5, 98.3, 87.2, 134.1],
"Q2": [142.8, 108.7, 95.4, 156.3],
"Q3": [138.2, 112.5, 89.6, 148.9],
"Q4": [165.7, 125.4, 102.3, 178.2],
},
index=["North", "South", "West", "East"],
)
# fmt: on
ax = heatmap.plot(
sample_data,
cbar_label="Values",
title="Basic Heatmap with Default Formatting",
x_label="Columns",
y_label="Rows",
)
plt.show()
# Create sample data for basic heatmap demonstration
# fmt: off
sample_data = pd.DataFrame(
{
"Q1": [125.5, 98.3, 87.2, 134.1],
"Q2": [142.8, 108.7, 95.4, 156.3],
"Q3": [138.2, 112.5, 89.6, 148.9],
"Q4": [165.7, 125.4, 102.3, 178.2],
},
index=["North", "South", "West", "East"],
)
# fmt: on
ax = heatmap.plot(
sample_data,
cbar_label="Values",
title="Basic Heatmap with Default Formatting",
x_label="Columns",
y_label="Rows",
)
plt.show()
Percentage Formatting¶
Customize the colorbar display format using the cbar_format parameter with percentage formatting.
In [ ]:
Copied!
# Create sample data with percentage values
# fmt: off
percentage_data = pd.DataFrame(
{
"Week 1": [0.0245, 0.0189, 0.0312, 0.0198],
"Week 2": [0.0278, 0.0201, 0.0334, 0.0215],
"Week 3": [0.0256, 0.0195, 0.0298, 0.0187],
"Week 4": [0.0289, 0.0223, 0.0356, 0.0231],
},
index=["Email Campaign", "Social Media", "Google Ads", "Display Ads"],
)
# fmt: on
ax = heatmap.plot(
percentage_data,
cbar_label="Rate",
cbar_format="{x:.1%}", # Format as percentage with 1 decimal place
title="Heatmap with Percentage Formatting",
x_label="Time Period",
y_label="Categories",
)
plt.show()
# Create sample data with percentage values
# fmt: off
percentage_data = pd.DataFrame(
{
"Week 1": [0.0245, 0.0189, 0.0312, 0.0198],
"Week 2": [0.0278, 0.0201, 0.0334, 0.0215],
"Week 3": [0.0256, 0.0195, 0.0298, 0.0187],
"Week 4": [0.0289, 0.0223, 0.0356, 0.0231],
},
index=["Email Campaign", "Social Media", "Google Ads", "Display Ads"],
)
# fmt: on
ax = heatmap.plot(
percentage_data,
cbar_label="Rate",
cbar_format="{x:.1%}", # Format as percentage with 1 decimal place
title="Heatmap with Percentage Formatting",
x_label="Time Period",
y_label="Categories",
)
plt.show()