Plotting a 2D map with colors for a 3rd column of values using rust

Viewed 197

I have an vector Vec<(i32,i32,i32)> and would like to create a 2D rainbow or gradient map. The third element of the vector would be the value for the color. I am using the plotters library. A colormap as in the image example. Any ideas or code examples?

Expected outcome

I implemented the code below and got a result very close to what I want. I have no idea how to resolve the issue of including the colorbar, as a color reference, and how to smooth the field.

    fn figura2(profun: Vec<(i32, i32, i32)>) -> Result<(), Box<dyn std::error::Error>> {
    let root = BitMapBackend::new("plot_fold.png", (800, 600)).into_drawing_area();
    root.fill(&WHITE)?;

    let mut scatter_ctx = ChartBuilder::on(&root)
        .top_x_label_area_size(40)
        .y_label_area_size(55)
        .build_cartesian_2d((1581001..1581542).step(1), 5500i32..0i32)?;
    scatter_ctx
        .configure_mesh()
        .disable_x_mesh()
        .disable_y_mesh()
        .y_desc("OFFSET (m)")
        .x_desc("ESTACAS (RP)")
        .axis_desc_style(("sans-serif", 15))
        .draw()?;
    scatter_ctx.draw_series(profun.iter().map(|(x, y, v)| {
        Rectangle::new(
            [(*x, *y), (x + 1, y - 500)],
            HSLColor(
                240.0 / 360.0 - 240.0 / 360.0 * (*v as f64 / 150.0),
                0.7,
                0.1 + 0.4 * *v as f64 / 200.0,
            )
            .filled(),
        )
    }))?;

    Ok(())
}

Result of the above code

1 Answers

I got a smoother result, on the Y axis, reducing the interaction range from 500 to 100. And better color matching using the colorous library.

Result

What has changed in the code.

scatter_ctx.draw_series(profun.iter().map(|(x, y, v)| {
        Rectangle::new(
            [(*x, *y), (x + 1, y - 100)],
            {
                let cor = gradient.eval_rational(*v as usize, nfold as usize);
                RGBColor(
                    cor.r,
                    cor.g,
                    cor.b,
                )
                .filled()
            },
        )
    }))?;

The issue of the color bar as a reference for the values assigned to each color in the field remains to be resolved.

Related