-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmain.rs
More file actions
82 lines (68 loc) · 2.35 KB
/
main.rs
File metadata and controls
82 lines (68 loc) · 2.35 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
#![allow(dead_code)]
use ndarray::{Array, Ix1, Ix2};
use plotly::common::Mode;
use plotly::ndarray::ArrayTraces;
use plotly::{Plot, Scatter};
use plotly_utils::write_example_to_html;
fn single_ndarray_trace(show: bool, file_name: &str) {
let n: usize = 11;
let t: Array<f64, Ix1> = Array::range(0., 10., 10. / n as f64);
let ys: Array<f64, Ix1> = t.iter().map(|v| (*v).powf(2.)).collect();
let trace = Scatter::from_array(t, ys).mode(Mode::LinesMarkers);
let mut plot = Plot::new();
plot.add_trace(trace);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
fn multiple_ndarray_traces_over_columns(show: bool, file_name: &str) {
let n: usize = 11;
let t: Array<f64, Ix1> = Array::range(0., 10., 10. / n as f64);
let mut ys: Array<f64, Ix2> = Array::zeros((11, 11));
let mut count = 0.;
for mut row in ys.columns_mut() {
for index in 0..row.len() {
row[index] = count + (index as f64).powf(2.);
}
count += 1.;
}
let traces =
Scatter::default()
.mode(Mode::LinesMarkers)
.to_traces(t, ys, ArrayTraces::OverColumns);
let mut plot = Plot::new();
plot.add_traces(traces);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
fn multiple_ndarray_traces_over_rows(show: bool, file_name: &str) {
let n: usize = 11;
let t: Array<f64, Ix1> = Array::range(0., 10., 10. / n as f64);
let mut ys: Array<f64, Ix2> = Array::zeros((11, 11));
let mut count = 0.;
for mut row in ys.columns_mut() {
for index in 0..row.len() {
row[index] = count + (index as f64).powf(2.);
}
count += 1.;
}
let traces =
Scatter::default()
.mode(Mode::LinesMarkers)
.to_traces(t, ys, ArrayTraces::OverRows);
let mut plot = Plot::new();
plot.add_traces(traces);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
fn main() {
// Change false to true on any of these lines to display the example.
single_ndarray_trace(false, "single_ndarray_trace");
multiple_ndarray_traces_over_columns(false, "multiple_ndarray_traces_over_columns");
multiple_ndarray_traces_over_rows(false, "multiple_ndarray_traces_over_rows");
}