{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from collections import OrderedDict\n",
    "from bokeh.charts import Donut, output_notebook, show\n",
    "from bokeh.sampledata.olympics2014 import data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# throw the data into a pandas df\n",
    "df = pd.io.json.json_normalize(data['data'])\n",
    "\n",
    "# filter by countries with at least one medal and sort\n",
    "df = df[df['medals.total'] > 8]\n",
    "df = df.sort(\"medals.total\", ascending=False)\n",
    "\n",
    "# get the countries and we group the data by medal type\n",
    "countries = df.abbr.values.tolist()\n",
    "gold = df['medals.gold'].astype(float).values\n",
    "silver = df['medals.silver'].astype(float).values\n",
    "bronze = df['medals.bronze'].astype(float).values\n",
    "\n",
    "# build a dict containing the grouped data\n",
    "medals = OrderedDict(bronze=bronze, silver=silver, gold=gold)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "donut = Donut(\n",
    "    medals, countries, title='Medals Count, dict input', \n",
    "    xlabel='countries', ylabel='medals')\n",
    "show(donut)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(medals)\n",
    "donut = Donut(\n",
    "    df, countries, title='Medals Count, pandas input', \n",
    "    xlabel='countries', ylabel='medals')\n",
    "show(donut)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from blaze import Data\n",
    "\n",
    "medals = Data(df)\n",
    "donut = Donut(medals, countries, title='Medals Count', \n",
    "              xlabel='countries', ylabel='medals', legend=False)\n",
    "show(donut)"
   ]
  },
  {
   "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
}
