Histograms
The following imports have been used to produce the plots below:
#![allow(unused)] fn main() { use itertools_num::linspace; use plotly::box_plot::{BoxMean, BoxPoints}; use plotly::common::{ErrorData, ErrorType, Line, Marker, Mode, Orientation, Title}; use plotly::histogram::{Bins, Cumulative, HistFunc, HistNorm}; use plotly::layout::{Axis, BarMode, BoxMode, Layout, Margin}; use plotly::{Bar, BoxPlot, Histogram, Plot, color::{NamedColor, Rgb, Rgba}, Scatter}; use rand_distr::{Distribution, Normal, Uniform}; }
The to_inline_html
method is used to produce the html plot displayed in this page.
Basic Histogram
#![allow(unused)] fn main() { fn basic_histogram(show: bool) { 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(); } println!( "{}", plot.to_inline_html(Some("basic_histogram")) ); } }
Horizontal Histogram
#![allow(unused)] fn main() { fn horizontal_histogram(show: bool) { let samples = sample_normal_distribution(10_000, 0.0, 1.0); let trace = Histogram::new_horizontal(samples) .name("h") .marker(Marker::new().color(NamedColor::Pink)); let mut plot = Plot::new(); plot.add_trace(trace); if show { plot.show(); } println!( "{}", plot.to_inline_html(Some("horizontal_histogram")) ); } }
Overlaid Histogram
#![allow(unused)] fn main() { fn overlaid_histogram(show: bool) { 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(); } println!( "{}", plot.to_inline_html(Some("overlaid_histogram")) ); } }
Stacked Histograms
#![allow(unused)] fn main() { fn stacked_histograms(show: bool) { 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(); } println!( "{}", plot.to_inline_html(Some("stacked_histograms")) ); } }
Colored and Styled Histograms
#![allow(unused)] fn main() { fn colored_and_styled_histograms(show: bool) { 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(Title::with_text("Sampled Results")) .x_axis(Axis::new().title(Title::with_text("Value"))) .y_axis(Axis::new().title(Title::with_text("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(); } println!( "{}", plot.to_inline_html(Some("colored_and_styled_histograms")) ); } }
Cumulative Histogram
#![allow(unused)] fn main() { fn cumulative_histogram(show: bool) { 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(); } println!( "{}", plot.to_inline_html(Some("cumulative_histogram")) ); } }
Normalized Histogram
#![allow(unused)] fn main() { fn normalized_histogram(show: bool) { 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(); } println!( "{}", plot.to_inline_html(Some("normalized_histogram")) ); } }
Specify Binning Function
#![allow(unused)] fn main() { fn specify_binning_function(show: bool) { let x = vec!["Apples", "Apples", "Apples", "Organges", "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(); } println!( "{}", plot.to_inline_html(Some("specify_binning_function")) ); } }