Crate criterion_plot

Source
Expand description

Criterion’s plotting library.

WARNING This library is criterion’s implementation detail and there no plans to stabilize it. In other words, the API may break at any time without notice.

§Examples

Plot

use itertools_num::linspace;
use criterion_plot::prelude::*;

let ref xs = linspace::<f64>(-10., 10., 51).collect::<Vec<_>>();

Figure::new()
    .configure(Key, |k| {
        k.set(Boxed::Yes)
         .set(Position::Inside(Vertical::Top, Horizontal::Left))
    })
    .plot(LinesPoints {
              x: xs,
              y: xs.iter().map(|x| x.sin()),
          },
          |lp| {
              lp.set(Color::DarkViolet)
                .set(Label("sin(x)"))
                .set(LineType::Dash)
                .set(PointSize(1.5))
                .set(PointType::Circle)
          })
    .plot(Steps {
              x: xs,
              y: xs.iter().map(|x| x.atan()),
          },
          |s| {
              s.set(Color::Rgb(0, 158, 115))
               .set(Label("atan(x)"))
               .set(LineWidth(2.))
          })
    .plot(Impulses {
              x: xs,
              y: xs.iter().map(|x| x.atan().cos()),
          },
          |i| {
              i.set(Color::Rgb(86, 180, 233))
               .set(Label("cos(atan(x))"))
          })
    .draw()  // (rest of the chain has been omitted)

Plot

use std::f64::consts::PI;

use itertools_num::linspace;
use rand::Rng;
use criterion_plot::prelude::*;

fn sinc(mut x: f64) -> f64 {
    if x == 0. {
        1.
    } else {
        x *= PI;
        x.sin() / x
    }
}

let ref xs_ = linspace::<f64>(-4., 4., 101).collect::<Vec<_>>();

// Fake some data
let ref mut rng = rand::thread_rng();
let xs = linspace::<f64>(-4., 4., 13).skip(1).take(11);
let ys = xs.map(|x| sinc(x) + 0.05 * rng.gen::<f64>() - 0.025).collect::<Vec<_>>();
let y_low = ys.iter().map(|&y| y - 0.025 - 0.075 * rng.gen::<f64>()).collect::<Vec<_>>();
let y_high = ys.iter().map(|&y| y + 0.025 + 0.075 * rng.gen::<f64>()).collect::<Vec<_>>();
let xs = linspace::<f64>(-4., 4., 13).skip(1).take(11);
let xs = xs.map(|x| x + 0.2 * rng.gen::<f64>() - 0.1);

Figure::new()
    .configure(Axis::BottomX, |a| {
        a.set(TicLabels {
            labels: &["-π", "0", "π"],
            positions: &[-PI, 0., PI],
        })
    })
    .configure(Key,
               |k| k.set(Position::Outside(Vertical::Top, Horizontal::Right)))
    .plot(Lines {
              x: xs_,
              y: xs_.iter().cloned().map(sinc),
          },
          |l| {
              l.set(Color::Rgb(0, 158, 115))
               .set(Label("sinc(x)"))
               .set(LineWidth(2.))
          })
    .plot(YErrorBars {
              x: xs,
              y: &ys,
              y_low: &y_low,
              y_high: &y_high,
          },
          |eb| {
              eb.set(Color::DarkViolet)
                .set(LineWidth(2.))
                .set(PointType::FilledCircle)
                .set(Label("measured"))
          })
    .draw()  // (rest of the chain has been omitted)

Plot

use criterion_plot::prelude::*;
use rand::Rng;

let xs = 1..11;

// Fake some data
let mut rng = rand::thread_rng();
let bh = xs.clone().map(|_| 5f64 + 2.5 * rng.gen::<f64>()).collect::<Vec<_>>();
let bm = xs.clone().map(|_| 2.5f64 + 2.5 * rng.gen::<f64>()).collect::<Vec<_>>();
let wh = bh.iter().map(|&y| y + (10. - y) * rng.gen::<f64>()).collect::<Vec<_>>();
let wm = bm.iter().map(|&y| y * rng.gen::<f64>()).collect::<Vec<_>>();
let m = bm.iter().zip(bh.iter()).map(|(&l, &h)| (h - l) * rng.gen::<f64>() + l)
    .collect::<Vec<_>>();

