-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_legend.rs
More file actions
96 lines (79 loc) · 2.67 KB
/
test_legend.rs
File metadata and controls
96 lines (79 loc) · 2.67 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
94
95
96
use plotpy::{Curve, Legend, 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_legend_1() -> Result<(), StrError> {
// curve and options
let mut curve1 = Curve::new();
curve1.set_label("my-curve");
// another curve
let mut curve2 = Curve::new();
curve2.set_label("another-curve");
// draw curve
let x = &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
let y = &[1.0, 1.414, 1.732, 2.0, 2.236, 2.449, 2.645, 2.828, 3.0, 3.162];
let y2 = &[1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 3.5, 3.5, 3.5, 3.5];
curve1.draw(x, y);
curve2.draw(x, y2);
// legend and options
let mut legend = Legend::new();
legend
.set_fontsize(10.0)
.set_handle_len(3.2)
.set_num_col(2)
.set_outside(true)
.set_show_frame(false)
.set_x_coords(&[0.2, 1.05, 0.8, 0.1]);
// draw legend
legend.draw();
// add curve and legend to plot
let mut plot = Plot::new();
plot.add(&curve1);
plot.add(&curve2);
plot.add(&legend); // must be after a (labelled) curve
// save figure
let path = Path::new(OUT_DIR).join("integ_legend_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();
assert!(lines_iter.count() > 780);
Ok(())
}
#[test]
fn test_legend_2() -> Result<(), StrError> {
// curve and options
let mut curve1 = Curve::new();
curve1.set_marker_style("o").set_label("my-curve");
// another curve
let mut curve2 = Curve::new();
curve2.set_marker_style("*").set_label("another-curve");
// draw curve
let x = &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
let y = &[1.0, 1.414, 1.732, 2.0, 2.236, 2.449, 2.645, 2.828, 3.0, 3.162];
let y2 = &[1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 3.5, 3.5, 3.5, 3.5];
curve1.draw(x, y);
curve2.draw(x, y2);
// legend and options
let mut legend = Legend::new();
legend.set_extra("numpoints=3").set_location("lower right");
// draw legend
legend.draw();
// add curve and legend to plot
let mut plot = Plot::new();
plot.add(&curve1);
plot.add(&curve2);
plot.add(&legend); // must be after a (labelled) curve
// save figure
let path = Path::new(OUT_DIR).join("integ_legend_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();
assert!(lines_iter.count() > 780);
Ok(())
}