Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Introduction

The plotting crate provides simple interfaces for rapidly generating plots.

2D Plots

Quick 2D plot

use plotting::{Figure, quick_plot_2d};
use std::path::Path;

fn main() {
    // Create the figure.
    let fig: Figure = quick_plot_2d([1.0, 2.0, 3.0], [1.0, 4.0, 9.0]);

    // Save the figure so it can be displayed right below this example.
    fig.save_inline_html(Path::new("book/src/figures/quick_plot_2d.html"));

    // Alternatively, you can show the figure in a web browser.
    // fig.show();
}

Quick 2D plot with labels

use plotting::{Figure, quick_plot_2d_with_labels};
use std::path::Path;

fn main() {
    // Create the figure.
    let fig: Figure =
        quick_plot_2d_with_labels([1.0, 2.0, 3.0], [1.0, 4.0, 9.0], "x", "y", "y = x^2");

    // Save the figure so it can be displayed right below this example.
    fig.save_inline_html(Path::new("book/src/figures/quick_plot_2d_with_labels.html"));

    // Alternatively, you can show the figure in a web browser.
    // fig.show();
}

General 2D plotting

use plotting::{Color, Figure, Format, FormatBuilder, LineStyle, NamedColor, Trace};
use std::path::Path;

fn main() {
    // Define the traces.
    let trace_1 = Trace::new_2d([1.0, 2.0, 3.0], [1.0, 2.0, 3.0])
        .name("y = x")
        .line_color(Color::named(NamedColor::Red))
        .line_width(2.0)
        .line_style(LineStyle::Dash);
    let trace_2 = Trace::new_2d([1.0, 2.0, 3.0], [1.0, 4.0, 9.0])
        .name("y = x^2")
        .line_color(Color::named(NamedColor::Blue))
        .line_width(2.0)
        .line_style(LineStyle::Dot);

    // Figure formatting.
    let format: Format = FormatBuilder::default()
        .title("y vs. x")
        .x_label("x")
        .y_label("y")
        .width(800)
        .height(600)
        .build()
        .unwrap();

    // Create the figure.
    let fig = Figure::new(vec![trace_1, trace_2], format);

    // Save the figure so it can be displayed right below this example.
    fig.save_inline_html(Path::new("book/src/figures/general_2d_plot.html"));

    // Alternatively, you can show the figure in a web browser.
    // fig.show();
}

3D Plots

Quick 3D plot

use plotting::{Figure, quick_plot_3d};
use std::path::Path;

fn main() {
    // Create the figure.
    let fig: Figure = quick_plot_3d([1.0, 2.0, 10.0], [1.0, 4.0, 9.0], [2.0, 5.0, 10.0]);

    // Save the figure so it can be displayed right below this example.
    fig.save_inline_html(Path::new("book/src/figures/quick_plot_3d.html"));

    // Alternatively, you can show the figure in a web browser.
    // fig.show();
}

Quick 3D plot with labels

use plotting::{Figure, quick_plot_3d_with_labels};
use std::path::Path;

fn main() {
    // Create the figure.
    let fig: Figure = quick_plot_3d_with_labels(
        [1.0, 2.0, 10.0],
        [1.0, 4.0, 9.0],
        [2.0, 5.0, 10.0],
        "x",
        "y",
        "z",
        "z vs. x and y",
    );

    // Save the figure so it can be displayed right below this example.
    fig.save_inline_html(Path::new("book/src/figures/quick_plot_3d_with_labels.html"));

    // Alternatively, you can show the figure in a web browser.
    // fig.show();
}

General 3D plotting

use plotting::{Color, Figure, Format, FormatBuilder, LineStyle, NamedColor, Trace};
use std::path::Path;

fn main() {
    // Define the traces.
    let trace_1 = Trace::new_3d([1.0, 2.0, 5.0], [1.0, 2.0, 3.0], [1.0, 2.0, 4.0])
        .name("Trace 1")
        .line_color(Color::named(NamedColor::Red))
        .line_width(2.0)
        .line_style(LineStyle::Dash);
    let trace_2 = Trace::new_3d([1.0, 2.0, 5.0], [3.0, 2.0, 1.0], [1.0, 2.0, 4.0])
        .name("Trace 2")
        .line_color(Color::named(NamedColor::Blue))
        .line_width(2.0)
        .line_style(LineStyle::Dot);

    // Figure formatting.
    let format: Format = FormatBuilder::default()
        .title("z vs. x and y")
        .x_label("x")
        .y_label("y")
        .z_label("z")
        .width(800)
        .height(600)
        .build()
        .unwrap();

    // Create the figure.
    let fig = Figure::new(vec![trace_1, trace_2], format);

    // Save the figure so it can be displayed right below this example.
    fig.save_inline_html(Path::new("book/src/figures/general_3d_plot.html"));

    // Alternatively, you can show the figure in a web browser.
    // fig.show();
}