{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from collections import OrderedDict\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from bokeh.charts import Bar, output_notebook, show\n",
    "from bokeh.plotting import VBox\n",
    "from bokeh.sampledata.olympics2014 import data as original_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = {d['abbr']: d['medals'] for d in original_data['data'] if d['medals']['total'] > 0}\n",
    "\n",
    "countries = sorted(data.keys(), key=lambda x: data[x]['total'], reverse=True)\n",
    "\n",
    "gold = np.array([data[abbr]['gold'] for abbr in countries], dtype=np.float)\n",
    "silver = np.array([data[abbr]['silver'] for abbr in countries], dtype=np.float)\n",
    "bronze = np.array([data[abbr]['bronze'] for abbr in countries], dtype=np.float)\n",
    "output_notebook()\n",
    "medals = OrderedDict(bronze=bronze, silver=silver, gold=gold)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "bar = Bar(\n",
    "    medals, countries, title=\"grouped, dict input\", \n",
    "    xlabel=\"countries\", ylabel=\"medals\", legend=\"top_right\", width=600, height=400)\n",
    "show(bar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "bar = Bar(\n",
    "    medals, countries, title=\"stacked, dict input\", \n",
    "    xlabel=\"countries\", ylabel=\"medals\", legend=\"top_right\", width=600, height=400, stacked=True)\n",
    "show(bar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(medals, index=countries)\n",
    "\n",
    "bar1 = Bar(\n",
    "    df, title=\"stacked, pandas input\", xlabel=\"countries\", ylabel=\"medals\", \n",
    "    legend=\"top_right\", width=600, height=400, stacked=True)\n",
    "\n",
    "bar2 = bar = Bar(\n",
    "    df, title=\"grouped, pandas input\", xlabel=\"countries\", ylabel=\"medals\", \n",
    "    legend=\"top_right\", width=600, height=400, stacked=False)\n",
    "\n",
    "show(VBox(bar1, bar2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "medals = np.array([bronze, silver, gold])\n",
    "bar = Bar(\n",
    "    medals, title=\"grouped, array input\", xlabel=\"countries\", ylabel=\"medals\", \n",
    "    legend=\"top_right\", width=600, height=400)\n",
    "show(bar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from blaze import Data\n",
    "\n",
    "medals = Data(df)\n",
    "bar = Bar(\n",
    "    medals, cat=countries, title=\"grouped, df_input\", xlabel=\"countries\", \n",
    "    ylabel=\"medals\", legend='top_right', stacked=True, width=800, height=600)\n",
    "show(bar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
