forked from plotly/plotly.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathndarray_support.rs
More file actions
94 lines (82 loc) · 2.51 KB
/
Copy pathndarray_support.rs
File metadata and controls
94 lines (82 loc) · 2.51 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
#[cfg(feature = "plotly_ndarray")]
use ndarray::{Array, Ix1, Ix2};
#[cfg(feature = "plotly_ndarray")]
use plotly::common::Mode;
#[cfg(feature = "plotly_ndarray")]
use plotly::ndarray::ArrayTraces;
#[cfg(feature = "plotly_ndarray")]
use plotly::{Plot, Scatter};
#[cfg(feature = "plotly_ndarray")]
fn single_ndarray_trace(show: bool) {
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);
if show {
plot.show();
}
println!("{}", plot.to_inline_html(Some("single_ndarray_trace")));
}
#[cfg(feature = "plotly_ndarray")]
fn multiple_ndarray_traces_over_columns(show: bool) {
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);
if show {
plot.show();
}
println!(
"{}",
plot.to_inline_html(Some("multiple_ndarray_traces_over_columns"))
);
}
#[cfg(feature = "plotly_ndarray")]
fn multiple_ndarray_traces_over_rows(show: bool) {
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);
if show {
plot.show();
}
println!(
"{}",
plot.to_inline_html(Some("multiple_ndarray_traces_over_rows"))
);
}
#[cfg(feature = "plotly_ndarray")]
fn main() -> std::io::Result<()> {
single_ndarray_trace(true);
multiple_ndarray_traces_over_columns(true);
multiple_ndarray_traces_over_rows(true);
Ok(())
}
#[cfg(not(feature = "plotly_ndarray"))]
fn main() {}