Figure::new()
    .set(BoxWidth(0.2))
    .configure(Axis::BottomX, |a| a.set(Range::Limits(0., 11.)))
    .plot(Candlesticks {
              x: xs.clone(),
              whisker_min: &wm,
              box_min: &bm,
              box_high: &bh,
              whisker_high: &wh,
          },
          |cs| {
              cs.set(Color::Rgb(86, 180, 233))
                .set(Label("Quartiles"))
                .set(LineWidth(2.))
          })
    // trick to plot the median
    .plot(Candlesticks {
              x: xs,
              whisker_min: &m,
              box_min: &m,
              box_high: &m,
              whisker_high: &m,
          },
          |cs| {
              cs.set(Color::Black)
                .set(LineWidth(2.))
          })
    .draw()  // (rest of the chain has been omitted)

Plot

use std::f64::consts::PI;

use itertools_num::linspace;
use num_complex::Complex;
use criterion_plot::prelude::*;

fn tf(x: f64) -> Complex<f64> {
    Complex::new(0., x) / Complex::new(10., x) / Complex::new(1., x / 10_000.)
}

let (start, end): (f64, f64) = (1.1, 90_000.);
let ref xs = linspace(start.ln(), end.ln(), 101).map(|x| x.exp()).collect::<Vec<_>>();
let phase = xs.iter().map(|&x| tf(x).arg() * 180. / PI);
let magnitude = xs.iter().map(|&x| tf(x).norm());

Figure::new().
    set(Title("Frequency response")).
    configure(Axis::BottomX, |a| a.
        configure(Grid::Major, |g| g.
            show()).
        set(Label("Angular frequency (rad/s)")).
        set(Range::Limits(start, end)).
        set(Scale::Logarithmic)).
    configure(Axis::LeftY, |a| a.
        set(Label("Gain")).
        set(Scale::Logarithmic)).
    configure(Axis::RightY, |a| a.
        configure(Grid::Major, |g| g.
            show()).
        set(Label("Phase shift (°)"))).
    configure(Key, |k| k.
        set(Position::Inside(Vertical::Top, Horizontal::Center)).
        set(Title(" "))).
    plot(Lines {
        x: xs,
        y: magnitude,
    }, |l| l.
        set(Color::DarkViolet).
        set(Label("Magnitude")).
        set(LineWidth(2.))).
    plot(Lines {
        x: xs,
        y: phase,
    }, |l| l.
        set(Axes::BottomXRightY).
        set(Color::Rgb(0, 158, 115)).
        set(Label("Phase")).
        set(LineWidth(2.))).
    draw().  // (rest of the chain has been omitted)

Plot

use std::f64::consts::PI;
use std::iter;

use itertools_num::linspace;
use criterion_plot::prelude::*;

let (start, end) = (-5., 5.);
let ref xs = linspace(start, end, 101).collect::<Vec<_>>();
let zeros = iter::repeat(0);

fn gaussian(x: f64, mu: f64, sigma: f64) -> f64 {
    1. / (((x - mu).powi(2) / 2. / sigma.powi(2)).exp() * sigma * (2. * PI).sqrt())
}

Figure::new()
    .set(Title("Transparent filled curve"))
    .configure(Axis::BottomX, |a| a.set(Range::Limits(start, end)))
    .configure(Axis::LeftY, |a| a.set(Range::Limits(0., 1.)))
    .configure(Key, |k| {
        k.set(Justification::Left)
         .set(Order::SampleText)
         .set(Position::Inside(Vertical::Top, Horizontal::Left))
         .set(Title("Gaussian Distribution"))
    })
    .plot(FilledCurve {
              x: xs,
              y1: xs.iter().map(|&x| gaussian(x, 0.5, 0.5)),
              y2: zeros.clone(),
          },
          |fc| {
              fc.set(Color::ForestGreen)
                .set(Label("μ = 0.5 σ = 0.5"))
          })
    .plot(FilledCurve {
              x: xs,
              y1: xs.iter().map(|&x| gaussian(x, 2.0, 1.0)),
              y2: zeros.clone(),
          },
          |fc| {
              fc.set(Color::Gold)
                .set(Label("μ = 2.0 σ = 1.0"))
                .set(Opacity(0.5))
          })
    .plot(FilledCurve {
              x: xs,
              y1: xs.iter().map(|&x| gaussian(x, -1.0, 2.0)),
              y2: zeros,
          },
          |fc| {
              fc.set(Color::Red)
                .set(Label("μ = -1.0 σ = 2.0"))
                .set(Opacity(0.5))
          })
    .draw()
    .ok()
    .and_then(|gnuplot| {
        gnuplot.wait_with_output().ok().and_then(|p| String::from_utf8(p.stderr).ok())
    }));

Modules§

Structs§

Enums§

Functions§