{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Maps" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "

Plotly javascript loaded.

" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "using PlotlyJS" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function maps1()\n", " marker = attr(size=[20, 30, 15, 10],\n", " color=[10, 20, 40, 50],\n", " cmin=0,\n", " cmax=50,\n", " colorscale=\"Greens\",\n", " colorbar=attr(title=\"Some rate\",\n", " ticksuffix=\"%\",\n", " showticksuffix=\"last\"),\n", " line_color=\"black\")\n", " trace = scattergeo(;mode=\"markers\", locations=[\"FRA\", \"DEU\", \"RUS\", \"ESP\"],\n", " marker=marker, name=\"Europe Data\")\n", " layout = Layout(geo_scope=\"europe\", geo_resolution=50, width=500, height=550,\n", " margin=attr(l=0, r=0, t=10, b=0))\n", " plot(trace, layout)\n", "end\n", "maps1()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "function maps2()\n", " @eval using DataFrames\n", "\n", " # read Data into dataframe\n", " nm = tempname()\n", " url = \"https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv\"\n", " download(url, nm)\n", " df = readtable(nm)\n", " rm(nm)\n", "\n", " trace = scattergeo(;locationmode=\"USA-states\",\n", " lat=df[:lat],\n", " lon=df[:lon],\n", " hoverinfo=\"text\",\n", " text=[string(x[:name], \" pop: \", x[:pop]) for x in eachrow(df)],\n", " marker_size=df[:pop]/50_000,\n", " marker_line_color=\"black\", marker_line_width=2)\n", " geo = attr(scope=\"usa\",\n", " projection_type=\"albers usa\",\n", " showland=true,\n", " landcolor=\"rgb(217, 217, 217)\",\n", " subunitwidth=1,\n", " countrywidth=1,\n", " subunitcolor=\"rgb(255,255,255)\",\n", " countrycolor=\"rgb(255,255,255)\")\n", "\n", " layout = Layout(;title=\"2014 US City Populations\", showlegend=false, geo=geo)\n", " plot(trace, layout)\n", "end\n", "maps2()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## line/scatter" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "function dumbell()\n", " # reference: https://plot.ly/r/dumbbell-plots/\n", " @eval using DataFrames\n", "\n", " # read Data into dataframe\n", " nm = tempname()\n", " url = \"https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv\"\n", " download(url, nm)\n", " df = readtable(nm)\n", " rm(nm)\n", "\n", " # sort dataframe by male earnings\n", " df = sort(df, cols=[:Men], rev=false)\n", "\n", " men = scatter(;y=df[:School], x=df[:Men], mode=\"markers\", name=\"Men\",\n", " marker=attr(color=\"blue\", size=12))\n", " women = scatter(;y=df[:School], x=df[:Women], mode=\"markers\", name=\"Women\",\n", " marker=attr(color=\"pink\", size=12))\n", "\n", " lines = map(eachrow(df)) do r\n", " scatter(y=fill(r[:School], 2), x=[r[:Women], r[:Men]], mode=\"lines\",\n", " name=r[:School], showlegend=false, line_color=\"gray\")\n", " end\n", "\n", " data = Base.typed_vcat(GenericTrace, men, women, lines)\n", " layout = Layout(width=650, height=650, margin_l=100, yaxis_title=\"School\",\n", " xaxis_title=\"Annual Salary (thousands)\",\n", " title=\"Gender earnings disparity\")\n", "\n", " plot(data, layout)\n", "end\n", "dumbell()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "function batman()\n", " # reference: https://github.com/alanedelman/18.337_2015/blob/master/Lecture01_0909/The%20Bat%20Curve.ipynb\n", " σ(x) = √(1-x.^2)\n", " el(x) = 3*σ(x/7)\n", " s(x) = 4.2 - 0.5*x - 2.0*σ(0.5*x-0.5)\n", " b(x) = σ(abs(2-x)-1) - x.^2/11 + 0.5x - 3\n", " c(x) = [1.7, 1.7, 2.6, 0.9]\n", "\n", " p(i, f; kwargs...) = scatter(;x=[-i; 0.0; i], y=[f(i); NaN; f(i)],\n", " marker_color=\"black\", showlegend=false,\n", " kwargs...)\n", " traces = vcat(p(3:0.1:7, el; name=\"wings 1\"),\n", " p(4:0.1:7, t->-el(t); name=\"wings 2\"),\n", " p(1:0.1:3, s; name=\"Shoulders\"),\n", " p(0:0.1:4, b; name=\"Bottom\"),\n", " p([0, 0.5, 0.8, 1], c; name=\"head\"))\n", "\n", " plot(traces, Layout(title=\"Batman\"))\n", "end\n", "batman()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contour" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "unction contour1()\n", " x = [-9, -6, -5 , -3, -1]\n", " y = [0, 1, 4, 5, 7]\n", " z = [10 10.625 12.5 15.625 20\n", " 5.625 6.25 8.125 11.25 15.625\n", " 2.5 3.125 5. 8.125 12.5\n", " 0.625 1.25 3.125 6.25 10.625\n", " 0 0.625 2.5 5.625 10]\n", " trace = contour(x=x, y=y, z=z)\n", "\n", " layout = Layout(title=\"Setting the X and Y Coordinates in a Contour Plot\")\n", " plot(trace, layout)\n", "end\n", "contour1()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3d" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "function random_line()\n", " n = 400\n", " rw() = cumsum(randn(n))\n", " trace1 = scatter3d(;x=rw(),y=rw(), z=rw(), mode=\"lines\",\n", " marker=attr(color=\"#1f77b4\", size=12, symbol=\"circle\",\n", " line=attr(color=\"rgb(0,0,0)\", width=0)),\n", " line=attr(color=\"#1f77b4\", width=1))\n", " trace2 = scatter3d(;x=rw(),y=rw(), z=rw(), mode=\"lines\",\n", " marker=attr(color=\"#9467bd\", size=12, symbol=\"circle\",\n", " line=attr(color=\"rgb(0,0,0)\", width=0)),\n", " line=attr(color=\"rgb(44, 160, 44)\", width=1))\n", " trace3 = scatter3d(;x=rw(),y=rw(), z=rw(), mode=\"lines\",\n", " marker=attr(color=\"#bcbd22\", size=12, symbol=\"circle\",\n", " line=attr(color=\"rgb(0,0,0)\", width=0)),\n", " line=attr(color=\"#bcbd22\", width=1))\n", " layout = Layout(autosize=false, width=500, height=500,\n", " margin=attr(l=0, r=0, b=0, t=65))\n", " plot([trace1, trace2, trace3], layout)\n", "end\n", "random_line()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "function multiple_surface()\n", " z1 = [8.83 8.89 8.81 8.87 8.90 8.87\n", " 8.89 8.94 8.85 8.94 8.96 8.92\n", " 8.84 8.90 8.82 8.92 8.93 8.91\n", " 8.79 8.85 8.79 8.90 8.94 8.92\n", " 8.79 8.88 8.81 8.90 8.95 8.92\n", " 8.80 8.82 8.78 8.91 8.94 8.92\n", " 8.75 8.78 8.77 8.91 8.95 8.92\n", " 8.80 8.80 8.77 8.91 8.95 8.94\n", " 8.74 8.81 8.76 8.93 8.98 8.99\n", " 8.89 8.99 8.92 9.10 9.13 9.11\n", " 8.97 8.97 8.91 9.09 9.11 9.11\n", " 9.04 9.08 9.05 9.25 9.28 9.27\n", " 9.00 9.01 9.00 9.20 9.23 9.20\n", " 8.99 8.99 8.98 9.18 9.20 9.19\n", " 8.93 8.97 8.97 9.18 9.20 9.18]\n", " z2 = map(x->x+1, z1)\n", " z3 = map(x->x-1, z1)\n", " trace1 = surface(z=z1)\n", " trace2 = surface(z=z2, showscale=false, opacity=0.9)\n", " trace3 = surface(z=z3, showscale=false, opacity=0.9)\n", " plot([trace1, trace2, trace3])\n", "end\n", "multiple_surface()" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 0.5.0-rc3", "language": "julia", "name": "julia-0.5" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "0.5.0" } }, "nbformat": 4, "nbformat_minor": 0 }