{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.html.widgets import interact\n",
    "from dautil import data\n",
    "from dautil import ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = data.Weather.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def plot_data(x='TEMP', y='RAIN', z='WIND_SPEED', f='A', size=10, cmap='Blues'):\n",
    "    dfx = df[x].resample(f)\n",
    "    dfy = df[y].resample(f)\n",
    "    dfz = df[z].resample(f)\n",
    "    \n",
    "    bubbles = (dfz - dfz.min())/(dfz.max() - dfz.min())\n",
    "    years = dfz.index.year\n",
    "    sc = plt.scatter(dfx, dfy, s= size * bubbles + 9, c = years,\n",
    "                cmap=cmap, label=data.Weather.get_header(z), alpha=0.5)\n",
    "    plt.colorbar(sc, label='Year')\n",
    "    \n",
    "    freqs = {'A': 'Annual', 'M': 'Monthly', 'D': 'Daily'}\n",
    "    plt.title(freqs[f] + ' Averages')\n",
    "    plt.xlabel(data.Weather.get_header(x))\n",
    "    plt.ylabel(data.Weather.get_header(y))\n",
    "    plt.legend(loc='best')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "vars = df.columns.tolist()\n",
    "freqs = ('A', 'M', 'D')\n",
    "cmaps = [cmap for cmap in plt.cm.datad if not cmap.endswith(\"_r\")]\n",
    "cmaps.sort()\n",
    "interact(plot_data, x=vars, y=vars, z=vars, f=freqs, size=(100,700), cmap=cmaps)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def plot_data(x='TEMP', y='RAIN', z='WIND_SPEED', groupby='ts.groupby_yday', size=10, cmap='Blues'):\n",
    "    if groupby == 'ts.groupby_yday':\n",
    "        groupby = ts.groupby_yday\n",
    "    elif groupby == 'ts.groupby_month':\n",
    "        groupby = ts.groupby_month\n",
    "    else:\n",
    "        raise AssertionError('Unknown groupby ' + groupby)\n",
    "        \n",
    "    dfx = groupby(df[x]).mean()\n",
    "    dfy = groupby(df[y]).mean()\n",
    "    dfz = groupby(df[z]).mean()\n",
    "    \n",
    "    bubbles = (dfz - dfz.min())/(dfz.max() - dfz.min())\n",
    "    colors = dfx.index.values\n",
    "    sc = plt.scatter(dfx, dfy, s= size * bubbles + 9, c = colors,\n",
    "                cmap=cmap, label=data.Weather.get_header(z), alpha=0.5)\n",
    "    plt.colorbar(sc, label='Day of Year')\n",
    "    \n",
    "    by_dict = {ts.groupby_yday: 'Day of Year', ts.groupby_month: 'Month'}\n",
    "    plt.title('Grouped by ' + by_dict[groupby])\n",
    "    plt.xlabel(data.Weather.get_header(x))\n",
    "    plt.ylabel(data.Weather.get_header(y))\n",
    "    plt.legend(loc='best')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "groupbys = ('ts.groupby_yday', 'ts.groupby_month')\n",
    "interact(plot_data, x=vars, y=vars, z=vars, groupby=groupbys, size=(100,700), cmap=cmaps)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
