• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use plotters::prelude::*;
2 
3 const OUT_FILE_NAME: &str = "plotters-doc-data/matshow.png";
main() -> Result<(), Box<dyn std::error::Error>>4 fn main() -> Result<(), Box<dyn std::error::Error>> {
5     let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area();
6 
7     root.fill(&WHITE)?;
8 
9     let mut chart = ChartBuilder::on(&root)
10         .caption("Matshow Example", ("sans-serif", 80))
11         .margin(5)
12         .top_x_label_area_size(40)
13         .y_label_area_size(40)
14         .build_cartesian_2d(0i32..15i32, 0i32..15i32)?;
15 
16     chart
17         .configure_mesh()
18         .x_labels(15)
19         .y_labels(15)
20         .max_light_lines(4)
21         .x_label_offset(35)
22         .y_label_offset(25)
23         .disable_x_mesh()
24         .disable_y_mesh()
25         .label_style(("sans-serif", 20))
26         .draw()?;
27 
28     let mut matrix = [[0; 15]; 15];
29 
30     for i in 0..15 {
31         matrix[i][i] = i + 4;
32     }
33 
34     chart.draw_series(
35         matrix
36             .iter()
37             .zip(0..)
38             .flat_map(|(l, y)| l.iter().zip(0..).map(move |(v, x)| (x, y, v)))
39             .map(|(x, y, v)| {
40                 Rectangle::new(
41                     [(x, y), (x + 1, y + 1)],
42                     HSLColor(
43                         240.0 / 360.0 - 240.0 / 360.0 * (*v as f64 / 20.0),
44                         0.7,
45                         0.1 + 0.4 * *v as f64 / 20.0,
46                     )
47                     .filled(),
48                 )
49             }),
50     )?;
51 
52     // To avoid the IO failure being ignored silently, we manually call the present function
53     root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir");
54     println!("Result has been saved to {}", OUT_FILE_NAME);
55 
56     Ok(())
57 }
58 #[test]
entry_point()59 fn entry_point() {
60     main().unwrap()
61 }
62