{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pandas as pd\n", "data = pd.read_csv('assets/gapminder.csv', thousands=',', index_col='Year')\n", "data.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.io import output_notebook\n", "output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scatter plot of 2010 - income vs life expectancy " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data.loc[2010].head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.plotting import figure\n", "#p = figure()\n", "#p.circle(x=data.loc[2010].income, y=data.loc[2010].life)\n", "from bokeh.io import show\n", "#show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [01 - plotting](01 - plotting.ipynb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#p = figure(\n", "# height=400, x_axis_type='log', \n", "# x_range=(100, 100000), y_range=(0, 100), \n", "# title='2010', x_axis_label='Income', y_axis_label='Life expectancy'\n", "#)\n", "# MAKE A FUNCTION\n", "#p.circle(x=data.loc[2010].income, y=data.loc[2010].life, color='firebrick')\n", "#show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Column Data Source" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from bokeh.models import ColumnDataSource\n", "source = ColumnDataSource(\n", " {\n", " 'column_1': [1, 2, 3],\n", " 'column_2': [3, 4, 5]\n", " }\n", ")\n", "#source = ColumnDataSource({\n", "# 'income': data.loc[2010].income,\n", "# 'life': data.loc[2010].life,\n", "# 'country': data.loc[2010].Country\n", "#})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [02 - column data source](02 - column data source.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can show regions by color" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "regions = list(data.region.unique())\n", "regions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.palettes import Spectral6\n", "Spectral6" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def get_color(r):\n", " return Spectral6[regions.index(r.region)]\n", "data['region_color'] = data.apply(get_color, axis=1)\n", "data.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#p.circle(x='income', y='life', size=20, alpha=0.6, color='color', source=source)\n", "#show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add a hover" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.models import HoverTool\n", "#hover = HoverTool(tooltips='@country', show_arrow=False)\n", "#p.circle(x='income', y='life', size=20, alpha=0.6, color='color', source=source)\n", "#p.add_tools(hover)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [03 - interactions](03 - interactions.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Examples Interlude - [A1 - Extra Resources](A1 - Extra Resources.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [04 - styling](04 - styling.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [05 - data transformations](05 - data transformations.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Working plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.models import NumeralTickFormatter\n", "\n", "\n", "source = ColumnDataSource({\n", " 'income': data.loc[2010].income,\n", " 'life': data.loc[2010].life,\n", " 'country': data.loc[2010].Country,\n", " 'color': data.loc[2010].region_color,\n", " 'population': data.loc[2010].population\n", "})\n", "\n", "from bokeh.models import LinearInterpolator\n", "size_mapper = LinearInterpolator(\n", " x=[data.population.min(), data.population.max()],\n", " y=[5, 50]\n", ")\n", "\n", "p = figure(\n", " height=400, x_axis_type='log', \n", " x_range=(100, 100000), y_range=(0, 100), \n", " title='2010', x_axis_label='Income', y_axis_label='Life expectancy',\n", " tools=[HoverTool(tooltips='@country', show_arrow=False)]\n", ")\n", "p.xaxis[0].formatter = NumeralTickFormatter(format=\"$0,\")\n", "p.circle(\n", " x='income', y='life', \n", " size={'field': 'population', 'transform': size_mapper}, \n", " color='color',\n", " alpha=0.6, \n", " source=source,\n", ")\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Interactivity with slider" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.io import push_notebook\n", "source = ColumnDataSource({\n", " 'income': data.loc[2010].income,\n", " 'life': data.loc[2010].life,\n", " 'country': data.loc[2010].Country,\n", " 'color': data.loc[2010].region_color,\n", " 'population': data.loc[2010].population\n", "})\n", "\n", "def update(year):\n", " new_data = dict(\n", " income=data.loc[year].income, \n", " life=data.loc[year].life, \n", " country=data.loc[year].Country,\n", " population=data.loc[year].population,\n", " color=data.loc[year].region_color,\n", " )\n", " source.data = new_data\n", " p.title.text = str(year)\n", " push_notebook()\n", " \n", "size_mapper = LinearInterpolator(\n", " x=[data.population.min(), data.population.max()],\n", " y=[5, 50]\n", ")\n", "p = figure(\n", " height=400, x_axis_type='log', \n", " x_range=(100, 100000), y_range=(0, 100), \n", " title='2010', x_axis_label='Income', y_axis_label='Life expectancy',\n", " tools=[HoverTool(tooltips='@country', show_arrow=False)]\n", ")\n", "p.xaxis[0].formatter = NumeralTickFormatter(format=\"$0,\")\n", "p.circle(\n", " x='income', y='life', \n", " size={'field': 'population', 'transform': size_mapper}, \n", " color='color',\n", " alpha=0.6, \n", " source=source,\n", ")\n", "show(p, notebook_handle=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ipywidgets import interact, IntSlider\n", "slider = IntSlider(min=1960, max=2014, value=2010)\n", "interact(update, year=slider)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [06 - server](06 - server.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [07 - sharing](07 - sharing.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we haven't talked about it yet:\n", "- tab completing in notebook\n", "- fuzzy search" ] }, { "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.5.2" }, "widgets": { "state": { "a39e4c56518748ce9e27dc931a445be1": { "views": [ { "cell_index": 25 } ] } }, "version": "1.2.0" } }, "nbformat": 4, "nbformat_minor": 1 }