{
 "metadata": {
  "name": "",
  "signature": "sha256:f5a1a78c7a183c4d1558b5ed442bba5e7560c48b3b9f442198c1fe24bdab7c24"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import bokeh\n",
      "bokeh.load_notebook()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import OrderedDict\n",
      "\n",
      "import numpy as np\n",
      "from bokeh.charts import Histogram\n",
      "\n",
      "mu, sigma = 0, 0.5\n",
      "normal = np.random.normal(mu, sigma, 1000)\n",
      "\n",
      "normal_dist = OrderedDict(normal=normal)\n",
      "\n",
      "hist = Histogram(normal_dist, bins=50, mu=mu, sigma=sigma,\n",
      "                 title=\"kwargs, dict_input\", ylabel=\"frequency\", legend=\"top_left\",\n",
      "                 width=400, height=350, notebook=True)\n",
      "hist.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pandas as pd\n",
      "df = pd.DataFrame(normal_dist)\n",
      "\n",
      "hist = Histogram(df, bins=50, mu=mu, sigma=sigma,\n",
      "                 title=\"no_tools, df_input\", ylabel=\"frequency\", legend=\"top_left\",\n",
      "                 tools=True, width=400, height=350, notebook=True)\n",
      "hist.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import OrderedDict\n",
      "\n",
      "import numpy as np\n",
      "from bokeh.charts import Histogram\n",
      "\n",
      "mu, sigma = 0, 0.5\n",
      "normal = np.random.normal(mu, sigma, 1000)\n",
      "lognormal = np.random.lognormal(mu, sigma, 1000)\n",
      "\n",
      "distributions = OrderedDict(normal=normal, lognormal=lognormal)\n",
      "\n",
      "hist = Histogram(distributions, bins=50, notebook=True)\n",
      "hist.title(\"chained_methods, dict_input\").ylabel(\"frequency\").legend(True).width(400).height(350).show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pandas as pd\n",
      "df = pd.DataFrame(distributions)\n",
      "\n",
      "hist = Histogram(df, bins=50, notebook=True)\n",
      "hist.title(\"chained_methods, df_input\").ylabel(\"frequency\").width(400).height(350).show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import OrderedDict\n",
      "\n",
      "import numpy as np\n",
      "\n",
      "from bokeh.charts import Bar\n",
      "from bokeh.sampledata.olympics2014 import data\n",
      "\n",
      "data = {d['abbr']: d['medals'] for d in 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",
      "\n",
      "medals = OrderedDict(bronze=bronze, silver=silver, gold=gold)\n",
      "\n",
      "bar = Bar(medals, countries, title=\"grouped, dict_input\", xlabel=\"countries\", ylabel=\"medals\", \n",
      "          legend=True, width=800, height=600, notebook=True)\n",
      "bar.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "import pandas as pd\n",
      "df = pd.DataFrame(medals, index=countries)\n",
      "\n",
      "bar = Bar(df, title=\"grouped, df_input\", xlabel=\"countries\", ylabel=\"medals\", legend=True, width=800, height=600, notebook=True)\n",
      "bar.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import OrderedDict\n",
      "\n",
      "import numpy as np\n",
      "\n",
      "from bokeh.charts import Bar\n",
      "from bokeh.sampledata.olympics2014 import data\n",
      "\n",
      "data = {d['abbr']: d['medals'] for d in 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",
      "\n",
      "medals = OrderedDict(bronze=bronze, silver=silver, gold=gold)\n",
      "\n",
      "bar = Bar(medals, countries)\n",
      "bar.title(\"stacked, dict_input\").xlabel(\"countries\").ylabel(\"medals\").legend(True).width(600).height(400).stacked().notebook().show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pandas as pd\n",
      "df = pd.DataFrame(medals, index=countries)\n",
      "\n",
      "bar = Bar(df)\n",
      "bar.title(\"stacked, df_input\").xlabel(\"countries\").ylabel(\"medals\").legend(True).width(600).height(400).stacked().notebook().show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import OrderedDict\n",
      "\n",
      "from bokeh.charts import Scatter\n",
      "from bokeh.sampledata.iris import flowers\n",
      "\n",
      "setosa = flowers[(flowers.species == \"setosa\")][[\"petal_length\", \"petal_width\"]]\n",
      "versicolor = flowers[(flowers.species == \"versicolor\")][[\"petal_length\", \"petal_width\"]]\n",
      "virginica = flowers[(flowers.species == \"virginica\")][[\"petal_length\", \"petal_width\"]]\n",
      "\n",
      "xyvalues = OrderedDict([(\"setosa\", setosa.values), (\"versicolor\", versicolor.values), (\"virginica\", virginica.values)])\n",
      "\n",
      "scatter = Scatter(xyvalues)\n",
      "scatter.title(\"iris dataset, dict_input\").xlabel(\"petal_length\").ylabel(\"petal_width\").legend(\"top_left\").width(600).height(400).notebook().show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pandas as pd\n",
      "\n",
      "xyvalues = OrderedDict([(\"setosa\", setosa), (\"versicolor\", versicolor), (\"virginica\", virginica)])\n",
      "df = pd.concat(xyvalues, axis=1, names=[\"l0\", \"l1\"])\n",
      "\n",
      "scatter = Scatter(df)\n",
      "scatter.title(\"iris dataset, df_input\").legend(\"top_left\").width(600).height(400).notebook().show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from bokeh.charts import Scatter\n",
      "from bokeh.sampledata.iris import flowers\n",
      "\n",
      "df = flowers[[\"petal_length\", \"petal_width\", \"species\"]]\n",
      "g = df.groupby(\"species\")\n",
      "\n",
      "scatter = Scatter(g)\n",
      "scatter.title(\"iris dataset, gp_by_input\").legend(\"top_left\").width(600).height(400).notebook().show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import OrderedDict\n",
      "\n",
      "import numpy as np\n",
      "\n",
      "from bokeh.charts import BoxPlot\n",
      "from bokeh.sampledata.olympics2014 import data\n",
      "\n",
      "data = {d['abbr']: d['medals'] for d in 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",
      "\n",
      "medals = OrderedDict(bronze=bronze, silver=silver, gold=gold)\n",
      "\n",
      "boxplot = BoxPlot(medals, title=\"boxplot, dict_input\", xlabel=\"medal type\", ylabel=\"medal count\", \n",
      "             width=800, height=600, notebook=True)\n",
      "boxplot.marker(\"circle\").outliers(True).legend(True).show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import OrderedDict\n",
      "import pandas as pd\n",
      "\n",
      "# Here is some code to read in some stock data from the Yahoo Finance API\n",
      "AAPL = pd.read_csv(\n",
      "    \"http://ichart.yahoo.com/table.csv?s=AAPL&a=0&b=1&c=2000\",\n",
      "    parse_dates=['Date'])\n",
      "MSFT = pd.read_csv(\n",
      "    \"http://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2000\",\n",
      "    parse_dates=['Date'])\n",
      "IBM = pd.read_csv(\n",
      "    \"http://ichart.yahoo.com/table.csv?s=IBM&a=0&b=1&c=2000\",\n",
      "    parse_dates=['Date'])\n",
      "\n",
      "xyvalues = OrderedDict(AAPL=AAPL[['Date', 'Adj Close']], \n",
      "                       MSFT=MSFT[['Date', 'Adj Close']], \n",
      "                       IBM=IBM[['Date', 'Adj Close']])\n",
      "df = pd.concat(xyvalues, axis=1, names=[\"l0\", \"l1\"])\n",
      "\n",
      "from bokeh.charts import TimeSeries\n",
      "ts = TimeSeries(df, title=\"timeseries, pd_input\", notebook=True)\n",
      "ts.legend(\"top_left\").show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from bokeh.sampledata.unemployment1948 import data\n",
      "\n",
      "# pandas magic\n",
      "df = data[data.columns[:-2]]\n",
      "df2 = df.set_index(df[df.columns[0]].astype(str))\n",
      "df2.drop(df.columns[0], axis=1, inplace=True)\n",
      "df3 = df2.transpose()\n",
      "\n",
      "from bokeh.palettes import brewer\n",
      "palette = brewer[\"Purples\"][9]\n",
      "\n",
      "# bokeh magic\n",
      "from bokeh.charts import CategoricalHeatMap\n",
      "hm = CategoricalHeatMap(df3, palette=palette, title=\"categorical heatmap, pd_input\", notebook=True)\n",
      "hm.width(1000).height(400).show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}