stat-bin-2d

Two-dimensional bin statistic: partition (x, y) into a rectangular grid and count rows per cell.

bins and binwidth accept either a scalar (applied to both axes) or an (x, y) pair. binwidth wins on each axis where both are set.

Usage

stat-bin-2d(
  bins: 30,
  binwidth: none,
)

Parameters

Parameter Default Description
bins 30 Scalar or (x, y) pair — target bin counts when binwidth is none.
binwidth none Scalar or (x, y) pair — fixed bin widths. Overrides bins per axis.

Returns

Statistic object with name: "bin_2d".

Outputs

  • x.
  • y.
  • _count.
  • _density.
  • xmin.
  • xmax.
  • ymin.
  • ymax.

Examples

Drive geom-rect with the constructor form to bin a noisy scatter into a 20-by-20 grid coloured by count.

#let d = range(0, 400).map(i => (
  x: calc.sin(i * 0.13) * 4,
  y: calc.cos(i * 0.21) * 4,
))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-rect(stat: stat-bin-2d(bins: 20)),),
  scales: (scale-fill-viridis-c(),),
  width: 10cm,
  height: 6cm,
)

Two-dimensional bin grid of sine/cosine samples with 20-by-20 rectangular cells shaded by count via stat-bin-2d and the viridis fill palette.

Two-dimensional bin grid of sine/cosine samples with 20-by-20 rectangular cells shaded by count via stat-bin-2d and the viridis fill palette.

See also

geom-bin-2d, stat-bin.

Back to top