{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "

Bokeh Tutorial

\n", "
\n", "\n", "

01. Basic Plotting Interface

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This section of the tutorial covers the [`bokeh.plotting`](http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html) \n", "interface. This interface is a \"mid-level\" interface, and the main idea can be described by the statement:\n", "\n", "**Starting from simple default figures (with sensible default tools, grids and axes), add markers and other shapes whose visual attributes are tied to directly data.**\n", "\n", "We will see that it is possible to customize and change all of the defaults, but having them means that it is possible to get up and running very quickly. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Imports and Setup\n", "\n", "When using the [`bokeh.plotting`](http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html) interface, there are a few common imports:\n", "* Use the [`figure`](http://bokeh.pydata.org/en/latest/docs/reference/plotting.html#bokeh.plotting.figure) function to create new plot objects to work with. \n", "* Call the functions [`output_file`](http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh.io.output_file), [`output_notebook`](http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh.io.output_notebook), and [`output_server`](http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh.io.output_server) (possibly in combination) to tell Bokeh how to display or save output. \n", "* Execute [`show`](http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh.io.show) and [`save`](http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#bokeh.io.save) to display or save plots and layouts." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.io import output_notebook, show\n", "from bokeh.plotting import figure" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, we are in the Jupyter notebook, so call `output_notebook()`. We only need to call this once, and all subsequent calls to `show()` will display inline in the notebook." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(global) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " var force = \"1\";\n", "\n", " if (typeof (window._bokeh_onload_callbacks) === \"undefined\" || force !== \"\") {\n", " window._bokeh_onload_callbacks = [];\n", " window._bokeh_is_loading = undefined;\n", " }\n", "\n", "\n", " \n", " if (typeof (window._bokeh_timeout) === \"undefined\" || force !== \"\") {\n", " window._bokeh_timeout = Date.now() + 5000;\n", " window._bokeh_failed_load = false;\n", " }\n", "\n", " var NB_LOAD_WARNING = {'data': {'text/html':\n", " \"
\\n\"+\n", " \"

\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"

\\n\"+\n", " \"\\n\"+\n", " \"\\n\"+\n", " \"from bokeh.resources import INLINE\\n\"+\n", " \"output_notebook(resources=INLINE)\\n\"+\n", " \"\\n\"+\n", " \"
\"}};\n", "\n", " function display_loaded() {\n", " if (window.Bokeh !== undefined) {\n", " Bokeh.$(\"#02cc5827-0d45-4b08-b5b6-a44561eba1a6\").text(\"BokehJS successfully loaded.\");\n", " } else if (Date.now() < window._bokeh_timeout) {\n", " setTimeout(display_loaded, 100)\n", " }\n", " }\n", "\n", " function run_callbacks() {\n", " window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n", " delete window._bokeh_onload_callbacks\n", " console.info(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(js_urls, callback) {\n", " window._bokeh_onload_callbacks.push(callback);\n", " if (window._bokeh_is_loading > 0) {\n", " console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " window._bokeh_is_loading = js_urls.length;\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " var s = document.createElement('script');\n", " s.src = url;\n", " s.async = false;\n", " s.onreadystatechange = s.onload = function() {\n", " window._bokeh_is_loading--;\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: all BokehJS libraries loaded\");\n", " run_callbacks()\n", " }\n", " };\n", " s.onerror = function() {\n", " console.warn(\"failed to load library \" + url);\n", " };\n", " console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", " }\n", " };var element = document.getElementById(\"02cc5827-0d45-4b08-b5b6-a44561eba1a6\");\n", " if (element == null) {\n", " console.log(\"Bokeh: ERROR: autoload.js configured with elementid '02cc5827-0d45-4b08-b5b6-a44561eba1a6' but no matching script tag was found. \")\n", " return false;\n", " }\n", "\n", " var js_urls = ['https://cdn.pydata.org/bokeh/dev/bokeh-0.12.2rc3.min.js', 'https://cdn.pydata.org/bokeh/dev/bokeh-widgets-0.12.2rc3.min.js', 'https://cdn.pydata.org/bokeh/dev/bokeh-compiler-0.12.2rc3.min.js'];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " \n", " Bokeh.$(\"#02cc5827-0d45-4b08-b5b6-a44561eba1a6\").text(\"BokehJS is loading...\");\n", " },\n", " function(Bokeh) {\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/dev/bokeh-0.12.2rc3.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/dev/bokeh-0.12.2rc3.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/dev/bokeh-widgets-0.12.2rc3.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/dev/bokeh-widgets-0.12.2rc3.min.css\");\n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " \n", " if ((window.Bokeh !== undefined) || (force === \"1\")) {\n", " for (var i = 0; i < inline_js.length; i++) {\n", " inline_js[i](window.Bokeh);\n", " }if (force === \"1\") {\n", " display_loaded();\n", " }} else if (Date.now() < window._bokeh_timeout) {\n", " setTimeout(run_inline_js, 100);\n", " } else if (!window._bokeh_failed_load) {\n", " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", " window._bokeh_failed_load = true;\n", " } else if (!force) {\n", " var cell = $(\"#02cc5827-0d45-4b08-b5b6-a44561eba1a6\").parents('.cell').data().cell;\n", " cell.output_area.append_execute_result(NB_LOAD_WARNING)\n", " }\n", "\n", " }\n", "\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(js_urls, function() {\n", " console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(this));" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Some Basic Scatter Plots\n", "\n", "In this section you will see how to use Bokeh's various marker types to create simple scatter plots. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# create a new plot with default tools, using figure\n", "p = figure(plot_width=400, plot_height=400)\n", "\n", "# add a circle renderer with a size, color, and alpha\n", "p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15, line_color=\"navy\", fill_color=\"orange\", fill_alpha=0.5)\n", "\n", "show(p) # show the results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All Bokeh markers accept `size` (measured in screen space units) as a property. Circles also have `radius` (measured in \"data\" space units). " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# EXERCISE: Try changing the example above to set a `radius` value instead of `size`\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To scatter square markers instead of circles, you can use the `square` method on figures. " ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# create a new plot using figure\n", "p = figure(plot_width=400, plot_height=400)\n", "\n", "# add a square renderer with a size, color, alpha, and sizes\n", "p.square([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=[10, 15, 20, 25, 30], color=\"firebrick\", alpha=0.6)\n", "\n", "show(p) # show the results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that in the example above, we are also specifying different sizes for each individual marker. ***In general, all of a glyph's properties can be \"vectorized\" in this fashion. *** Also note that we have passed ``color`` as a shorthand to set both the line and fill colors easily at the same time. This is a convenience specific to ``bokeh.plotting``.\n", "\n", "

There are many marker types available in Bokeh, you can see details and\n", "example plots for all of them in the reference guide by clicking on entries in the list below:

\n", "
\n", "\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# EXERCISE: Plot some different markers in this cell\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Line Plots" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# create a new plot (with a title) using figure\n", "p = figure(plot_width=400, plot_height=400, title=\"My Line Plot\")\n", "\n", "# add a line renderer\n", "p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2)\n", "\n", "show(p) # show the results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Images" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The example below shows how to use the `image_rgba` method to display raw RGBA data. \n", "\n", "**Note**: This example makes use of the NumPy library " ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from __future__ import division\n", "import numpy as np\n", "\n", "# set up some data\n", "N = 20\n", "img = np.empty((N,N), dtype=np.uint32)\n", "view = img.view(dtype=np.uint8).reshape((N, N, 4))\n", "for i in range(N):\n", " for j in range(N):\n", " view[i, j, 0] = int(i/N*255) # red\n", " view[i, j, 1] = 158 # green\n", " view[i, j, 2] = int(j/N*255) # blue\n", " view[i, j, 3] = 255 # alpha\n", " \n", "# create a new plot (with a fixed range) using figure\n", "p = figure(x_range=[0,10], y_range=[0,10])\n", "\n", "# add an RGBA image renderer\n", "p.image_rgba(image=[img], x=[0], y=[0], dw=[10], dh=[10])\n", "\n", "show(p) # show the results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other Kinds of Glyphs\n", "\n", "Bokeh supports many other kinds of glyphs. You can click on the User Guide links below to see how to create plots with these glyphs using the [`bokeh.plotting`](http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html) interface.\n", "\n", "* [Rectangles and Ovals](http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#rectangles-and-ovals)\n", "* [Segments and Rays](http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#segments-and-rays)\n", "* [Wedges and Arcs](http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#wedges-and-arcs)\n", "* [Specialized Curves](http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#specialized-curves)\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# EXERCISE: Plot some of the other glyph types, following the examples in the User Guide. \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plots with Multiple Glyphs \n", "\n", "It is possible to combine more than one glyph on a single figure. You just need to call multiple glyph methods on one figure object:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# set up some data\n", "x = [1, 2, 3, 4, 5]\n", "y = [6, 7, 8, 7, 3]\n", "\n", "# create a new plot with figure\n", "p = figure(plot_width=400, plot_height=400)\n", "\n", "# add both a line and circles on the same plot\n", "p.line(x, y, line_width=2)\n", "p.circle(x, y, fill_color=\"white\", size=8)\n", "\n", "show(p) # show the results" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# EXERCISE: create your own plot combining multiple glyphs together\n", "\n" ] } ], "metadata": { "anaconda-cloud": {}, "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" } }, "nbformat": 4, "nbformat_minor": 0 }