{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotters Tutorial with Jupyter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a interactive tutorial for [`Plotters`](https://github.com/38/plotters) drawing library. If you are looking at the static HTML version and want to try the interactive version. Please follow the steps:\n", "\n", "#### For Ubuntu/Debian users\n", "\n", "```bash\n", "# Install Jupyter notebook \n", "sudo apt install libzmq3-dev jupyter-notebook\n", "cargo install evcxr_jupyter\n", "evcxr_jupyter --install\n", "# Get the notebook\n", "git clone https://github.com/38/plotters-doc-data\n", "cd plotteres-doc-data\n", "jupyter notebook\n", "```\n", "\n", "#### For OSX users\n", "\n", "```bash\n", "# Install Jupyter notebook \n", "brew install zeromq pkg-config\n", "cargo install evcxr_jupyter\n", "evcxr_jupyter --install\n", "# Get the notebook\n", "git clone https://github.com/38/plotters-doc-data\n", "cd plotteres-doc-data\n", "jupyter notebook\n", "```\n", "\n", "You can also download the latest notebook from [https://raw.githubusercontent.com/38/plotters-doc-data/master/evcxr-jupyter-integration.ipynb](https://raw.githubusercontent.com/38/plotters-doc-data/master/evcxr-jupyter-integration.ipynb), thus you don't have to clone the entire data repo." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get Started" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to use `Plotters` in `jupyter-evcxr`, you need both Jupyter and evcxr installed.\n", "Check [https://github.com/google/evcxr](https://github.com/google/evcxr) for the instructions.\n", "\n", "To use Plotters with `jupyter-evcxr`, you need to import it using the following code:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ ":dep plotters = { git = \"https://github.com/38/plotters\", default_features = false, features = [\"evcxr\"] }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because `evcxr` uses only SVG images, so we don't need other types of backend. So we should put\n", "\n", "`default_features = false, features = [\"evcxr\"]`\n", "\n", "Make the compilation faster. Since `evcxr` shares all the artifacts among cells, after the first time we have `plotters` compiled, it should be faster after." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotters evcxr integration overview" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use plotters, the most convenient way is importing everything defined in the `prelude` module.\n", "It will import `evcxr_figure` function for `evcxr` integration. \n", "\n", "*Note: Currently evcxr doesn't work with nightly rust, so please make sure you are using a stable rust*" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ ":dep plotters = { git = \"https://github.com/38/plotters\", default_features = false, features = [\"evcxr\"] }\n", "extern crate plotters;\n", "// Import all the plotters prelude functions\n", "use plotters::prelude::*;\n", "// To create a figure that can be displayed in Jupyter notebook, use evcxr_figure function.\n", "// The first param is the resolution of the figure.\n", "// The second param is the closure that performes the drawing.\n", "evcxr_figure((300, 10), |root| {\n", " // Do the drawings\n", " root.fill(&BLUE)?;\n", " // Tell plotters that everything is ok\n", " Ok(())\n", "})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hello World" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ ":dep plotters = { git = \"https://github.com/38/plotters\", default_features = false, features = [\"evcxr\"] }\n", "extern crate plotters;\n", "use plotters::prelude::*;\n", "\n", "evcxr_figure((320,50), |root| {\n", " root.fill(&GREEN)?;\n", " root.draw(&Text::new(\"Hello World from Plotters!\", (15, 15), (\"Arial\", 20).into_font()))?;\n", " Ok(())\n", "})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sub- Drawing Areas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the very important features is, `Plotters` allows drawing multiple charts in a single figure. And this is done by having sub-drawing-areas. The root drawing area is able to be splitted into smaller drawing areas, and you can always do more fine-grained splits as well." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "