forked from plotly/plotly.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot3d.rs
More file actions
117 lines (102 loc) · 3.28 KB
/
Copy pathplot3d.rs
File metadata and controls
117 lines (102 loc) · 3.28 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
use itertools_num::linspace;
use plotly::{
common::{ColorScale, ColorScalePalette, Marker, MarkerSymbol, Mode, Title},
layout::{Axis, Layout},
Plot, Scatter3D, Surface,
};
// 3D Scatter Plots
fn simple_scatter3d_plot(show: bool) {
let n: usize = 100;
let t: Vec<f64> = linspace(0., 10., n).collect();
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
let z: Vec<f64> = t.iter().map(|x| x.cos()).collect();
let trace = Scatter3D::new(t, y, z).mode(Mode::Markers);
let mut plot = Plot::new();
plot.add_trace(trace);
if show {
plot.show();
}
}
fn customized_scatter3d_plot(show: bool) {
let n: usize = 100;
let t: Vec<f64> = linspace::<f64>(0., 10., n).collect();
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
let z: Vec<f64> = t.iter().map(|x| x.cos()).collect();
let sizelookup = z.clone();
let trace = Scatter3D::new(t.clone(), y.clone(), z.iter().map(|i| -i).collect())
.mode(Mode::Markers)
.marker(
Marker::new()
.symbol(MarkerSymbol::Diamond)
.size_array(
sizelookup
.iter()
.map(|i| (i.abs() * 25f64) as usize)
.collect(),
)
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis)),
);
let trace2 = Scatter3D::new(t, z, y).mode(Mode::Markers).marker(
Marker::new()
.size_array(
sizelookup
.iter()
.map(|i| (i.abs() * 25f64) as usize)
.collect(),
)
.color_scale(ColorScale::Palette(ColorScalePalette::Viridis)),
);
let mut plot = Plot::new();
plot.add_trace(trace);
plot.add_trace(trace2);
let layout = Layout::new()
.title("Helix".into())
.x_axis(Axis::new().title("x (A meaningful axis name goes here)".into()))
.y_axis(Axis::new().title(Title::new("This is the label of the Y axis")))
.z_axis(Axis::new().title("z Axis".into()));
plot.set_layout(layout);
if show {
plot.show();
}
}
// 3D Line Plots
fn simple_line3d_plot(show: bool) {
let n: usize = 100;
let t: Vec<f64> = linspace(0., 10., n).collect();
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
let z: Vec<f64> = t.iter().map(|x| x.cos()).collect();
let trace = Scatter3D::new(t, y, z).mode(Mode::Lines);
let mut plot = Plot::new();
plot.add_trace(trace);
if show {
plot.show();
}
}
// 3D Surface Plot
fn surface_plot(show: bool) {
let n: usize = 100;
let x: Vec<f64> = linspace(-10., 10., n).collect();
let y: Vec<f64> = linspace(-10., 10., n).collect();
let z: Vec<Vec<f64>> = x
.iter()
.map(|i| {
y.iter()
.map(|j| 1.0 / (j * j + 5.0) * j.sin() + 1.0 / (i * i + 5.0) * i.cos())
.collect()
})
.collect();
let trace = Surface::new(z).x(x).y(y);
let mut plot = Plot::new();
plot.add_trace(trace);
if show {
plot.show();
}
}
fn main() -> std::io::Result<()> {
// Scatter3D Plots
simple_scatter3d_plot(true);
simple_line3d_plot(true);
customized_scatter3d_plot(true);
surface_plot(true);
Ok(())
}