9 releases

0.2.4 Jul 9, 2018
0.2.3 Apr 15, 2018
0.2.2 Mar 25, 2018
0.2.1 Feb 24, 2018
0.1.0 Feb 26, 2016

#1 in Visualization

Download history 579/week @ 2018-05-27 1018/week @ 2018-06-03 766/week @ 2018-06-10 905/week @ 2018-06-17 1523/week @ 2018-06-24 1194/week @ 2018-07-01 1052/week @ 2018-07-08 1407/week @ 2018-07-15 1778/week @ 2018-07-22 1454/week @ 2018-07-29 1396/week @ 2018-08-05 1567/week @ 2018-08-12 1880/week @ 2018-08-19

4,605 downloads per month

This is the graphing subcrate of Criterion.rs. It is considered an implementation detail and is not stable. Anything may change at any time with no warning, including the public API. For further information, see the main repository.


lib.rs:

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

extern crate itertools_num;
extern crate criterion_plot as plot;

# use std::fs;
# use std::path::Path;
use itertools_num::linspace;
use plot::prelude::*;

# fn main() {
# if let Err(_) = plot::version() {
#     return;
# }
let ref xs = linspace::<f64>(-10., 10., 51).collect::<Vec<_>>();

# fs::create_dir_all(Path::new("target/doc/criterion_plot")).unwrap();
# assert_eq!(Some(String::new()),
Figure::new()
#   .set(Font("Helvetica"))
#   .set(FontSize(12.))
#   .set(Output(Path::new("target/doc/criterion_plot/curve.svg")))
#   .set(Size(1280, 720))
    .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)
#   .ok()
#   .and_then(|gnuplot| {
#       gnuplot.wait_with_output().ok().and_then(|p| String::from_utf8(p.stderr).ok())
#   }));
# }

Plot

extern crate itertools_num;
extern crate rand;
extern crate criterion_plot as plot;

# use std::fs;
# use std::path::Path;
use std::f64::consts::PI;

use itertools_num::linspace;
use rand::{Rng, XorShiftRng};
use plot::prelude::*;

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

# fn main() {
# if let Err(_) = plot::version() {
#     return;
# }
let ref xs_ = linspace::<f64>(-4., 4., 101).collect::<Vec<_>>();

// Fake some data
let ref mut rng: XorShiftRng = rand::thread_rng().gen();
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);

# fs::create_dir_all(Path::new("target/doc/criterion_plot")).unwrap();
# assert_eq!(Some(String::new()),
Figure::new()
#   .set(Font("Helvetica"))
#   .set(FontSize(12.))
#   .set(Output(Path::new("target/doc/criterion_plot/error_bar.svg")))
#   .set(Size(1280, 720))
    .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)
#   .ok()
#   .and_then(|gnuplot| {
#       gnuplot.wait_with_output().ok().and_then(|p| String::from_utf8(p.stderr).ok())
#   }));
# }

Plot

extern crate rand;
extern crate criterion_plot as plot;

# use std::fs;
# use std::path::Path;
use plot::prelude::*;
use rand::Rng;

# fn main() {
# if let Err(_) = plot::version() {
#     return;
# }
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<_>>();

# fs::create_dir_all(Path::new("target/doc/criterion_plot")).unwrap();
# assert_eq!(Some(String::new()),
Figure::new()
#   .set(Font("Helvetica"))
#   .set(FontSize(12.))
#   .set(Output(Path::new("target/doc/criterion_plot/candlesticks.svg")))
#   .set(Size(1280, 720))
    .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)
#   .ok()
#   .and_then(|gnuplot| {
#       gnuplot.wait_with_output().ok().and_then(|p| String::from_utf8(p.stderr).ok())
#   }));
# }

Plot

extern crate itertools_num;
extern crate num_complex;
extern crate criterion_plot as plot;

# use std::fs;
# use std::path::Path;
use std::f64::consts::PI;

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

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

# fn main() {
# if let Err(_) = plot::version() {
#     return;
# }
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());

# fs::create_dir_all(Path::new("target/doc/criterion_plot")).unwrap();
# assert_eq!(Some(String::new()),
Figure::new().
#   set(Font("Helvetica")).
#   set(FontSize(12.)).
#   set(Output(Path::new("target/doc/criterion_plot/multiaxis.svg"))).
#   set(Size(1280, 720)).
    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)
#   ok().and_then(|gnuplot| {
#       gnuplot.wait_with_output().ok().and_then(|p| {
#           String::from_utf8(p.stderr).ok()
#       })
#   }));
# }

Plot

extern crate itertools_num;
extern crate criterion_plot as plot;

# use std::fs;
# use std::path::Path;
use std::f64::consts::PI;
use std::iter;

use itertools_num::linspace;
use plot::prelude::*;

# fn main() {
# if let Err(_) = plot::version() {
#     return;
# }
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())
}

# fs::create_dir_all(Path::new("target/doc/criterion_plot")).unwrap();
# assert_eq!(Some(String::new()),
Figure::new()
#   .set(Font("Helvetica"))
#   .set(FontSize(12.))
#   .set(Output(Path::new("target/doc/criterion_plot/filled_curve.svg")))
#   .set(Size(1280, 720))
    .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())
    }));
# }
MIT/Apache-2.0 license

Dependencies

Reverse deps