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

Sankey Diagrams

The following imports have been used to produce the plots below:

#![allow(unused)]
fn main() {
use ndarray::Array;
use plotly::common::{
    ColorScale, ColorScalePalette, DashType, Fill, Font, Line, LineShape, Marker, Mode, Title,
};
use plotly::layout::{Axis, BarMode, Layout, Legend, TicksDirection};
use plotly::Sankey;
use rand_distr::{Distribution, Normal, Uniform};
}

The to_inline_html method is used to produce the html plot displayed in this page.

Constructing a basic Sankey diagram

#![allow(unused)]
fn main() {
fn basic_sankey_diagram(show: bool, file_name: &str) {
    // https://plotly.com/javascript/sankey-diagram/#basic-sankey-diagram
    let trace = Sankey::new()
        .orientation(Orientation::Horizontal)
        .node(
            Node::new()
                .pad(15)
                .thickness(30)
                .line(SankeyLine::new().color(NamedColor::Black).width(0.5))
                .label(vec!["A1", "A2", "B1", "B2", "C1", "C2"])
                .color_array(vec![
                    NamedColor::Blue,
                    NamedColor::Blue,
                    NamedColor::Blue,
                    NamedColor::Blue,
                    NamedColor::Blue,
                    NamedColor::Blue,
                ]),
        )
        .link(
            Link::new()
                .value(vec![8, 4, 2, 8, 4, 2])
                .source(vec![0, 1, 0, 2, 3, 3])
                .target(vec![2, 3, 3, 4, 4, 5]),
        );

    let layout = Layout::new()
        .title("Basic Sankey")
        .font(Font::new().size(10));

    let mut plot = Plot::new();
    plot.add_trace(trace);
    plot.set_layout(layout);

    let path = write_example_to_html(&plot, file_name);
    if show {
        plot.show_html(path);
    }
}
}

Skankey diagram with defined node position

#![allow(unused)]
fn main() {
fn custom_node_sankey_diagram(show: bool, file_name: &str) {
    // https://plotly.com/javascript/sankey-diagram/#basic-sankey-diagram
    let trace = Sankey::new()
        .orientation(Orientation::Horizontal)
        .arrangement(plotly::sankey::Arrangement::Snap)
        .node(
            Node::new()
                .pad(15)
                .thickness(30)
                .line(SankeyLine::new().color(NamedColor::Black).width(0.5))
                .label(vec!["A", "B", "C", "D", "E", "F"])
                .x(vec![0.2, 0.1, 0.5, 0.7, 0.3, 0.5])
                .y(vec![0.2, 0.1, 0.5, 0.7, 0.3, 0.5])
                .pad(20),
        )
        .link(
            Link::new()
                .source(vec![0, 0, 1, 2, 5, 4, 3, 5])
                .target(vec![5, 3, 4, 3, 0, 2, 2, 3])
                .value(vec![1, 2, 1, 1, 1, 1, 1, 2]),
        );

    let layout = Layout::new()
        .title("Define Node Position")
        .font(Font::new().size(10));

    let mut plot = Plot::new();
    plot.add_trace(trace);
    plot.set_layout(layout);

    let path = write_example_to_html(&plot, file_name);
    if show {
        plot.show_html(path);
    }
}
}