Skip to main content

plotters_statistical/figures/
mod.rs

1//! Figure-level builders: charts that own their own axes / multi-panel layout
2//! and render onto a whole `DrawingArea`, rather than plugging into a single
3//! `draw_series` call.
4//!
5//! * [`CorrelationHeatmap`] — labeled correlation matrix + colorbar.
6//! * [`MissingnessHeatmap`] — data-quality present/absent map for EDA.
7//! * [`PairPlot`] — scatterplot matrix with distribution diagonals.
8
9pub mod colorbar;
10pub mod correlation_heatmap;
11pub mod missingness_heatmap;
12pub mod pair_plot;
13
14pub use correlation_heatmap::CorrelationHeatmap;
15pub use missingness_heatmap::MissingnessHeatmap;
16pub use pair_plot::{Diagonal, PairPlot};
17
18use plotters::prelude::*;
19use plotters::style::text_anchor::{HPos, Pos, VPos};
20
21/// Build an anchored `sans-serif` [`TextStyle`]. The color is borrowed, so pass
22/// a `&color` that lives at least as long as the returned style is used (a
23/// temporary in the same `draw(...)` statement is fine).
24pub(crate) fn text_style<'a>(h: HPos, v: VPos, size: i32, color: &'a RGBColor) -> TextStyle<'a> {
25    TextStyle::from(("sans-serif", size).into_font())
26        .color(color)
27        .pos(Pos::new(h, v))
28}
29
30/// Pick black or white for legible text over a background color.
31pub(crate) fn contrast_text(bg: RGBColor) -> RGBColor {
32    let l = 0.299 * bg.0 as f64 + 0.587 * bg.1 as f64 + 0.114 * bg.2 as f64;
33    if l > 140.0 {
34        RGBColor(0, 0, 0)
35    } else {
36        RGBColor(255, 255, 255)
37    }
38}