mosaic_heatmap
mosaic_heatmap(data, ...)
Plot mosaic data as a color-encoded matrix.
Creates a mosaic heatmap where the column widths and row heights are proportional to the marginal sums of the data matrix. This provides a visualization that encodes both the cell values through color and the marginal distributions through cell sizes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
array - like
|
2D dataset that can be coerced into an ndarray. If a pandas DataFrame is provided, the index/column information will be used to label the columns and rows. |
required |
vmin
|
float
|
Values to anchor the colormap. If not provided, they are inferred from the data and other keyword arguments. |
None
|
vmax
|
float
|
Values to anchor the colormap. If not provided, they are inferred from the data and other keyword arguments. |
None
|
cmap
|
str or Colormap
|
The mapping from data values to color space. If not provided, the
default depends on whether |
None
|
center
|
float
|
The value at which to center the colormap for divergent data.
Changes the default |
None
|
robust
|
bool
|
If True and |
False
|
annot
|
bool or array - like
|
If True, write the data value in each cell. If array-like with same shape
as |
None
|
fmt
|
str
|
String formatting code for annotation values. Default: '.2g' |
'.2g'
|
annot_kws
|
dict
|
Keyword arguments for matplotlib.axes.Axes.text when |
None
|
linewidths
|
float
|
Width of cell divider lines. Default: 0 |
0
|
linecolor
|
color
|
Color of cell divider lines. Default: 'white' |
'white'
|
cbar
|
bool
|
Whether to draw a colorbar. Default: True |
True
|
cbar_kws
|
dict
|
Keyword arguments for matplotlib.figure.Figure.colorbar. |
None
|
cbar_ax
|
Axes
|
Axes in which to draw the colorbar. If None, takes space from main Axes. |
None
|
square
|
bool
|
If True, set aspect ratio to "equal" for square cells. Default: False |
False
|
xticklabels
|
'auto', bool, array-like, or int
|
|
'auto'
|
yticklabels
|
'auto', bool, array-like, or int
|
|
'auto'
|
mask
|
bool array or DataFrame
|
If True in a cell, data is not shown. Missing values are auto-masked. |
None
|
ax
|
Axes
|
Axes in which to draw the plot. Uses current axes if None. |
None
|
**kwargs
|
dict
|
Additional keyword arguments passed to matplotlib.axes.Axes.pcolormesh. |
{}
|
Returns:
Type | Description |
---|---|
Axes
|
The Axes object with the heatmap. |
Examples:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from mheatmap import mosaic_heatmap
>>>
>>> # Generate sample confusion matrix data
>>> data = np.array([[10, 2, 0], [1, 8, 3], [0, 1, 12]])
>>>
>>> # Create mosaic heatmap with annotations
>>> fig, ax = plt.subplots(figsize=(8, 6))
>>> mosaic_heatmap(data, annot=True, cmap='YlOrRd', fmt='d',
... xticklabels=['A', 'B', 'C'],
... yticklabels=['A', 'B', 'C'])
>>> plt.title('Mosaic Confusion Matrix')
>>> plt.show()
Notes
The mosaic heatmap is particularly useful for confusion matrices and contingency tables where the marginal distributions provide additional context beyond the cell values themselves.
Source code in mheatmap/matrix.py
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|