Error Bars
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 Symmetric Error Bars
#![allow(unused)] fn main() { fn basic_symmetric_error_bars(show: bool) { 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(); } println!( "{}", plot.to_inline_html(Some("basic_symmetric_error_bars")) ); } }
Asymmetric Error Bars
#![allow(unused)] fn main() { fn asymmetric_error_bars(show: bool) { 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(); } println!("{}", plot.to_inline_html(Some("asymmetric_error_bars"))); } }
Error Bars as a Percentage of the Y Value
#![allow(unused)] fn main() { fn error_bars_as_a_percentage_of_the_y_value(show: bool) { 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(); } println!( "{}", plot.to_inline_html(Some("error_bars_as_a_percentage_of_the_y_value")) ); } }
Asymmetric Error Bars with a Constant Offset
#![allow(unused)] fn main() { fn asymmetric_error_bars_with_a_constant_offset(show: bool) { 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(); } println!( "{}", plot.to_inline_html(Some("asymmetric_error_bars_with_a_constant_offset")) ); } }
Horizontal Error Bars
#![allow(unused)] fn main() { fn horizontal_error_bars(show: bool) { 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(); } println!("{}", plot.to_inline_html(Some("horizontal_error_bars"))); } }
Bar Chart with Error Bars
#![allow(unused)] fn main() { fn bar_chart_with_error_bars(show: bool) { 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(); } println!("{}", plot.to_inline_html(Some("bar_chart_with_error_bars"))); } }
Colored and Styled Error Bars
#![allow(unused)] fn main() { fn colored_and_styled_error_bars(show: bool) { let x_theo: Vec<f64> = linspace(-4., 4., 100).collect(); 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(); } println!( "{}", plot.to_inline_html(Some("colored_and_styled_error_bars")) ); } }