Visualize.Contour.Density (Visualize v0.2.25)

Copy Markdown View Source

Kernel density estimation for contour generation from point data.

Estimates a continuous density surface from scattered points using Gaussian kernel density estimation, then generates contours from the resulting grid.

Examples

# Generate density contours from points
points = [
  %{x: 10, y: 20},
  %{x: 15, y: 25},
  %{x: 12, y: 22},
  # ... more points
]

density = Visualize.Contour.Density.new()
  |> Visualize.Contour.Density.x(fn d -> d.x end)
  |> Visualize.Contour.Density.y(fn d -> d.y end)
  |> Visualize.Contour.Density.size(100, 100)
  |> Visualize.Contour.Density.bandwidth(10)
  |> Visualize.Contour.Density.thresholds(10)

contours = Visualize.Contour.Density.compute(density, points)

Algorithm

Uses Gaussian kernel density estimation:

  • For each grid cell, sum the weighted contributions from all points
  • Weight is based on distance using Gaussian kernel: K(u) = exp(-u²/2)
  • Bandwidth controls the smoothness (larger = smoother)

Summary

Functions

Sets the bandwidth (kernel size) for density estimation.

Sets the cell size for the density grid

Computes density contours from point data.

Creates a new density estimator

Renders density contours as SVG paths.

Sets the output dimensions

Sets the threshold values for contour generation.

Sets the weight accessor function

Sets the x accessor function

Sets the y accessor function

Types

t()

@type t() :: %Visualize.Contour.Density{
  bandwidth: number(),
  cell_size: pos_integer(),
  height: pos_integer(),
  thresholds: [number()] | pos_integer(),
  weight: (any() -> number()) | nil,
  width: pos_integer(),
  x: (any() -> number()) | nil,
  y: (any() -> number()) | nil
}

Functions

bandwidth(density, bw)

@spec bandwidth(t(), number()) :: t()

Sets the bandwidth (kernel size) for density estimation.

Larger values produce smoother results but may obscure detail.

cell_size(density, size)

@spec cell_size(t(), pos_integer()) :: t()

Sets the cell size for the density grid

compute(density, points)

Computes density contours from point data.

Returns a list of contour objects compatible with Visualize.Contour.

new()

@spec new() :: t()

Creates a new density estimator

render(density, points)

@spec render(t(), [any()]) :: [%{value: number(), path: String.t()}]

Renders density contours as SVG paths.

size(density, width, height)

@spec size(t(), pos_integer(), pos_integer()) :: t()

Sets the output dimensions

thresholds(density, values)

@spec thresholds(t(), [number()] | pos_integer()) :: t()

Sets the threshold values for contour generation.

Can be a list of specific values or a count (generates that many levels).

weight(density, accessor)

@spec weight(t(), (any() -> number())) :: t()

Sets the weight accessor function

x(density, accessor)

@spec x(t(), (any() -> number())) :: t()

Sets the x accessor function

y(density, accessor)

@spec y(t(), (any() -> number())) :: t()

Sets the y accessor function