-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmain.rs
More file actions
761 lines (667 loc) · 21.8 KB
/
main.rs
File metadata and controls
761 lines (667 loc) · 21.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
#![allow(dead_code)]
use ndarray::Array;
use plotly::{
box_plot::{BoxMean, BoxPoints},
color::{NamedColor, Rgb, Rgba},
common::{ErrorData, ErrorType, Line, Marker, Mode, Orientation},
histogram::{Bins, Cumulative, HistFunc, HistNorm},
layout::{Axis, BarMode, BoxMode, Layout, Margin},
Bar, BoxPlot, Histogram, Plot, Scatter,
};
use rand_distr::{Distribution, Normal, Uniform};
// Error Bars
// ANCHOR: basic_symmetric_error_bars
fn basic_symmetric_error_bars(show: bool) -> Plot {
let trace1 = Scatter::new(vec![0, 1, 2], vec![6, 10, 2])
.name("trace1")
.error_y(ErrorData::new(ErrorType::Data).array(vec![1.0, 2.0, 3.0]));
let mut plot = Plot::new();
plot.add_trace(trace1);
if show {
plot.show();
}
plot
}
// ANCHOR_END: basic_symmetric_error_bars
// ANCHOR: asymmetric_error_bars
fn asymmetric_error_bars(show: bool) -> Plot {
let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![2, 1, 3, 4])
.name("trace1")
.error_y(
ErrorData::new(ErrorType::Data)
.array(vec![0.1, 0.2, 0.1, 0.1])
.array_minus(vec![0.2, 0.4, 1., 0.2]),
);
let mut plot = Plot::new();
plot.add_trace(trace1);
if show {
plot.show();
}
plot
}
// ANCHOR_END: asymmetric_error_bars
// ANCHOR: error_bars_as_a_percentage_of_the_y_value
fn error_bars_as_a_percentage_of_the_y_value(show: bool) -> Plot {
let trace1 = Scatter::new(vec![0, 1, 2], vec![6, 10, 2])
.name("trace1")
.error_y(ErrorData::new(ErrorType::Percent).value(50.).visible(true));
let mut plot = Plot::new();
plot.add_trace(trace1);
if show {
plot.show();
}
plot
}
// ANCHOR_END: error_bars_as_a_percentage_of_the_y_value
// ANCHOR: asymmetric_error_bars_with_a_constant_offset
fn asymmetric_error_bars_with_a_constant_offset(show: bool) -> Plot {
let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![2, 1, 3, 4])
.name("trace1")
.error_y(
ErrorData::new(ErrorType::Percent)
.symmetric(false)
.value(15.)
.value_minus(25.),
);
let mut plot = Plot::new();
plot.add_trace(trace1);
if show {
plot.show();
}
plot
}
// ANCHOR_END: asymmetric_error_bars_with_a_constant_offset
// ANCHOR: horizontal_error_bars
fn horizontal_error_bars(show: bool) -> Plot {
let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![2, 1, 3, 4])
.name("trace1")
.error_x(ErrorData::new(ErrorType::Percent).value(10.));
let mut plot = Plot::new();
plot.add_trace(trace1);
if show {
plot.show();
}
plot
}
// ANCHOR_END: horizontal_error_bars
// ANCHOR: bar_chart_with_error_bars
fn bar_chart_with_error_bars(show: bool) -> Plot {
let trace_c = Bar::new(vec!["Trial 1", "Trial 2", "Trial 3"], vec![3, 6, 4])
.error_y(ErrorData::new(ErrorType::Data).array(vec![1., 0.5, 1.5]));
let trace_e = Bar::new(vec!["Trial 1", "Trial 2", "Trial 3"], vec![4, 7, 3])
.error_y(ErrorData::new(ErrorType::Data).array(vec![0.5, 1., 2.]));
let mut plot = Plot::new();
plot.add_trace(trace_c);
plot.add_trace(trace_e);
let layout = Layout::new().bar_mode(BarMode::Group);
plot.set_layout(layout);
if show {
plot.show();
}
plot
}
// ANCHOR_END: bar_chart_with_error_bars
// ANCHOR: colored_and_styled_error_bars
fn colored_and_styled_error_bars(show: bool) -> Plot {
let x_theo: Vec<f64> = Array::linspace(-4., 4., 100).into_raw_vec_and_offset().0;
let sincx: Vec<f64> = x_theo
.iter()
.map(|x| (x * std::f64::consts::PI).sin() / (*x * std::f64::consts::PI))
.collect();
let x = vec![
-3.8, -3.03, -1.91, -1.46, -0.89, -0.24, -0.0, 0.41, 0.89, 1.01, 1.91, 2.28, 2.79, 3.56,
];
let y = vec![
-0.02, 0.04, -0.01, -0.27, 0.36, 0.75, 1.03, 0.65, 0.28, 0.02, -0.11, 0.16, 0.04, -0.15,
];
let trace1 = Scatter::new(x_theo, sincx).name("sinc(x)");
let trace2 = Scatter::new(x, y)
.mode(Mode::Markers)
.name("measured")
.error_y(
ErrorData::new(ErrorType::Constant)
.value(0.1)
.color(NamedColor::Purple)
.thickness(1.5)
.width(3),
)
.error_x(
ErrorData::new(ErrorType::Constant)
.value(0.2)
.color(NamedColor::Purple)
.thickness(1.5)
.width(3),
)
.marker(Marker::new().color(NamedColor::Purple).size(8));
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
if show {
plot.show();
}
plot
}
// ANCHOR_END: colored_and_styled_error_bars
// Box Plots
// ANCHOR: basic_box_plot
fn basic_box_plot(show: bool) -> Plot {
let mut rng = rand::rng();
let uniform1 = Uniform::new(0.0, 1.0).unwrap();
let uniform2 = Uniform::new(1.0, 2.0).unwrap();
let n = 50;
let mut y0 = Vec::with_capacity(n);
let mut y1 = Vec::with_capacity(n);
for _ in 0..n {
y0.push(uniform1.sample(&mut rng));
y1.push(uniform2.sample(&mut rng));
}
let trace1 = BoxPlot::<f64, f64>::new(y0);
let trace2 = BoxPlot::<f64, f64>::new(y1);
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
if show {
plot.show();
}
plot
}
// ANCHOR_END: basic_box_plot
// ANCHOR: box_plot_that_displays_the_underlying_data
fn box_plot_that_displays_the_underlying_data(show: bool) -> Plot {
let trace1 = BoxPlot::new(vec![0, 1, 1, 2, 3, 5, 8, 13, 21])
.box_points(BoxPoints::All)
.jitter(0.3)
.point_pos(-1.8);
let mut plot = Plot::new();
plot.add_trace(trace1);
if show {
plot.show();
}
plot
}
// ANCHOR_END: box_plot_that_displays_the_underlying_data
// ANCHOR: horizontal_box_plot
fn horizontal_box_plot(show: bool) -> Plot {
let x = vec![
"Set 1", "Set 1", "Set 1", "Set 1", "Set 1", "Set 1", "Set 1", "Set 1", "Set 1", "Set 2",
"Set 2", "Set 2", "Set 2", "Set 2", "Set 2", "Set 2", "Set 2", "Set 2",
];
let trace = BoxPlot::new_xy(
vec![1, 2, 3, 4, 4, 4, 8, 9, 10, 2, 3, 3, 3, 3, 5, 6, 6, 7],
x.clone(),
)
.orientation(Orientation::Horizontal);
let mut plot = Plot::new();
plot.add_trace(trace);
if show {
plot.show();
}
plot
}
// ANCHOR_END: horizontal_box_plot
// ANCHOR: grouped_box_plot
fn grouped_box_plot(show: bool) -> Plot {
let x = vec![
"day 1", "day 1", "day 1", "day 1", "day 1", "day 1", "day 2", "day 2", "day 2", "day 2",
"day 2", "day 2",
];
let trace1 = BoxPlot::new_xy(
x.clone(),
vec![0.2, 0.2, 0.6, 1.0, 0.5, 0.4, 0.2, 0.7, 0.9, 0.1, 0.5, 0.3],
);
let trace2 = BoxPlot::new_xy(
x.clone(),
vec![0.6, 0.7, 0.3, 0.6, 0.0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2],
);
let trace3 = BoxPlot::new_xy(
x.clone(),
vec![0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1.0, 0.3, 0.6, 0.8, 0.5],
);
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().title("normalized moisture").zero_line(false))
.box_mode(BoxMode::Group);
plot.set_layout(layout);
if show {
plot.show();
}
plot
}
// ANCHOR_END: grouped_box_plot
// ANCHOR: box_plot_styling_outliers
fn box_plot_styling_outliers(show: bool) -> Plot {
let y = vec![
0.75, 5.25, 5.5, 6.0, 6.2, 6.6, 6.80, 7.0, 7.2, 7.5, 7.5, 7.75, 8.15, 8.15, 8.65, 8.93,
9.2, 9.5, 10.0, 10.25, 11.5, 12.0, 16.0, 20.90, 22.3, 23.25,
];
let trace1 = BoxPlot::new(y.clone())
.name("All Points")
.jitter(0.3)
.point_pos(-1.8)
.marker(Marker::new().color(Rgb::new(7, 40, 89)))
.box_points(BoxPoints::All);
let trace2 = BoxPlot::new(y.clone())
.name("Only Whiskers")
.marker(Marker::new().color(Rgb::new(9, 56, 125)))
.box_points(BoxPoints::False);
let trace3 = BoxPlot::new(y.clone())
.name("Suspected Outlier")
.marker(
Marker::new()
.color(Rgb::new(8, 81, 156))
.outlier_color(Rgba::new(219, 64, 82, 0.6))
.line(
Line::new()
.outlier_color(Rgba::new(219, 64, 82, 1.0))
.outlier_width(2),
),
)
.box_points(BoxPoints::SuspectedOutliers);
let trace4 = BoxPlot::new(y)
.name("Whiskers and Outliers")
.marker(Marker::new().color(Rgb::new(107, 174, 214)))
.box_points(BoxPoints::Outliers);
let layout = Layout::new().title("Box Plot Styling Outliers");
let mut plot = Plot::new();
plot.set_layout(layout);
plot.add_trace(trace1);
plot.add_trace(trace2);
plot.add_trace(trace3);
plot.add_trace(trace4);
if show {
plot.show();
}
plot
}
// ANCHOR_END: box_plot_styling_outliers
// ANCHOR: box_plot_styling_mean_and_standard_deviation
fn box_plot_styling_mean_and_standard_deviation(show: bool) -> Plot {
let y = vec![
2.37, 2.16, 4.82, 1.73, 1.04, 0.23, 1.32, 2.91, 0.11, 4.51, 0.51, 3.75, 1.35, 2.98, 4.50,
0.18, 4.66, 1.30, 2.06, 1.19,
];
let trace1 = BoxPlot::new(y.clone())
.name("Only Mean")
.marker(Marker::new().color(Rgb::new(8, 81, 156)))
.box_mean(BoxMean::True);
let trace2 = BoxPlot::new(y)
.name("Mean and Standard Deviation")
.marker(Marker::new().color(Rgb::new(8, 81, 156)))
.box_mean(BoxMean::StandardDeviation);
let layout = Layout::new().title("Box Plot Styling Mean and Standard Deviation");
let mut plot = Plot::new();
plot.set_layout(layout);
plot.add_trace(trace1);
plot.add_trace(trace2);
if show {
plot.show();
}
plot
}
// ANCHOR_END: box_plot_styling_mean_and_standard_deviation
// ANCHOR: grouped_horizontal_box_plot
fn grouped_horizontal_box_plot(show: bool) -> Plot {
let x = vec![
"day 1", "day 1", "day 1", "day 1", "day 1", "day 1", "day 2", "day 2", "day 2", "day 2",
"day 2", "day 2",
];
let trace1 = BoxPlot::new_xy(
vec![0.2, 0.2, 0.6, 1.0, 0.5, 0.4, 0.2, 0.7, 0.9, 0.1, 0.5, 0.3],
x.clone(),
)
.name("Kale")
.marker(Marker::new().color("3D9970"))
.box_mean(BoxMean::False)
.orientation(Orientation::Horizontal);
let trace2 = BoxPlot::new_xy(
vec![0.6, 0.7, 0.3, 0.6, 0.0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2],
x.clone(),
)
.name("Radishes")
.marker(Marker::new().color("FF4136"))
.box_mean(BoxMean::False)
.orientation(Orientation::Horizontal);
let trace3 = BoxPlot::new_xy(
vec![0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1.0, 0.3, 0.6, 0.8, 0.5],
x.clone(),
)
.name("Carrots")
.marker(Marker::new().color("FF851B"))
.box_mean(BoxMean::False)
.orientation(Orientation::Horizontal);
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
plot.add_trace(trace3);
let layout = Layout::new()
.title("Grouped Horizontal Box Plot")
.x_axis(Axis::new().title("normalized moisture").zero_line(false))
.box_mode(BoxMode::Group);
plot.set_layout(layout);
if show {
plot.show();
}
plot
}
// ANCHOR_END: grouped_horizontal_box_plot
// ANCHOR: fully_styled_box_plot
fn fully_styled_box_plot(show: bool) -> Plot {
let rnd_sample = |num, mul| -> Vec<f64> {
let mut v: Vec<f64> = Vec::with_capacity(num);
let mut rng = rand::rng();
let uniform = Uniform::new(0.0, mul).unwrap();
for _ in 0..num {
v.push(uniform.sample(&mut rng));
}
v
};
let x_data = [
"Carmelo<br>Anthony",
"Dwyane<br>Wade",
"Deron<br>Williams",
"Brook<br>Lopez",
"Damian<br>Lillard",
"David<br>West",
"Blake<br>Griffin",
"David<br>Lee",
"Demar<br>Derozan",
];
let y_data = vec![
rnd_sample(30, 10.0),
rnd_sample(30, 20.0),
rnd_sample(30, 25.0),
rnd_sample(30, 40.0),
rnd_sample(30, 45.0),
rnd_sample(30, 30.0),
rnd_sample(30, 20.0),
rnd_sample(30, 15.0),
rnd_sample(30, 43.0),
];
let mut plot = Plot::new();
let layout = Layout::new()
.title("Points Scored by the Top 9 Scoring NBA Players in 2012")
.y_axis(
Axis::new()
.auto_range(true)
.show_grid(true)
.zero_line(true)
.dtick(5.0)
.grid_color(Rgb::new(255, 255, 255))
.grid_width(1)
.zero_line_color(Rgb::new(255, 255, 255))
.zero_line_width(2),
)
.margin(Margin::new().left(40).right(30).bottom(80).top(100))
.paper_background_color(Rgb::new(243, 243, 243))
.plot_background_color(Rgb::new(243, 243, 243))
.show_legend(false);
plot.set_layout(layout);
for index in 0..x_data.len() {
let trace = BoxPlot::new(y_data[index].clone())
.name(x_data[index])
.box_points(BoxPoints::All)
.jitter(0.5)
.whisker_width(0.2)
.marker(Marker::new().size(6))
.line(Line::new().width(2.0));
plot.add_trace(trace);
}
if show {
plot.show();
}
plot
}
// ANCHOR_END: fully_styled_box_plot
// Histograms
fn sample_normal_distribution(n: usize, mean: f64, std_dev: f64) -> Vec<f64> {
let mut rng = rand::rng();
let dist = Normal::new(mean, std_dev).unwrap();
let mut v = Vec::<f64>::with_capacity(n);
for _idx in 1..n {
v.push(dist.sample(&mut rng));
}
v
}
fn sample_uniform_distribution(n: usize, lb: f64, ub: f64) -> Vec<f64> {
let mut rng = rand::rng();
let dist = Uniform::new(lb, ub).unwrap();
let mut v = Vec::<f64>::with_capacity(n);
for _idx in 1..n {
v.push(dist.sample(&mut rng));
}
v
}
// ANCHOR: basic_histogram
fn basic_histogram(show: bool) -> Plot {
let samples = sample_normal_distribution(10_000, 0.0, 1.0);
let trace = Histogram::new(samples).name("h");
let mut plot = Plot::new();
plot.add_trace(trace);
if show {
plot.show();
}
plot
}
// ANCHOR_END: basic_histogram
// ANCHOR: horizontal_histogram
fn horizontal_histogram(show: bool) -> Plot {
let samples = sample_normal_distribution(10_000, 0.0, 1.0);
let trace = Histogram::new_vertical(samples)
.name("h")
.marker(Marker::new().color(NamedColor::Pink));
let mut plot = Plot::new();
plot.add_trace(trace);
if show {
plot.show();
}
plot
}
// ANCHOR_END: horizontal_histogram
// ANCHOR: overlaid_histogram
fn overlaid_histogram(show: bool) -> Plot {
let samples1 = sample_normal_distribution(500, 0.0, 1.0);
let trace1 = Histogram::new(samples1)
.name("trace 1")
.opacity(0.5)
.marker(Marker::new().color(NamedColor::Green));
let samples2 = sample_normal_distribution(500, 0.0, 1.0);
let trace2 = Histogram::new(samples2)
.name("trace 2")
.opacity(0.6)
.marker(Marker::new().color(NamedColor::Red));
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
let layout = Layout::new().bar_mode(BarMode::Overlay);
plot.set_layout(layout);
if show {
plot.show();
}
plot
}
// ANCHOR_END: overlaid_histogram
// ANCHOR: stacked_histograms
fn stacked_histograms(show: bool) -> Plot {
let samples1 = sample_normal_distribution(500, 0.0, 1.0);
let trace1 = Histogram::new(samples1)
.name("trace 1")
.opacity(0.5)
.marker(Marker::new().color(NamedColor::Green));
let samples2 = sample_normal_distribution(500, 0.0, 1.0);
let trace2 = Histogram::new(samples2)
.name("trace 2")
.opacity(0.6)
.marker(Marker::new().color(NamedColor::Red));
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
let layout = Layout::new().bar_mode(BarMode::Stack);
plot.set_layout(layout);
if show {
plot.show();
}
plot
}
// ANCHOR_END: stacked_histograms
// ANCHOR: colored_and_styled_histograms
fn colored_and_styled_histograms(show: bool) -> Plot {
let n = 500;
let x1 = sample_uniform_distribution(n, 0.0, 5.0);
let x2 = sample_uniform_distribution(n, 0.0, 10.0);
let y1 = sample_uniform_distribution(n, 0.0, 1.0);
let y2 = sample_uniform_distribution(n, 0.0, 2.0);
let trace1 = Histogram::new_xy(x1, y1)
.name("control")
.hist_func(HistFunc::Count)
.marker(
Marker::new()
.color(Rgba::new(255, 100, 102, 0.7))
.line(Line::new().color(Rgba::new(255, 100, 102, 1.0)).width(1.0)),
)
.opacity(0.5)
.auto_bin_x(false)
.x_bins(Bins::new(0.5, 2.8, 0.06));
let trace2 = Histogram::new_xy(x2, y2)
.name("experimental")
.hist_func(HistFunc::Count)
.marker(
Marker::new()
.color(Rgba::new(100, 200, 102, 0.7))
.line(Line::new().color(Rgba::new(100, 200, 102, 1.0)).width(1.0)),
)
.opacity(0.75)
.auto_bin_x(false)
.x_bins(Bins::new(-3.2, 4.0, 0.06));
let layout = Layout::new()
.title("Colored and Styled Histograms")
.x_axis(Axis::new().title("Value"))
.y_axis(Axis::new().title("Count"))
.bar_mode(BarMode::Overlay)
.bar_gap(0.05)
.bar_group_gap(0.2);
let mut plot = Plot::new();
plot.set_layout(layout);
plot.add_trace(trace1);
plot.add_trace(trace2);
if show {
plot.show();
}
plot
}
// ANCHOR_END: colored_and_styled_histograms
// ANCHOR: cumulative_histogram
fn cumulative_histogram(show: bool) -> Plot {
let n = 500;
let x = sample_uniform_distribution(n, 0.0, 1.0);
let trace = Histogram::new(x)
.cumulative(Cumulative::new().enabled(true))
.marker(Marker::new().color(NamedColor::BurlyWood));
let mut plot = Plot::new();
plot.add_trace(trace);
if show {
plot.show();
}
plot
}
// ANCHOR_END: cumulative_histogram
// ANCHOR: normalized_histogram
fn normalized_histogram(show: bool) -> Plot {
let n = 500;
let x = sample_uniform_distribution(n, 0.0, 1.0);
let trace = Histogram::new(x)
.hist_norm(HistNorm::Probability)
.marker(Marker::new().color(NamedColor::SeaGreen));
let mut plot = Plot::new();
plot.add_trace(trace);
if show {
plot.show();
}
plot
}
// ANCHOR_END: normalized_histogram
// ANCHOR: specify_binning_function
fn specify_binning_function(show: bool) -> Plot {
let x = vec!["Apples", "Apples", "Apples", "Oranges", "Bananas"];
let y = vec!["5", "10", "3", "10", "5"];
let trace1 = Histogram::new_xy(x.clone(), y.clone())
.name("count")
.hist_func(HistFunc::Count);
let trace2 = Histogram::new_xy(x.clone(), y.clone())
.name("sum")
.hist_func(HistFunc::Sum);
let mut plot = Plot::new();
plot.add_trace(trace1);
plot.add_trace(trace2);
if show {
plot.show();
}
plot
}
// ANCHOR_END: specify_binning_function
fn write_example_to_html(plot: Plot, name: &str) {
std::fs::create_dir_all("./out").unwrap();
let html = plot.to_inline_html(Some(name));
std::fs::write(format!("./out/{}.html", name), html).unwrap();
}
fn main() {
// Change false to true on any of these lines to display the example.
// Error Bars
write_example_to_html(
basic_symmetric_error_bars(false),
"basic_symmetric_error_bars",
);
write_example_to_html(asymmetric_error_bars(false), "asymmetric_error_bars");
write_example_to_html(
error_bars_as_a_percentage_of_the_y_value(false),
"error_bars_as_a_percentage_of_the_y_value",
);
write_example_to_html(
asymmetric_error_bars_with_a_constant_offset(false),
"asymmetric_error_bars_with_a_constant_offset",
);
write_example_to_html(horizontal_error_bars(false), "horizontal_error_bars");
write_example_to_html(
bar_chart_with_error_bars(false),
"bar_chart_with_error_bars",
);
write_example_to_html(
colored_and_styled_error_bars(false),
"colored_and_styled_error_bars",
);
// Box Plots
write_example_to_html(basic_box_plot(false), "basic_box_plot");
write_example_to_html(
box_plot_that_displays_the_underlying_data(false),
"box_plot_that_displays_the_underlying_data",
);
write_example_to_html(horizontal_box_plot(false), "horizontal_box_plot");
write_example_to_html(grouped_box_plot(false), "grouped_box_plot");
write_example_to_html(
box_plot_styling_outliers(false),
"box_plot_styling_outliers",
);
write_example_to_html(
box_plot_styling_mean_and_standard_deviation(false),
"box_plot_styling_mean_and_standard_deviation",
);
write_example_to_html(
grouped_horizontal_box_plot(false),
"grouped_horizontal_box_plot",
);
write_example_to_html(fully_styled_box_plot(false), "fully_styled_box_plot");
// Histograms
write_example_to_html(basic_histogram(false), "basic_histogram");
write_example_to_html(horizontal_histogram(false), "horizontal_histogram");
write_example_to_html(overlaid_histogram(false), "overlaid_histogram");
write_example_to_html(stacked_histograms(false), "stacked_histograms");
write_example_to_html(
colored_and_styled_histograms(false),
"colored_and_styled_histograms",
);
write_example_to_html(cumulative_histogram(false), "cumulative_histogram");
write_example_to_html(normalized_histogram(false), "normalized_histogram");
write_example_to_html(specify_binning_function(false), "specify_binning_function");
}