{ "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": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function plot_matrices()\n", " pyplot()\n", " z = rand(10,10)\n", " plot(spy(z), heatmap(z), contour(z), surface(z))\n", "end\n", "plot_matrices()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Advanced Layouts" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function advanced_layouts()\n", " l = @layout [ a{0.3w} [grid(3,3)\n", " b{0.2h} ]]\n", " plot(\n", " rand(10,11),\n", " layout = l, legend = false, seriestype = [:bar :scatter :path],\n", " title = reshape([\"($i)\" for i=1:11], 1, 11), titleloc = :center, titlefont = font(8)\n", " )\n", "end\n", "advanced_layouts()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "## Basic Plots for Statistical Analysis" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotlyjs()\n", "function grouped_bar_example()\n", " x=[\"giraffes\", \"orangutans\", \"monkeys\"]\n", " y1=[20, 14, 23]\n", " y2=[12, 18, 29]\n", "\n", " bar(x, y1, label= \"SF Zoo\")\n", " bar!(x, y2, label= \"LA Zoo\")\n", "end\n", "grouped_bar_example()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Challenge: Stacking vertical (bar_position) doesn't seem to be supported (only overlay) and it doesn't work for all backends" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pyplot()\n", "function stacked_bar_example()\n", " x=[\"giraffes\", \"orangutans\", \"monkeys\"]\n", " y1=[20, 14, 23]\n", " y2=[12, 18, 29]\n", "\n", " bar(x, y1, bar_position=:stack, label= \"SF Zoo\")\n", " bar!(x, y2, bar_position=:stack, label= \"LA Zoo\")\n", "end\n", "stacked_bar_example()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function two_hists()\n", " x0 = randn(500)\n", " x1 = x0+1\n", "\n", " histogram(x0, opacity=0.75)\n", " histogram!(x1, opacity=0.75)\n", "end\n", "\n", "two_hists()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### BoxPlot \n", "\n", "#### Challenge: How to group by day without overlapping?" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/html": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function box_plot()\n", " x1 = [\"day 1\", \"day 1\", \"day 1\", \"day 1\", \"day 1\", \"day 1\",\n", " \"day 2\", \"day 2\", \"day 2\", \"day 2\", \"day 2\", \"day 2\"]\n", " x2 = [\"day 1B\", \"day 1B\", \"day 1B\", \"day 1B\", \"day 1B\", \"day 1B\",\n", " \"day 2B\", \"day 2B\", \"day 2B\", \"day 2B\", \"day 2B\", \"day 2B\"]\n", " x3 = [\"day 1C\", \"day 1C\", \"day 1C\", \"day 1C\", \"day 1C\", \"day 1C\",\n", " \"day 2C\", \"day 2C\", \"day 2C\", \"day 2C\", \"day 2C\", \"day 2C\"]\n", " \n", " y1=[0.2, 0.2, 0.6, 1.0, 0.5, 0.4, 0.2, 0.7, 0.9, 0.1, 0.5, 0.3]\n", " y2=[0.6, 0.7, 0.3, 0.6, 0.0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2]\n", " y3=[0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1.0, 0.3, 0.6, 0.8, 0.5]\n", " \n", " boxplot(x1, y1, label=\"kale\", color=\"#3D9970\")\n", " boxplot!(x2, y2, label=\"radishes\", color=\"#FF4136\")\n", " boxplot!(x3, y3, label=\"carrot\", color=\"#FF851B\")\n", "# \n", "end\n", "box_plot()\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "### Plotting DataFrames:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING: Method definition dataframe_scatter() in module Main at In[18]:3 overwritten at In[19]:3.\n" ] }, { "data": { "text/html": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "function dataframe_scatter()\n", " iris = dataset(\"datasets\", \"iris\");\n", "# display(head(iris));\n", " scatter(iris, :SepalLength, :SepalWidth, group=:Species,\n", " title = \"Iris Sepal lengh vs width\",\n", " xlabel = \"Length\", ylabel = \"Width\",\n", " m=(0.5, [:cross :hex :star7], 12))\n", "end\n", "dataframe_scatter()" ] } ], "metadata": { "anaconda-cloud": {}, "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Julia 0.5.1", "language": "julia", "name": "julia-0.5" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.5.1" } }, "nbformat": 4, "nbformat_minor": 1 }