{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Plotting with Plotls.jl\n", "\n", "* [GitHub Repository](https://github.com/JuliaPlots/Plots.jl)\n", "* [Documentation](https://juliaplots.github.io)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", "
Plotly javascript loaded.
\n", "To load again call
init_notebook(true)\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Method definition describe(AbstractArray) in module StatsBase at /Users/mrestrep/.julia/v0.5/StatsBase/src/scalarstats.jl:573 overwritten in module DataFrames at /Users/mrestrep/.julia/v0.5/DataFrames/src/abstractdataframe/abstractdataframe.jl:407.\n" ] }, { "data": { "text/plain": [ "Plots.PlotlyJSBackend()" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Plots\n", "using StatPlots\n", "using DataFrames, RDatasets\n", "\n", "plotlyjs()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Basics\n", "\n", "## Simple line scatter\n", "\n", "* Define x, y vectors \n", "* Invoke plot() with x, y as regular inputs" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function linescatter()\n", " x=1:4\n", " y=[10, 15, 13, 17]\n", " plot(x, y)\n", "end\n", "linescatter()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Customize and add multiple traces to one plot\n", "\n", "* Multiple approaches\n", " * Plot each trace at a time, and plot on top using plot!()\n", " * Could also pass inputs as vectors and matrices" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function multiple_scatter_traces()\n", " x=1:4\n", " y1 = [10, 15, 13, 17]\n", " y2 = [16, 5, 11, 9]\n", " y3 = [12, 9, 15, 12]\n", " y4 = [5, 10, 8, 12]\n", " plot(y1, line = (:scatter, 1), label=\"marker\")\n", " plot!(y2, marker =(:circle, 4), label=\"line+marker\")\n", " plot!(y3, label=\"line\")\n", " plot!(y4, line = (:dash, 2), label=\"dash\")\n", "end\n", "multiple_scatter_traces()\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Multiple traces can also be plotted as a matrix\n", "If the argument is a \"matrix-type\", then each column will map to a series, cycling through columns if there are fewer columns than series. In this sense, a vector is treated just like an \"nx1 matrix\".\n", "\n", "\n", "### Challenge! Make the markers work!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function multiple_scatter_traces_v2()\n", " x=1:4\n", " y1 = [10, 15, 13, 17]\n", " y2 = [16, 5, 11, 9]\n", " y3 = [12, 9, 15, 12]\n", " y4 = [5, 10, 8, 12]\n", " y = [y1, y2, y3, y4]\n", " plot(y, line=(2, [:solid :solid :dash :dash]))\n", "end\n", "multiple_scatter_traces_v2()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Adding data labels\n", "* Pass the array of labels as `series_annotations`" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function data_labels()\n", " x = 1:5\n", " y1 = [1, 6, 3, 6, 1]\n", " y2 = [4, 1, 7, 1, 4]\n", "\n", " plot(y1, line = (:scatter, 1), \n", " series_annotations=[text(\"A-1\", :bottom, 12, :blue, \"Raleway, sans-serif\"),\n", " \"A-2\",\"A-3\",\"A-4\",\"A-5\"], \n", " label=\"Team A\",\n", " title=\"Data Labels on the plot\")\n", " plot!(y2, line = (:scatter, 1), \n", " series_annotations=[text(\"B-1\", :top, 12, :red, \"Times\"),\n", " \"B-2\",\"B-3\",\"B-4\",\"B-5\"], \n", " label=\"Team B\")\n", "end\n", "data_labels()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Area\n", "\n", "* How tomake it fill only to next y?" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pyplot() -- to fill to one\n", "function area1()\n", " y1=[0, 2, 3, 5] #, fill=\"tozeroy\")\n", " y2=[3, 5, 1, 7] #, fill=\"tonexty\")\n", " plot(y1, label=\"lines\", w=3, fill=0, α=1)\n", " plot!(y2, label=\"lines\", w=3, fill=0, α=0.6)\n", "end\n", "area1()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Other visual interpratations of matrix data?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "