-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmain.rs
More file actions
549 lines (477 loc) · 16.8 KB
/
main.rs
File metadata and controls
549 lines (477 loc) · 16.8 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#![allow(dead_code)]
use plotly::common::{Anchor, AxisSide, Font, Title};
use plotly::layout::{
Annotation, Axis, GridPattern, Layout, LayoutGrid, Legend, RowOrder, TraceOrder,
};
use plotly::Configuration;
use plotly::{color::Rgb, Plot, Scatter};
use plotly_utils::write_example_to_html;
// SubplotsBuilder
// ANCHOR: simple_subplot
fn simple_subplot(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![1, 2, 3], vec![4, 5, 6]).name("trace1");
let trace2 = Scatter::new(vec![20, 30, 40], vec![50, 60, 70])
.name("trace2")
.x_axis("x2")
.y_axis("y2");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
let layout = Layout::new().grid(
LayoutGrid::new()
.rows(1)
.columns(2)
.pattern(GridPattern::Independent),
);
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: simple_subplot
// ANCHOR: simple_subplot_matches_x_axis
fn simple_subplot_matches_x_axis(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![1, 2, 3], vec![4, 5, 6]).name("trace1");
let trace2 = Scatter::new(vec![20, 30, 40], vec![50, 60, 70])
.name("trace2")
.x_axis("x2")
.y_axis("y2");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
let layout = Layout::new().x_axis(Axis::new().matches("x2")).grid(
LayoutGrid::new()
.rows(1)
.columns(2)
.pattern(GridPattern::Independent),
);
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: simple_subplot_matches_x_axis
// ANCHOR: simple_subplot_matches_y_axis
fn simple_subplot_matches_y_axis(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![1, 2, 3], vec![4, 5, 6]).name("trace1");
let trace2 = Scatter::new(vec![20, 30, 40], vec![50, 60, 70])
.name("trace2")
.x_axis("x2")
.y_axis("y2");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
let layout = Layout::new().y_axis(Axis::new().matches("y2")).grid(
LayoutGrid::new()
.rows(1)
.columns(2)
.pattern(GridPattern::Independent),
);
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: simple_subplot_matches_y_axis
// ANCHOR: custom_sized_subplot
fn custom_sized_subplot(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![1, 2, 3], vec![4, 5, 6]).name("trace1");
let trace2 = Scatter::new(vec![20, 30, 40], vec![50, 60, 70])
.name("trace2")
.x_axis("x2")
.y_axis("y2");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
let layout = Layout::new()
.x_axis(Axis::new().domain(&[0., 0.7]))
.y_axis2(Axis::new().anchor("x2"))
.x_axis2(Axis::new().domain(&[0.8, 1.]));
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: custom_sized_subplot
// ANCHOR: multiple_subplots
fn multiple_subplots(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![1, 2, 3], vec![4, 5, 6]).name("trace1");
let trace2 = Scatter::new(vec![20, 30, 40], vec![50, 60, 70])
.name("trace2")
.x_axis("x2")
.y_axis("y2");
let trace3 = Scatter::new(vec![300, 400, 500], vec![600, 700, 800])
.x_axis("x3")
.y_axis("y3");
let trace4 = Scatter::new(vec![4000, 5000, 6000], vec![7000, 8000, 9000])
.x_axis("x4")
.y_axis("y4");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
plot.add_trace(trace3);
plot.add_trace(trace4);
let layout = Layout::new().grid(
LayoutGrid::new()
.rows(2)
.columns(2)
.pattern(GridPattern::Independent),
);
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: multiple_subplots
// ANCHOR: stacked_subplots
fn stacked_subplots(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![0, 1, 2], vec![10, 11, 12]).name("trace1");
let trace2 = Scatter::new(vec![2, 3, 4], vec![100, 110, 120])
.name("trace2")
.x_axis("x2")
.y_axis("y2");
let trace3 = Scatter::new(vec![3, 4, 5], vec![1000, 1100, 1200])
.x_axis("x3")
.y_axis("y3");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
plot.add_trace(trace3);
let layout = Layout::new().grid(
LayoutGrid::new()
.rows(3)
.columns(1)
.pattern(GridPattern::Independent)
.row_order(RowOrder::BottomToTop),
);
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: stacked_subplots
// ANCHOR: stacked_subplots_with_shared_x_axis
fn stacked_subplots_with_shared_x_axis(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![0, 1, 2], vec![10, 11, 12]).name("trace1");
let trace2 = Scatter::new(vec![2, 3, 4], vec![100, 110, 120])
.name("trace2")
.y_axis("y2");
let trace3 = Scatter::new(vec![3, 4, 5], vec![1000, 1100, 1200]).y_axis("y3");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
plot.add_trace(trace3);
let layout = Layout::new()
.y_axis(Axis::new().domain(&[0., 0.33]))
.legend(Legend::new().trace_order(TraceOrder::Reversed))
.y_axis2(Axis::new().domain(&[0.33, 0.66]))
.y_axis3(Axis::new().domain(&[0.66, 1.]));
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: stacked_subplots_with_shared_x_axis
// ANCHOR: multiple_custom_sized_subplots
fn multiple_custom_sized_subplots(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![1, 2], vec![1, 2]).name("(1,1)");
let trace2 = Scatter::new(vec![1, 2], vec![1, 2])
.name("(1,2,1)")
.x_axis("x2")
.y_axis("y2");
let trace3 = Scatter::new(vec![1, 2], vec![1, 2])
.name("(1,2,2)")
.x_axis("x3")
.y_axis("y3");
let trace4 = Scatter::new(vec![1, 2], vec![1, 2])
.name("{(2,1), (2,2)}")
.x_axis("x4")
.y_axis("y4");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
plot.add_trace(trace3);
plot.add_trace(trace4);
let layout = Layout::new()
.title("Multiple Custom Sized SubplotsBuilder")
.x_axis(Axis::new().domain(&[0., 0.45]).anchor("y1"))
.y_axis(Axis::new().domain(&[0.5, 1.]).anchor("x1"))
.x_axis2(Axis::new().domain(&[0.55, 1.]).anchor("y2"))
.y_axis2(Axis::new().domain(&[0.8, 1.]).anchor("x2"))
.x_axis3(Axis::new().domain(&[0.55, 1.]).anchor("y3"))
.y_axis3(Axis::new().domain(&[0.5, 0.75]).anchor("x3"))
.x_axis4(Axis::new().domain(&[0., 1.]).anchor("y4"))
.y_axis4(Axis::new().domain(&[0., 0.45]).anchor("x4"));
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: multiple_custom_sized_subplots
// Multiple Axes
// ANCHOR: two_y_axes
fn two_y_axes(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![1, 2, 3], vec![40, 50, 60]).name("trace1");
let trace2 = Scatter::new(vec![2, 3, 4], vec![4, 5, 6])
.name("trace2")
.y_axis("y2");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
let layout = Layout::new()
.title("Double Y Axis Example")
.y_axis(Axis::new().title("yaxis title"))
.y_axis2(
Axis::new()
.title(Title::from("yaxis2 title").font(Font::new().color(Rgb::new(148, 103, 189))))
.tick_font(Font::new().color(Rgb::new(148, 103, 189)))
.overlaying("y")
.side(AxisSide::Right),
);
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: two_y_axes
// ANCHOR: multiple_axes
fn multiple_axes(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![1, 2, 3], vec![4, 5, 6]).name("trace1");
let trace2 = Scatter::new(vec![2, 3, 4], vec![40, 50, 60])
.name("trace2")
.y_axis("y2");
let trace3 = Scatter::new(vec![4, 5, 6], vec![40_000, 50_000, 60_000]).y_axis("y3");
let trace4 = Scatter::new(vec![5, 6, 7], vec![400_000, 500_000, 600_000]).y_axis("y4");
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
plot.add_trace(trace3);
plot.add_trace(trace4);
let layout = Layout::new()
.title("multiple y-axes example")
.width(800)
.x_axis(Axis::new().domain(&[0.3, 0.7]))
.y_axis(
Axis::new()
.title(Title::from("yaxis title").font(Font::new().color("#1f77b4")))
.tick_font(Font::new().color("#1f77b4")),
)
.y_axis2(
Axis::new()
.title(Title::from("yaxis2 title").font(Font::new().color("#ff7f0e")))
.tick_font(Font::new().color("#ff7f0e"))
.anchor("free")
.overlaying("y")
.side(AxisSide::Left)
.position(0.15),
)
.y_axis3(
Axis::new()
.title(Title::from("yaxis3 title").font(Font::new().color("#d62728")))
.tick_font(Font::new().color("#d62728"))
.anchor("x")
.overlaying("y")
.side(AxisSide::Right),
)
.y_axis4(
Axis::new()
.title(Title::from("yaxis4 title").font(Font::new().color("#9467bd")))
.tick_font(Font::new().color("#9467bd"))
.anchor("free")
.overlaying("y")
.side(AxisSide::Right)
.position(0.85),
);
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: multiple_axes
// ANCHOR: many_subplots_with_titles
fn many_subplots_with_titles(show: bool, file_name: &str) {
let trace1 = Scatter::new(vec![1, 2], vec![4, 5]);
let number_of_plots = 10;
let mut plot = Plot::new();
let mut layout = Layout::new()
.grid(
LayoutGrid::new()
.rows(number_of_plots / 2)
.columns(2)
.pattern(GridPattern::Independent),
)
.height(number_of_plots * 200);
for i in 1..number_of_plots + 1 {
plot.add_trace(
trace1
.clone()
.y_axis(format!("y{i}"))
.x_axis(format!("x{i}")),
);
layout.add_annotation(
Annotation::new()
.y_ref(format!("y{i} domain"))
.y_anchor(Anchor::Bottom)
.y(1)
.text(format!("Title {i}"))
.x_ref(format!("x{i} domain"))
.x_anchor(Anchor::Center)
.x(0.5)
.show_arrow(false),
)
}
plot.set_layout(layout);
plot.set_configuration(Configuration::new().responsive(true));
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: many_subplots_with_titles
// ANCHOR: subplots_with_multiple_traces
fn subplots_with_multiple_traces(show: bool, file_name: &str) {
// Create multiple traces for the first subplot (left side)
let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 11, 12, 13])
.name("Line 1")
.mode(plotly::common::Mode::LinesMarkers);
let trace2 = Scatter::new(vec![1, 2, 3, 4], vec![8, 9, 10, 11])
.name("Line 2")
.mode(plotly::common::Mode::LinesMarkers);
let trace3 = Scatter::new(vec![1, 2, 3, 4], vec![12, 13, 14, 15])
.name("Line 3")
.mode(plotly::common::Mode::LinesMarkers);
// Create traces for the second subplot (right side)
let trace4 = Scatter::new(vec![1, 2, 3, 4], vec![20, 25, 30, 35])
.name("Dots 1")
.x_axis("x2")
.y_axis("y2")
.mode(plotly::common::Mode::Markers);
let trace5 = Scatter::new(vec![1, 2, 3, 4], vec![15, 20, 25, 30])
.name("Dots 2")
.x_axis("x2")
.y_axis("y2")
.mode(plotly::common::Mode::Markers);
let mut plot = Plot::new();
// Add traces to first subplot (default axes)
plot.add_trace(trace1);
plot.add_trace(trace2);
plot.add_trace(trace3);
// Add traces to second subplot (x2, y2 axes)
plot.add_trace(trace4);
plot.add_trace(trace5);
let layout = Layout::new()
.title("SubplotsBuilder with Multiple Traces")
.grid(
LayoutGrid::new()
.rows(1)
.columns(2)
.pattern(GridPattern::Independent),
);
plot.set_layout(layout);
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: subplots_with_multiple_traces
// ANCHOR: make_subplots_api
fn make_subplots_api(show: bool, file_name: &str) {
use plotly::subplot::SubplotsBuilder;
let mut fig = SubplotsBuilder::new(1, 2)
.subplot_titles(vec!["Plot 1", "Plot 2"])
.x_axis_titles(vec!["X Axis 1", "X Axis 2"])
.y_axis_titles(vec!["Y Axis 1", "Y Axis 2"])
.x_title_at(1, 1, "X Axis 1")
.y_title_at(1, 2, "Y Axis 2")
.horizontal_spacing(0.1);
fig.add_trace(Scatter::new(vec![1, 2, 3], vec![4, 5, 6]), 1, 1);
fig.add_trace(Scatter::new(vec![20, 30, 40], vec![50, 60, 70]), 1, 2);
let plot = fig.make_subplots();
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR: make_subplots_large_grid
fn make_subplots_large_grid(show: bool, file_name: &str) {
use plotly::subplot::SubplotsBuilder;
// Create a 4x4 grid (16 subplots) to test beyond the 8-axis hardcoded limit
let titles = vec![
"Subplot 1",
"Subplot 2",
"Subplot 3",
"Subplot 4",
"Subplot 5",
"Subplot 6",
"Subplot 7",
"Subplot 8",
"Subplot 9",
"Subplot 10",
"Subplot 11",
"Subplot 12",
"Subplot 13",
"Subplot 14",
"Subplot 15",
"Subplot 16",
];
let rows = 4;
let cols = 4;
let mut fig = SubplotsBuilder::new(rows, cols)
.subplot_titles(titles)
.x_axis_titles(vec!["X Axis 1", "X Axis 2", "X Axis 3", "X Axis 4"])
.y_axis_titles(vec!["Y Axis 1", "Y Axis 2", "Y Axis 3", "Y Axis 4"])
.x_title_at(1, 1, "X Axis 1")
.y_title_at(1, 2, "Y Axis 2")
.horizontal_spacing(0.05)
.vertical_spacing(0.5);
for row in 1..=rows {
for col in 1..=cols {
let subplot_num = (row - 1) * cols + col;
let x_data: Vec<i32> = (1..=5).map(|i| (i as i32) * subplot_num as i32).collect();
let y_data: Vec<i32> = (1..=5)
.map(|i| (i as i32) * subplot_num as i32 + 10)
.collect();
fig.add_trace(
Scatter::new(x_data, y_data).name(&format!("Trace {}", subplot_num)),
row as usize,
col as usize,
);
}
}
let plot = fig.make_subplots();
let path = write_example_to_html(&plot, file_name);
if show {
plot.show_html(path);
}
}
// ANCHOR_END: make_subplots_large_grid
fn main() {
// Change false to true on any of these lines to display the example.
// SubplotsBuilder
simple_subplot(false, "simple_subplot");
simple_subplot_matches_x_axis(false, "simple_subplot_matches_x_axis");
simple_subplot_matches_y_axis(false, "simple_subplot_matches_y_axis");
custom_sized_subplot(false, "custom_sized_subplot");
multiple_subplots(false, "multiple_subplots");
stacked_subplots(false, "stacked_subplots");
stacked_subplots_with_shared_x_axis(false, "stacked_subplots_with_shared_x_axis");
multiple_custom_sized_subplots(false, "multiple_custom_sized_subplots");
many_subplots_with_titles(false, "many_subplots_with_titles");
// Multiple traces in subplots
subplots_with_multiple_traces(false, "subplots_with_multiple_traces");
// Multiple Axes
two_y_axes(false, "two_y_axes");
multiple_axes(false, "multiple_axes");
make_subplots_api(false, "make_subplots_api");
make_subplots_large_grid(false, "make_subplots_large_grid");
}