(x, y) into a rectangular grid and colors each cell by its count. The colorbar (labeled “Count”) is added to the right margin automatically.
Constructor
new()
Create a 2D histogram with default settings.
Returns: Histogram2D
Defaults:
- 10×10 bins
- Viridis colormap
- No correlation annotation
Data Loading
with_data()
Load scatter points and bin them into a grid.
(x, y) pairs; any type implementing Into<f64>Axis extents
(min, max). Points outside these bounds are silently discarded. Start at 0.0 to keep bin-index and layout coordinates aligned.Axis extents
(min, max) for the y-axisNumber of columns in the grid
Number of rows in the grid
Self
Styling
with_color_map()
Set the colormap for bin counts.
Colormap applied to normalized bin counts. Options:
ColorMap::Viridis (default), ColorMap::Inferno, ColorMap::Grayscale, or ColorMap::Custom(Arc<Fn>)Self
Default: ColorMap::Viridis
Annotations
with_correlation()
Overlay the Pearson correlation coefficient in the top-right corner.
Returns: Self
The coefficient is computed from all points passed to with_data, including those clipped outside the plot range. Displayed as r = 0.85.
ColorMap
Variants
Viridis
Perceptually uniform, blue → green → yellow. Colorblind-safe. (default)
Inferno
Dark purple → orange → bright yellow. High contrast.
Grayscale
White (low) → black (high). Print-friendly.
Custom(Arc<dyn Fn(f64) -> String>)
User-supplied function f64 → String. The function receives a normalized value in [0.0, 1.0].
Complete Example
Public Fields
Raw scatter points used for correlation computation
Pre-computed bin counts indexed as
bins[row][col]Physical x-axis range
(min, max) used for binningPhysical y-axis range
(min, max) used for binningNumber of bins along the x-axis. Default:
10Number of bins along the y-axis. Default:
10Colormap applied to normalized bin counts. Default:
ColorMap::ViridisWhen
true, the Pearson r coefficient is printed in the top-right corner