-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_subplot.rs
More file actions
125 lines (109 loc) · 3.5 KB
/
test_subplot.rs
File metadata and controls
125 lines (109 loc) · 3.5 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use plotpy::{Curve, 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_subplot() -> Result<(), StrError> {
// curves
let mut curve1 = Curve::new();
let mut curve2 = Curve::new();
let x = &[1.0, 2.0, 3.0, 4.0];
let y = &[1.0, 1.424, 1.732, 2.0];
let z = &[1.0, 4.0, 9.0, 16.0];
curve1.draw(x, y);
curve2.draw(x, z);
// plot and subplots
let mut plot = Plot::new();
plot.set_subplot(1, 2, 1).add(&curve1);
plot.set_subplot(1, 2, 2).add(&curve2);
// save figure
let path = Path::new(OUT_DIR).join("integ_subplot_before.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 > 600);
assert!(n < 650);
// clear the current axes
plot.set_subplot(1, 2, 1).clear_current_axes();
// save figure again
let path = Path::new(OUT_DIR).join("integ_subplot_after.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 > 650);
assert!(n < 680);
Ok(())
}
#[test]
fn test_gridspec_1() -> Result<(), StrError> {
// curves
let mut curve1 = Curve::new();
let mut curve2 = Curve::new();
let x = &[1.0, 2.0, 3.0, 4.0];
let y = &[1.0, 1.424, 1.732, 2.0];
let z = &[1.0, 4.0, 9.0, 16.0];
curve1.draw(x, y);
curve2.draw(x, z);
// plot and gridspec
let mut plot = Plot::new();
plot.set_gridspec("my_grid", 3, 1, "wspace=0, hspace=0.7")
.set_subplot_grid("my_grid", "0:2", "0")
.add(&curve1)
.set_subplot_grid("my_grid", "2", "0")
.add(&curve2);
// save figure
let path = Path::new(OUT_DIR).join("integ_gridspec_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 > 650);
assert!(n < 700);
Ok(())
}
#[test]
fn test_gridspec_rotation_and_align_labels() -> Result<(), StrError> {
// curves
let mut curve = Curve::new();
let x = &[1000.0, 2000.0, 3000.0, 4000.0];
let y = &[1.0, 2.0, 3.0, 4.0];
curve.draw(x, y);
// plot and gridspec
let mut plot = Plot::new();
plot.set_gridspec("grid", 2, 2, "hspace=0.35")
.set_subplot_grid("grid", "0", ":")
.set_label_x("x-label 0 0")
.set_label_y("y-label 0 0")
.set_rotation_ticks_y(55.0)
.add(&curve)
.set_subplot_grid("grid", "1", "0")
.set_label_x("x-label 1 0")
.set_label_y("y-label 1 0")
.set_rotation_ticks_x(55.0)
.add(&curve)
.set_subplot_grid("grid", "1", "1")
.set_label_x("x-label 1 1")
.set_label_y("y-label 1 1")
.add(&curve)
.set_align_labels();
// save figure
let path = Path::new(OUT_DIR).join("integ_gridspec_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 > 930);
assert!(n < 960);
Ok(())
}