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
- 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 OpenRetailScience 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, eyebrow=None, subtitle=None, ax=None, source_text=None, figsize=None, cbar_format='{x:g}', colormap_style='discrete', x_labels_position='bottom')
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
|
eyebrow |
str
|
Small uppercase label rendered above the title. Defaults to None. |
None
|
subtitle |
str
|
Supporting copy rendered below the title. Defaults to None. |
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 applied to in-cell text. In
|
'{x:g}'
|
colormap_style |
Literal['discrete', 'continuous']
|
Render the colorbar as a stepped 5-bin scale ("discrete", default — matches the design system) or a smooth gradient ("continuous"). Discrete bins lose precision but read more cleanly when cell values are annotated; continuous gives a finer-grained sense of magnitude. |
'discrete'
|
x_labels_position |
Literal['top', 'bottom']
|
Whether x-axis tick labels render above
or below the matrix. Cohort charts conventionally use |
'bottom'
|
Returns:
| Name | Type | Description |
|---|---|---|
SubplotBase |
SubplotBase
|
The matplotlib axes object. |
Source code in openretailscience/plots/heatmap.py
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |