{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Preamble" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "extern crate plotly;\n", "\n", "use plotly::{Plot, Scatter};\n", "use plotly::common::{Mode};\n", "use std::fs;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotly for Visualisation\n", "\n", "In my other book, [Practical Evolutionary Algorithms](https://store.shahinrostami.com/product/practical-evolutionary-algorithms-book/), I relied on the [Plotly graphic libraries](https://plot.ly/graphing-libraries/) to generate visualisations throughout each notebook. When I started writing Rust Notebooks a Plotly solution was not available, however, I found [Plotters](https://docs.rs/plotters/0.2.12/plotters/) to be a suitable alternative for rendering visualisations. Less than 24 hours after making that decision, a [plotting library](https://github.com/igiagkiozis/plotly/tree/master) for Rust powered by Plotly.js was posted on Reddit and caught my attention." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Currently there is no documented support for rendering within Jupyter Notebook cells, however, it is possible to use the `.to_html()` function to save to a HTML file, and then load and print that HTML file with RUST. We'll store this in a file named `temp_plot.html`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "let plotly_file = \"temp_plot.html\";" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's demonstrate this with the first code example listed on the *Plotly with Rust* README." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "let trace1 = Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17])\n", " .name(\"trace1\")\n", " .mode(Mode::Markers);\n", "let trace2 = Scatter::new(vec![2, 3, 4, 5], vec![16, 5, 11, 9])\n", " .name(\"trace2\")\n", " .mode(Mode::Lines);\n", "let trace3 = Scatter::new(vec![1, 2, 3, 4], vec![12, 9, 15, 12]).name(\"trace3\");\n", "\n", "let mut plot = Plot::new();\n", "plot.add_trace(trace1);\n", "plot.add_trace(trace2);\n", "plot.add_trace(trace3);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we will save this to a file using the `.to_html()` function, read it to `plotly_contents` using Rust, print it using `println!()`, and finally delete the file created by `.to_html()` as we don't need it after it is embedded.\n", "
" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", "\n", "\n", "
\n", " \n", " \n", " \n", "