HexbinGlyph Class#
The HexbinGlyph class bins an (x, y) point cloud onto a hexagonal lattice and
colours each cell by a per-bin aggregate — a count by default, or the
reduce of a per-point values array (mean, sum, min, max, std, or a callable).
It is the discrete counterpart of KDEGlyph: where the KDE smooths
the cloud into a continuous density with a bandwidth, hexbin answers "how many
observations fell here" (or "what is their mean/…") — a value you can read straight
off the colorbar, without the over-plotting an alpha scatter suffers from.
It wraps matplotlib.axes.Axes.hexbin and routes the per-bin aggregate through the
shared scalar-mapping pipeline, so vmin / vmax, color_scale, levels,
ticks_spacing, the ColorBar spec and classify=Classify(...) behave exactly as
for the other colour-mapped glyphs. The glyph is geometry- and CRS-agnostic: it
takes plain x / y arrays in the axes' own coordinates.
Class Documentation#
cleopatra.glyphs.stats.hexbin_glyph.HexbinGlyph
#
Visualization class for hexagonally-binned point density.
Wraps matplotlib.axes.Axes.hexbin. With no values, each hexagonal
bin is coloured by the count of points that fell in it; with a
per-point values array, each bin is coloured by the reduce
aggregate of those values (mean by default). The per-bin aggregate is
colour-mapped through the shared scalar-mapping pipeline and a matching
colorbar is attached, so vmin / vmax / color_scale / levels and
classify=Classify(...) apply as they do for the other glyphs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
1-D array of point x-coordinates. |
required |
y
|
ndarray
|
1-D array of point y-coordinates. Must match the length of |
required |
values
|
ndarray | None
|
Optional 1-D array of per-point values aggregated per bin
by |
None
|
ax
|
Axes | None
|
Pre-existing axes to draw on. Default is None. |
None
|
fig
|
Figure | None
|
Pre-existing figure. Default is None. |
None
|
**kwargs
|
Override any key in |
{}
|
Note
Empty bins are handled differently by matplotlib's hexbin in the two
modes, and with the default min_count=None this glyph passes that
through: the counts mode (values is None) draws every lattice
cell in the window, colouring empty ones 0 (so the colorbar starts at
0 and the whole window is tinted), whereas the reduce mode drops
empty cells. Pass min_count=1 on the counts mode to blank the empty
cells and match the reduce mode -- this is also required before
color=ColorScaling.log(), which cannot map the 0 of an empty count
cell.
Raises:
| Type | Description |
|---|---|
ValueError
|
At construction, if |
Examples:
- Read the per-bin counts back off the drawn collection:
- Colour bins by the mean of a per-point value (three coincident
points share one bin, so its value is their mean):
>>> import numpy as np >>> from cleopatra.glyphs.stats.hexbin_glyph import HexbinGlyph >>> x = np.array([0.0, 0.0, 0.0]) >>> y = np.array([0.0, 0.0, 0.0]) >>> values = np.array([2.0, 4.0, 6.0]) >>> glyph = HexbinGlyph(x, y, values, gridsize=2, reduce="mean") >>> fig, ax, pc = glyph.plot() >>> float(pc.get_array().max()) 4.0
See Also
cleopatra.glyphs.stats.kde_glyph.KDEGlyph: The continuous (smoothed-density) counterpart. cleopatra.glyphs.base.glyph.Glyph._prepare_scalar_mapping: Shared norm / colorbar / ticks pipeline used to colour the bins.
Source code in src/cleopatra/glyphs/stats/hexbin_glyph.py
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 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
evaluate()
#
Bin the points and return the result without rendering on self.ax.
Draws the hexbin onto a throwaway figure (so no global state or the
caller's axes are touched), then reads back the bin centres and the
per-bin aggregate. Mirrors KDEGlyph.evaluate, and because it uses
the same _hexbin_kwargs as plot, the returned aggregate matches
the array of the PolyCollection that plot draws.
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray, ndarray]
|
tuple[np.ndarray, np.ndarray, np.ndarray]: the bin-centre |
Examples:
- The aggregate has one value per bin centre:
- The per-bin counts total the number of points:
Source code in src/cleopatra/glyphs/stats/hexbin_glyph.py
plot(ax=None, title=None, add_colorbar=None, colorbar=None, color=None, contour=None, classify=None)
#
Draw the hexagonally-binned density and colour-map the aggregate.
The per-bin aggregate is resolved through _prepare_scalar_mapping,
so vmin / vmax / levels / color_scale and the ColorBar
spec behave as for the other glyphs, and classify=Classify(...)
bins the aggregate into discrete colour classes. A categorical
scheme is rejected (_SUPPORTS_CATEGORICAL_SCHEME is False): a
per-bin aggregate is a continuous magnitude, not a nominal class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes | None
|
Axes to draw on. Falls back to the axes supplied at construction, otherwise a new figure/axes is created. |
None
|
title
|
str | None
|
Plot title. Overrides |
None
|
add_colorbar
|
bool | None
|
Override the |
None
|
colorbar
|
bool | ColorBar | None
|
Typed |
None
|
color
|
ColorScaling | None
|
A |
None
|
contour
|
Contour | None
|
A |
None
|
classify
|
Classify | None
|
A |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Figure, Axes, PolyCollection]
|
tuple[Figure, Axes, PolyCollection]: the figure, the axes, and
the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the binning leaves no cells to draw (an |
Examples:
- A classified hexbin steps the colorbar and still returns the
collection:
>>> import numpy as np >>> from cleopatra.glyphs.stats.hexbin_glyph import HexbinGlyph >>> from cleopatra.styling.params import Classify >>> rng = np.random.default_rng(3) >>> x, y = rng.normal(size=600), rng.normal(size=600) >>> glyph = HexbinGlyph(x, y, gridsize=10) >>> fig, ax, pc = glyph.plot( ... classify=Classify(scheme="quantiles", k=4) ... ) >>> glyph.cbar is not None True - Suppress the colorbar for shared-axes composition:
Source code in src/cleopatra/glyphs/stats/hexbin_glyph.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
Examples#
Per-bin counts#
import numpy as np
from cleopatra.glyphs.stats.hexbin_glyph import HexbinGlyph
rng = np.random.default_rng(0)
x = rng.normal(0, 1, 5000)
y = rng.normal(0, 1, 5000)
fig, ax, pc = HexbinGlyph(x, y, gridsize=40).plot(title="Point density")
Empty bins: counts vs reduce
With the default min_count=None, the counts mode (no values) draws every lattice cell — empty ones
are coloured 0, so the colorbar starts at 0 and the whole window is tinted — whereas the reduce mode
drops empty cells. Pass min_count=1 on a counts plot to blank the empty cells (and it is required before
color=ColorScaling.log(), which cannot map an empty cell's 0).
Per-bin mean of a third variable, dropping sparse bins#
depth = x + y
glyph = HexbinGlyph(x, y, depth, reduce="mean", min_count=5)
fig, ax, pc = glyph.plot()
Classified and log-scaled density#
from cleopatra.styling.params import Classify
from cleopatra.styling.scaling import ColorScaling
# stepped colorbar by quantile classes
HexbinGlyph(x, y).plot(classify=Classify(scheme="quantiles", k=5))
# log-scaled counts (min_count=1 drops the empty bins that a log scale cannot map)
HexbinGlyph(x, y, min_count=1).plot(color=ColorScaling.log())