-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_fill_between.rs
More file actions
93 lines (77 loc) · 2.6 KB
/
test_fill_between.rs
File metadata and controls
93 lines (77 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use plotpy::{linspace, Curve, FillBetween, Plot, StrError};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
const OUT_DIR: &str = "/tmp/plotpy/integ_tests";
#[test]
fn test_fill_between_1() -> Result<(), StrError> {
// data
let x = linspace(-1.0, 2.0, 21);
let y1: Vec<_> = x.iter().map(|&x| x * x).collect();
let y2: Vec<_> = x.iter().map(|&x| x).collect();
// draw
let mut fb = FillBetween::new();
fb.draw(&x, &y1, Some(&y2));
// add fb to plot
let mut plot = Plot::new();
plot.add(&fb);
// save figure
let path = Path::new(OUT_DIR).join("integ_fill_between_1.svg");
plot.save(&path)?;
// check number of lines
let file = File::open(path).map_err(|_| "cannot open file")?;
let buffered = BufReader::new(file);
let lines_iter = buffered.lines();
let n = lines_iter.count();
assert!(n > 420 && n < 500);
Ok(())
}
#[test]
fn test_fill_between_2() -> Result<(), StrError> {
// data
let x = linspace(-1.0, 2.0, 21);
let y1: Vec<_> = x.iter().map(|&x| x * x).collect();
let y2: Vec<_> = x.iter().map(|&x| x).collect();
// draw
let mut fb = FillBetween::new();
fb.set_interpolate(true);
fb.set_facecolor("#ffaabb").set_where("y1>=y2").draw(&x, &y1, Some(&y2));
fb.set_facecolor("#c1e3ff").set_where("y2>=y1").draw(&x, &y1, Some(&y2));
// add fb to plot
let mut plot = Plot::new();
plot.add(&fb);
// save figure
let path = Path::new(OUT_DIR).join("integ_fill_between_2.svg");
plot.save(&path)?;
// check number of lines
let file = File::open(path).map_err(|_| "cannot open file")?;
let buffered = BufReader::new(file);
let lines_iter = buffered.lines();
let n = lines_iter.count();
assert!(n > 450 && n < 500);
Ok(())
}
#[test]
fn test_fill_between_3() -> Result<(), StrError> {
// data and curve
let x = linspace(-1.0, 2.0, 21);
let y: Vec<_> = x.iter().map(|&x| x * x).collect();
let mut curve = Curve::new();
curve.set_line_color("black").draw(&x, &y);
// draw
let mut fb = FillBetween::new();
fb.set_where("y1>=0.5").set_extra("alpha=0.5").draw(&x, &y, None);
// add curve and fb to plot
let mut plot = Plot::new();
plot.add(&curve).add(&fb);
// save figure
let path = Path::new(OUT_DIR).join("integ_fill_between_3.svg");
plot.save(&path)?;
// check number of lines
let file = File::open(path).map_err(|_| "cannot open file")?;
let buffered = BufReader::new(file);
let lines_iter = buffered.lines();
let n = lines_iter.count();
assert!(n > 500 && n < 560);
Ok(())
}