{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import OrderedDict\n",
      "import numpy as np\n",
      "import pandas as pd\n",
      "from bokeh.plotting import *"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "N = 20\n",
      "categories = ['y' + str(x) for x in range(10)]\n",
      "data = {}\n",
      "data['x'] = np.arange(N)\n",
      "for cat in categories:\n",
      "    data[cat] = np.random.randint(10, 100, size=N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "df = pd.DataFrame(data)\n",
      "df = df.set_index(['x'])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def stacked(df, categories):\n",
      "    areas = OrderedDict()\n",
      "    last = np.zeros(len(df[categories[0]]))\n",
      "    for cat in categories:\n",
      "        next = last + df[cat]\n",
      "        areas[cat] = np.hstack((last[::-1], next))\n",
      "        last = next\n",
      "    return areas"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "areas = stacked(df, categories)\n",
      "colors = brewer[\"Spectral\"][len(areas)]\n",
      "x2 = np.hstack((data['x'][::-1], data['x']))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "output_notebook()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "patches([x2 for a in areas], list(areas.values()), color=colors, alpha=0.8, line_color=None)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}