Crate ploteria

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

extern crate itertools_num;
extern crate ploteria as plot;

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

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

Figure::new()
    .configure_key(|k| {
        k.boxed(true)
         .position(Position::Inside(Vertical::Top, Horizontal::Left))
    })
    .plot(LinesPoints {
              x: xs,
              y: xs.iter().map(|x| x.sin()),
          },
          |lp| {
              lp.color(Color::DarkViolet)
                .label("sin(x)")
                .line_type(LineType::Dash)
                .point_size(1.5)
                .point_type(PointType::Circle)
          })
    .plot(Steps {
              x: xs,
              y: xs.iter().map(|x| x.atan()),
          },
          |s| {
              s.color(Color::Rgb(0, 158, 115))
               .label("atan(x)")
               .line_width(2.)
          })
    .plot(Impulses {
              x: xs,
              y: xs.iter().map(|x| x.atan().cos()),
          },
          |i| {
              i.color(Color::Rgb(86, 180, 233))
               .label("cos(atan(x))")
          })
    .draw()  // (rest of the chain has been omitted)

Plot

extern crate itertools_num;
extern crate rand;
extern crate ploteria as plot;

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
    }
}

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);

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

Plot

extern crate rand;
extern crate ploteria as plot;

use 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()
    .box_width(0.2)
    .configure_axis(Axis::BottomX, |a| a.range(Range::Limits(0., 11.)))
    .plot(Candlesticks {
              x: xs.clone(),
              whisker_min: &wm,
              box_min: &bm,
              box_high: &bh,
              whisker_high: &wh,
          },
          |cs| {
              cs.color(Color::Rgb(86, 180, 233))
                .label("Quartiles")
                .line_width(2.)
          })
    // trick to plot the median
    .plot(Candlesticks {
              x: xs,
              whisker_min: &m,
              box_min: &m,
              box_high: &m,
              whisker_high: &m,
          },
          |cs| {
              cs.color(Color::Black)
                .line_width(2.)
          })
    .draw()  // (rest of the chain has been omitted)

Plot

extern crate itertools_num;
extern crate num_complex;
extern crate ploteria as plot;

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.)
}

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()
    .title("Frequency response")
    .configure_axis(Axis::BottomX, |a| a
        .configure_major_grid(|g| g.show())
        .label("Angular frequency (rad/s)")
        .range(Range::Limits(start, end))
        .scale(Scale::Logarithmic))
    .configure_axis(Axis::LeftY, |a| a
        .label("Gain")
        .scale(Scale::Logarithmic))
    .configure_axis(Axis::RightY, |a| a
        .configure_major_grid(|g| g.show())
        .label("Phase shift (°)"))
    .configure_key(|k| k
        .position(Position::Inside(Vertical::Top, Horizontal::Center))
        .title(" "))
    .plot(Lines {
        x: xs,
        y: magnitude,
    }, |l| l
        .color(Color::DarkViolet)
        .label("Magnitude")
        .line_width(2.))
    .plot(Lines {
        x: xs,
        y: phase,
    }, |l| l
        .axes(Axes::BottomXRightY)
        .color(Color::Rgb(0, 158, 115))
        .label("Phase")
        .line_width(2.))
    .draw()  // (rest of the chain has been omitted)

Plot

extern crate itertools_num;
extern crate ploteria as plot;

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

use itertools_num::linspace;
use 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()
    .title("Transparent filled curve")
    .configure_axis(Axis::BottomX, |a| a.range(Range::Limits(start, end)))
    .configure_axis(Axis::LeftY, |a| a.range(Range::Limits(0., 1.)))
    .configure_key(|k| {
        k.justification(Justification::Left)
         .order(Order::SampleText)
         .position(Position::Inside(Vertical::Top, Horizontal::Left))
         .title("Gaussian Distribution")
    })
    .plot(FilledCurve {
              x: xs,
              y1: xs.iter().map(|&x| gaussian(x, 0.5, 0.5)),
              y2: zeros.clone(),
          },
          |fc| {
              fc.color(Color::ForestGreen)
                .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.color(Color::Gold)
                .label("μ = 2.0 σ = 1.0")
                .opacity(0.5)
          })
    .plot(FilledCurve {
              x: xs,
              y1: xs.iter().map(|&x| gaussian(x, -1.0, 2.0)),
              y2: zeros,
          },
          |fc| {
              fc.color(Color::Red)
                .label("μ = -1.0 σ = 2.0")
                .opacity(0.5)
          })
    .draw()
    .ok()
    .and_then(|gnuplot| {
        gnuplot.wait_with_output().ok().and_then(|p| String::from_utf8(p.stderr).ok())
    }));

Modules§

axis
Coordinate axis
candlestick
“Candlestick” plots
curve
Simple “curve” like plots
errorbar
Error bar plots
filledcurve
Filled curve plots
key
Key (or legend)
prelude
A collection of the most used traits, structs and enums
traits
Traits

Structs§

Figure
Plot container
Version
Structure representing a gnuplot version number.

Enums§

Color
Color
LineType
Line type
PointType
Point type
Terminal
Output terminal
VersionError
Possible errors when parsing gnuplot’s version string

Functions§

version
Returns gnuplot version