plotters-statistical 0.2.0

Statistical chart primitives (box, violin, ROC, PR, regularization-path, residual) as native plotters series
Documentation
//! Correlation heatmap figure rendered to `correlation_heatmap.svg`.

use plotters::prelude::*;
use plotters_statistical::stats::CorrelationMethod;
use plotters_statistical::CorrelationHeatmap;

fn noise(i: usize, salt: usize) -> f64 {
    (((i * 31 + salt) as f64 * 12.9898).sin() * 43758.5453).rem_euclid(1.0) - 0.5
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let root = SVGBackend::new("correlation_heatmap.svg", (640, 560)).into_drawing_area();
    root.fill(&WHITE)?;

    // Five variables with designed relationships.
    let n = 200;
    let mut age = Vec::new();
    let mut income = Vec::new();
    let mut spend = Vec::new();
    let mut savings = Vec::new();
    let mut random = Vec::new();
    for i in 0..n {
        let a = i as f64 / n as f64;
        age.push(a + 0.1 * noise(i, 1));
        income.push(2.0 * a + 0.3 * noise(i, 2)); // strongly correlated with age
        spend.push(1.5 * a + 0.6 * noise(i, 3)); // moderately correlated
        savings.push(-a + 0.4 * noise(i, 4)); // negatively correlated
        random.push(noise(i, 5)); // uncorrelated
    }

    let columns = vec![age, income, spend, savings, random];
    let labels = ["age", "income", "spend", "savings", "random"]
        .iter()
        .map(|s| s.to_string())
        .collect();

    CorrelationHeatmap::from_columns(&columns, labels, CorrelationMethod::Pearson)?
        .title("Pearson correlation")
        .precision(2)
        .draw(&root)?;

    root.present()?;
    println!("wrote correlation_heatmap.svg");
    Ok(())
}