-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmain.rs
More file actions
202 lines (174 loc) · 6.02 KB
/
main.rs
File metadata and controls
202 lines (174 loc) · 6.02 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#![allow(dead_code)]
use std::f64::consts::PI;
use plotly::common::{ColorScale, ColorScalePalette, Font, HoverInfo};
use plotly::contour::Contours;
use plotly::{Contour, HeatMap, Layout, Plot};
use plotly_utils::write_example_to_html;
// Contour Plots
// ANCHOR: simple_contour_plot
fn simple_contour_plot(show: bool, file_name: &str) {
let n = 200;
let mut x = Vec::<f64>::new();
let mut y = Vec::<f64>::new();
let mut z: Vec<Vec<f64>> = Vec::new();
for index in 0..n {
let value = -2.0 * PI + 4.0 * PI * (index as f64) / (n as f64);
x.push(value);
y.push(value);
}
y.iter().take(n).for_each(|y| {
let mut row = Vec::<f64>::new();
x.iter().take(n).for_each(|x| {
let radius_squared = x.powf(2.0) + y.powf(2.0);
let zv = x.sin() * y.cos() * radius_squared.sin() / (radius_squared + 1.0).log10();
row.push(zv);
});
z.push(row);
});
let trace = Contour::new(x, y, z);
let mut plot = Plot::new();
plot.add_trace(trace);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: simple_contour_plot
// ANCHOR: colorscale_for_contour_plot
fn colorscale_for_contour_plot(show: bool, file_name: &str) {
let z = vec![
vec![10.0, 10.625, 12.5, 15.625, 20.0],
vec![5.625, 6.25, 8.125, 11.25, 15.625],
vec![2.5, 3.125, 5., 8.125, 12.5],
vec![0.625, 1.25, 3.125, 6.25, 10.625],
vec![0.0, 0.625, 2.5, 5.625, 10.0],
];
let trace = Contour::new_z(z).color_scale(ColorScale::Palette(ColorScalePalette::Jet));
let layout = Layout::new().title("Colorscale for Contour Plot");
let mut plot = Plot::new();
plot.set_layout(layout);
plot.add_trace(trace);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: colorscale_for_contour_plot
// ANCHOR: customizing_size_and_range_of_a_contour_plots_contours
fn customizing_size_and_range_of_a_contour_plots_contours(show: bool, file_name: &str) {
let z = vec![
vec![10.0, 10.625, 12.5, 15.625, 20.0],
vec![5.625, 6.25, 8.125, 11.25, 15.625],
vec![2.5, 3.125, 5., 8.125, 12.5],
vec![0.625, 1.25, 3.125, 6.25, 10.625],
vec![0.0, 0.625, 2.5, 5.625, 10.0],
];
let trace = Contour::new_z(z)
.color_scale(ColorScale::Palette(ColorScalePalette::Jet))
.auto_contour(false)
.contours(Contours::new().start(0.0).end(8.0).size(2.0));
let layout = Layout::new().title("Customizing Size and Range of Contours");
let mut plot = Plot::new();
plot.set_layout(layout);
plot.add_trace(trace);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: customizing_size_and_range_of_a_contour_plots_contours
// ANCHOR: customizing_spacing_between_x_and_y_ticks
fn customizing_spacing_between_x_and_y_ticks(show: bool, file_name: &str) {
let z = vec![
vec![10.0, 10.625, 12.5, 15.625, 20.0],
vec![5.625, 6.25, 8.125, 11.25, 15.625],
vec![2.5, 3.125, 5., 8.125, 12.5],
vec![0.625, 1.25, 3.125, 6.25, 10.625],
vec![0.0, 0.625, 2.5, 5.625, 10.0],
];
let trace = Contour::new_z(z)
.color_scale(ColorScale::Palette(ColorScalePalette::Jet))
.dx(10.0)
.x0(5.0)
.dy(10.0)
.y0(10.0);
let layout = Layout::new().title("Customizing Size and Range of Contours");
let mut plot = Plot::new();
plot.set_layout(layout);
plot.add_trace(trace);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: customizing_spacing_between_x_and_y_ticks
// Heatmaps
// ANCHOR: basic_heat_map
fn basic_heat_map(show: bool, file_name: &str) {
let z = vec![vec![1, 20, 30], vec![20, 1, 60], vec![30, 60, 1]];
let trace = HeatMap::new_z(z)
.zmin(1.0)
.zmax(60.0)
.hover_info(HoverInfo::Text)
.hover_text_matrix(vec![
vec!["A", "B", "C"],
vec!["D", "E", "F"],
vec!["G", "H", "I"],
]);
let mut plot = Plot::new();
plot.add_trace(trace);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: basic_heat_map
// ANCHOR: customized_heat_map
fn customized_heat_map(show: bool, file_name: &str) {
let x = (0..100).map(|x| x as f64).collect::<Vec<f64>>();
let y = (0..100).map(|y| y as f64).collect::<Vec<f64>>();
let z: Vec<Vec<f64>> = y
.iter()
.map(|y| {
x.iter()
.map(|x| (x / 5.0).powf(2.0) + (y / 5.0).powf(2.0))
.collect::<Vec<f64>>()
})
.collect::<Vec<Vec<f64>>>();
let (_z_min, z_max) = z
.iter()
.flatten()
.fold((f64::MAX, f64::MIN), |(min, max), &x| {
(min.min(x), max.max(x))
});
let colorscale = ColorScalePalette::Jet;
let trace = HeatMap::new(x, y, z)
.zmin(z_max * 0.4)
.zmax(z_max * 0.5)
.color_scale(colorscale.into());
let layout = Layout::new()
.title("Customized Heatmap")
.font(Font::new().size(32));
let mut plot = Plot::new();
plot.set_layout(layout);
plot.add_trace(trace);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: customized_heat_map
fn main() {
// Change false to true on any of these lines to display the example.
// Contour Plots
simple_contour_plot(false, "simple_contour_plot");
colorscale_for_contour_plot(false, "colorscale_for_contour_plot");
customizing_size_and_range_of_a_contour_plots_contours(
false,
"customizing_size_and_range_of_a_contour_plots_contours",
);
customizing_spacing_between_x_and_y_ticks(false, "customizing_spacing_between_x_and_y_ticks");
// Heatmaps
basic_heat_map(false, "basic_heat_map");
customized_heat_map(false, "customized_heat_map");
}