Finishing Touches for Visualisation


In [2]:
extern crate plotly;
extern crate nanoid;
extern crate darn;

use plotly::{Plot, Scatter, Layout};
use plotly::common::{Mode, Anchor, Orientation, Title};
use plotly::layout::{Legend, Margin, Axis};
use nanoid::nanoid;
use std::fs;

Plotly Workaround Function

In the last section, we improved upon our Plotly workaround to get our plots to appear within our notebooks. All that's left now is to separate our workaround into a function so that we can re-use it throughout the rest of this book. We'll name this function show_plot()

In [3]:
fn show_plot(plot: Plot) { 
    let plotly_file = "temp_plot.html";
    let plotly_contents = fs::read_to_string(plotly_file).unwrap();

    let start_bytes = plotly_contents
        .find("<div id='plotly-html-element' class='plotly-graph-div'")
    let end_bytes = plotly_contents
    println!("EVCXR_BEGIN_CONTENT text/html\n{}\nEVCXR_END_CONTENT",
        .replace("plotly-html-element", Box::leak(nanoid!().into_boxed_str()))));

Now let's use the same example code from the previous sections and use our function above to display the plot. This time, we're going to use the Layout struct and specify some customisations. This will reduce the padding on our plot, change the positioning of the legend, and label the axes. This is not a necessary step, however, the output should look much nicer.

In [4]:
let layout = Layout::new()
    .xaxis(Axis::new().title(Title::new("x axis")))
    .yaxis(Axis::new().title(Title::new("y axis")))

let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17])
let trace2 = Scatter::new(vec![2, 3, 4, 5], vec![16, 5, 11, 9])
let trace3 = Scatter::new(vec![1, 2, 3, 4], vec![12, 9, 15, 12])

let mut plot = Plot::new();


To display our plot, we can pass the plot variable to our show_plot() function.

In [5]: