1 use plotters::prelude::*;
2
main() -> Result<(), Box<dyn std::error::Error>>3 fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let root = BitMapBackend::new("plotters-doc-data/matshow.png", (1024, 768)).into_drawing_area();
5
6 root.fill(&WHITE)?;
7
8 let mut chart = ChartBuilder::on(&root)
9 .caption("Matshow Example", ("sans-serif", 80))
10 .margin(5)
11 .top_x_label_area_size(40)
12 .y_label_area_size(40)
13 .build_cartesian_2d(0i32..15i32, 15i32..0i32)?;
14
15 chart
16 .configure_mesh()
17 .x_labels(15)
18 .y_labels(15)
19 .x_label_offset(35)
20 .y_label_offset(25)
21 .disable_x_mesh()
22 .disable_y_mesh()
23 .label_style(("sans-serif", 20))
24 .draw()?;
25
26 let mut matrix = [[0; 15]; 15];
27
28 for i in 0..15 {
29 matrix[i][i] = i + 4;
30 }
31
32 chart.draw_series(
33 matrix
34 .iter()
35 .zip(0..)
36 .map(|(l, y)| l.iter().zip(0..).map(move |(v, x)| (x as i32, y as i32, v)))
37 .flatten()
38 .map(|(x, y, v)| {
39 Rectangle::new(
40 [(x, y), (x + 1, y + 1)],
41 HSLColor(
42 240.0 / 360.0 - 240.0 / 360.0 * (*v as f64 / 20.0),
43 0.7,
44 0.1 + 0.4 * *v as f64 / 20.0,
45 )
46 .filled(),
47 )
48 }),
49 )?;
50
51 Ok(())
52 }
53 #[test]
entry_point()54 fn entry_point() {
55 main().unwrap()
56 }
57