Heatmap Plot
This module provides functionality for creating generic heatmap plots from pandas DataFrames.
This module is designed to create flexible heatmap visualizations suitable for various use cases including migration matrices, confusion matrices, correlation matrices, and other 2D data visualizations. It provides a clean, reusable interface without domain-specific assumptions.
Core Features
- Generic Design: No domain-specific assumptions or hardcoded elements
- Color Mapping: Uses Tailwind green colormap for consistent visualization
- Auto-contrast Text: Text color automatically switches between black and white based on cell intensity
- Customizable Labels: Supports custom labels for x-axis, y-axis, title, and colorbar
- Grid Styling: White grid lines between cells for clear separation
- Flexible Data: Displays values as-is without formatting assumptions
Use Cases
- Migration Matrices: Visualize customer movement between segments
- Correlation Matrices: Show relationships between variables
- Confusion Matrices: Display classification results
- Any 2D Data: Generic support for any tabular data visualization
Design Principles
- Display values as-is from the DataFrame (no percentage or other formatting assumptions)
- Consistent with existing PyRetailScience plotting modules (line.py, bar.py)
- Minimal parameters with **kwargs for advanced customization
- Match visual style of existing plots while remaining generic
plot(df, cbar_label, x_label=None, y_label=None, title=None, ax=None, source_text=None, figsize=None, cbar_format='{x:.2f}', **kwargs)
Creates a generic heatmap visualization from a pandas DataFrame.
This function creates a color-coded heatmap with cell values displayed as text. It is suitable for visualizing any 2D data structure including migration matrices, confusion matrices, correlation matrices, or cohort analysis data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
DataFrame to visualize. Index becomes y-axis, columns become x-axis. |
required |
cbar_label |
str
|
Label for the colorbar. |
required |
x_label |
str
|
Label for x-axis. |
None
|
y_label |
str
|
Label for y-axis. |
None
|
title |
str
|
Title of the plot. |
None
|
ax |
Axes
|
Matplotlib axes object to plot on. |
None
|
source_text |
str
|
Additional source text annotation. |
None
|
figsize |
tuple[int, int]
|
The size of the plot. Defaults to None. |
None
|
cbar_format |
str
|
Format string for colorbar values. Defaults to "{x:.2f}". |
'{x:.2f}'
|
**kwargs |
dict[str, any]
|
Additional keyword arguments passed to matplotlib's imshow function. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
SubplotBase |
SubplotBase
|
The matplotlib axes object. |
Source code in pyretailscience/plots/heatmap.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |