frontpage_example/
frontpage_example.rs

1//! Example shown on the docs frontpage and on the github readme
2//!
3
4extern crate ndarray;
5extern crate rand;
6extern crate astrup;
7
8use std::f64::consts::PI;
9
10use ndarray::Array;
11use rand::distributions::{IndependentSample, Normal};
12use rand::{thread_rng};
13
14use astrup::view::View;
15use astrup::figure::Figure;
16use astrup::plot::Plot;
17use astrup::chart::Chart;
18use astrup::chart::scatter::Scatter;
19use astrup::chart::line::Line;
20
21fn main() {
22
23    // Create data contained in ndarray
24    let x_data = Array::from_iter((0..100).map(|x| (x as f64) * 2.0 * PI / 100.0));
25    let y_data1 = Array::from_iter((0..100).map(|i| x_data[i].sin()));
26    let y_data2 = Array::from_iter((0..100).map(|i| (x_data[i] - PI / 2.0).sin()));
27
28    // Plot lines
29    let line1 = Line::new(&x_data, &y_data1).set_stroke_style("dotted");
30    let line2 = Line::new(&x_data, &y_data2).set_color_rgba(0.9, 0.2, 0.2, 0.9);
31
32    // Add lines to a plot
33    let line_plot = Plot::new().add(Chart::Line(line1))
34                               .add(Chart::Line(line2))
35                               .set_y_min(-1.2)
36                               .set_local_frame(0.0, 0.7, 0.51, 1.0);
37
38    // Create scatter points
39    let normal_0_1 = Normal::new(0.0, 1.0);
40    let normal_0_2 = Normal::new(0.0, 2.0);
41    let x_data: Vec<f64> = (0..1000)
42                           .map(|_| normal_0_1.ind_sample(&mut thread_rng()))
43                           .collect();
44    let y_data: Vec<f64> = (0..1000)
45                           .map(|_| normal_0_2.ind_sample(&mut thread_rng()))
46                           .collect();
47    let scatter = Scatter::new(&x_data, &y_data).set_color_rgba(0.1, 0.8, 0.3, 0.9)
48                                                .set_point_size(0.005);
49
50    // Add scatter points to a new plot
51    let scatter_plot = Plot::new().set_local_frame(0.3, 1.0, 0.0, 0.49)
52                                  .add(Chart::Scatter(scatter));
53
54    // Add the plots to a figure, and save it
55    let fig = Figure::new().add(line_plot)
56                           .add(scatter_plot)
57                           .set_width(1000)
58                           .set_height(800)
59                           .set_border_thickness(0.001)
60                           .save("assets/frontpage_example.png").expect("Could not save frontpage_example.png")
61                           .save("target/doc/astrup/frontpage_example.png").expect("Could not save doc frontpage_example.png");
62
63    // Display the result on screen
64    View::new_from(fig).expect("Could not add figure to view")
65                       .show();
66}