{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 6.3. Creating interactive Web visualizations with Bokeh" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Let's import NumPy and Bokeh. We need to call `output_notebook` in order to tell Bokeh to render plots in the IPython notebook." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ " \n", " \n", " \n", " \n", "
\n", "Warning: BokehJS previously loaded
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import bokeh.plotting as bkh\n", "bkh.output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Let's create some random data." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.linspace(0., 1., 100)\n", "y = np.cumsum(np.random.randn(100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Let's draw a curve." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#bkh.line(x, y, line_width=5)\n", "#bkh.show()\n", "\n", "p = bkh.figure()\n", "p.line(x=x, y=y)\n", "bkh.show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An interactive plot is rendered in the notebook. We can pan and zoom by clicking on the buttons above the plot." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Let's move to another example. We first load a sample dataset (*Iris flowers*). We also generate some colors based on the species of the flowers." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.sampledata.iris import flowers\n", "colormap = {'setosa': 'red',\n", " 'versicolor': 'green',\n", " 'virginica': 'blue'}\n", "flowers['color'] = flowers['species'].map(lambda x: colormap[x])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Now, we render an interactive scatter plot." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "p = bkh.figure()\n", "p.scatter(flowers[\"petal_length\"], \n", " flowers[\"petal_width\"],\n", " color=flowers[\"color\"], \n", " fill_alpha=0.25, size=10,)\n", "bkh.show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 0 }