{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Some examples of seaborn-based plots rendered by Bokeh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from bokeh import mpl\n",
    "from bokeh.plotting import output_notebook, show"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from scipy import optimize\n",
    "\n",
    "# Set the palette colors.\n",
    "sns.set(palette=\"Set2\")\n",
    "\n",
    "\n",
    "# Build the sin wave\n",
    "def sine_wave(n_x, obs_err_sd=1.5, tp_err_sd=.3):\n",
    "    x = np.linspace(0, (n_x - 1) / 2, n_x)\n",
    "    y = np.sin(x) + np.random.normal(0, obs_err_sd) + np.random.normal(0, tp_err_sd, n_x)\n",
    "    return y\n",
    "\n",
    "sines = np.array([sine_wave(31) for _ in range(20)])\n",
    "\n",
    "# Generate the Seaborn plot with \"ci\" bars.\n",
    "ax = sns.tsplot(sines, err_style=\"ci_bars\", interpolate=False)\n",
    "xmin, xmax = ax.get_xlim()\n",
    "x = np.linspace(xmin, xmax, sines.shape[1])\n",
    "out, _ = optimize.leastsq(lambda p: sines.mean(0) - (np.sin(x / p[1]) + p[0]), (0, 2))\n",
    "a, b = out\n",
    "xx = np.linspace(xmin, xmax, 100)\n",
    "plt.plot(xx, np.sin(xx / b) + a, c=\"#444444\")\n",
    "\n",
    "plt.title(\"Seaborn tsplot with CI in bokeh.\")\n",
    "\n",
    "show(mpl.to_bokeh())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib as mplc\n",
    "\n",
    "# Generate the pandas dataframe\n",
    "data = np.random.multivariate_normal([0, 0], [[1, 2], [2, 20]], size=100)\n",
    "data = pd.DataFrame(data, columns=[\"X\", \"Y\"])\n",
    "mplc.rc(\"figure\", figsize=(6, 6))\n",
    "\n",
    "# Just plot seaborn kde\n",
    "sns.kdeplot(data, cmap=\"BuGn_d\")\n",
    "\n",
    "plt.title(\"Seaborn kdeplot in bokeh.\")\n",
    "\n",
    "show(mpl.to_bokeh())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "planets = sns.load_dataset(\"planets\")\n",
    "\n",
    "tips = sns.load_dataset(\"tips\")\n",
    "\n",
    "sns.set_style(\"whitegrid\")\n",
    "\n",
    "ax = sns.violinplot(x=\"day\", y=\"total_bill\", hue=\"sex\",\n",
    "                    data=tips, palette=\"Set2\", split=True,\n",
    "                    scale=\"count\", inner=\"stick\")\n",
    "\n",
    "plt.title(\"Seaborn violin plot in bokeh.\")\n",
    "\n",
    "show(mpl.to_bokeh())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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
}
