{ "metadata": { "name": "", "signature": "sha256:294d6f748dc0c0c5c2e23b175dd2588d8cde07fb9219d9a891f5ef20d6da1dc0" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Texas Drought Statistics" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Data source: http://droughtmonitor.unl.edu/MapsAndData/DataTables.aspx?TX" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check Bokeh version." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import bokeh\n", "bokeh.print_versions()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n", "Bokeh version: 0.4.4-267-ge9807af\n", "Python version: 2.7.6 |Anaconda 1.9.1 (x86_64)| (default, Jan 10 2014, 11:23:15) \n", "[GCC 4.0.1 (Apple Inc. build 5493)]\n", "Platform: Darwin-13.1.0-x86_64 (Darwin Kernel Version 13.1.0: Wed Apr 2 23:52:02 PDT 2014; root:xnu-2422.92.1~2/RELEASE_X86_64)\n", "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Gather imports." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pandas as pd\n", "import numpy as np\n", "from collections import OrderedDict\n", "\n", "import datetime as dt\n", "\n", "from bokeh.plotting import *\n", "from bokeh.objects import Range1d\n", "\n", "output_notebook()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ " \n", " \n", " \n", "\n", "
\n", " \n", " \"Bokeh\n", " \n", " BokehJS successfully loaded.\n", "
" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convenience functions to reference dates in UNIX time." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def unix_time(date):\n", " epoch = dt.datetime.utcfromtimestamp(0)\n", " delta = date - epoch\n", " return delta.total_seconds()\n", "\n", "def unix_time_millis(date):\n", " return unix_time(date) * 1000.0" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set a nominal colorscheme.\n", "\n", "These colors correspond to the severity of the drought:\n", "\n", "> D0 - Abnormally Dry \n", "D1 - Moderate \n", "D2 - Severe \n", "D3 - Extreme \n", "D4 - Exceptional (dark red) \n", "Nothing (white)" ] }, { "cell_type": "code", "collapsed": true, "input": [ "COLORS = ['#800000', '#942222', '#A84444', '#BC6666', '#D08888', '#FFFFFF']" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import data and reverse dataframe, so dates are in ascending order." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = pd.read_csv(\"data/DroughtStats.csv\")\n", "a = a.reindex(index=a.index[ ::-1 ])\n", "a = a.reset_index().drop('index', axis=1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take a look!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a.head()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
WeekNothingD0D1D2D3D4
0 1/4/00 2.56 23.71 22.18 51.55 0 0
1 1/11/00 0.17 24.98 23.29 51.56 0 0
2 1/18/00 0.00 25.15 23.29 51.56 0 0
3 1/25/00 0.00 25.15 23.29 51.56 0 0
4 2/1/00 0.00 18.39 25.48 56.13 0 0
\n", "

5 rows \u00d7 7 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ " Week Nothing D0 D1 D2 D3 D4\n", "0 1/4/00 2.56 23.71 22.18 51.55 0 0\n", "1 1/11/00 0.17 24.98 23.29 51.56 0 0\n", "2 1/18/00 0.00 25.15 23.29 51.56 0 0\n", "3 1/25/00 0.00 25.15 23.29 51.56 0 0\n", "4 2/1/00 0.00 18.39 25.48 56.13 0 0\n", "\n", "[5 rows x 7 columns]" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "dates = pd.to_datetime(a['Week'])\n", "categories = list(reversed(a.columns[1:]))\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Stacked data will be much easier to plot in future versions of Bokeh, but this algorithm is simple enough to generate the patch coordinates." ] }, { "cell_type": "code", "collapsed": false, "input": [ "xs = [list(dates) + list(reversed(dates))] * len(categories)\n", "ys = []\n", "\n", "for i, cat in enumerate(categories):\n", " if i == 0:\n", " y = list(np.zeros(len(dates))) + list(reversed(a[cat]))\n", " cumulative = y\n", " else:\n", " prev = categories[i-1]\n", " y = list(a[prev]) + list(reversed(a[cat]))\n", " cumulative = list(np.sum([cumulative, y], axis=0))\n", "\n", " ys.append(cumulative)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set the plot ranges, declare some basic figure properties, and plot the patches." ] }, { "cell_type": "code", "collapsed": false, "input": [ "start_date = unix_time_millis(dates.irow(0))\n", "end_date = unix_time_millis(dates.irow(-1))\n", "x_range = Range1d(start=start_date, end=end_date)\n", "\n", "y_range = Range1d(start=0, end=100)\n", "\n", "figure(title=\"Texas Drought Statistics - 2000 to Present\",\n", " plot_width=960,\n", " x_axis_type='datetime',\n", " x_range = x_range,\n", " y_range = y_range,\n", " background_fill=\"lightgrey\")\n", "\n", "patches(\n", " xs, ys,\n", " color=COLORS,\n", " alpha=0.8,\n", " line_color=None\n", ")" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Style the plot and show!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "axis().major_label_text_font_size = \"12pt\"\n", "axis().major_label_standoff = 10\n", "axis().axis_line_color = None\n", "\n", "xaxis().major_label_orientation = np.pi/4\n", "xaxis().major_tick_out = 5\n", "xaxis().major_tick_line_color = \"darkgrey\"\n", "\n", "ygrid().grid_line_color = \"white\"\n", "ygrid().grid_line_width = 2\n", "\n", "yaxis().major_tick_line_color = \"darkgrey\"\n", "yaxis()[0].axis_label = \"Cumulative Percent in Drought\"\n", "\n", "\n", "show()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "
\n", "\n" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 10 } ], "metadata": {} } ] }