{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Plots with categorical variables\n", "\n", "[Data set download](https://s3.amazonaws.com/bebi103.caltech.edu/data/gfmt_sleep.csv)\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(root) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " var force = true;\n", "\n", " if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n", " root._bokeh_onload_callbacks = [];\n", " root._bokeh_is_loading = undefined;\n", " }\n", "\n", " var JS_MIME_TYPE = 'application/javascript';\n", " var HTML_MIME_TYPE = 'text/html';\n", " var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n", " var CLASS_NAME = 'output_bokeh rendered_html';\n", "\n", " /**\n", " * Render data to the DOM node\n", " */\n", " function render(props, node) {\n", " var script = document.createElement(\"script\");\n", " node.appendChild(script);\n", " }\n", "\n", " /**\n", " * Handle when an output is cleared or removed\n", " */\n", " function handleClearOutput(event, handle) {\n", " var cell = handle.cell;\n", "\n", " var id = cell.output_area._bokeh_element_id;\n", " var server_id = cell.output_area._bokeh_server_id;\n", " // Clean up Bokeh references\n", " if (id != null && id in Bokeh.index) {\n", " Bokeh.index[id].model.document.clear();\n", " delete Bokeh.index[id];\n", " }\n", "\n", " if (server_id !== undefined) {\n", " // Clean up Bokeh references\n", " var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n", " cell.notebook.kernel.execute(cmd, {\n", " iopub: {\n", " output: function(msg) {\n", " var id = msg.content.text.trim();\n", " if (id in Bokeh.index) {\n", " Bokeh.index[id].model.document.clear();\n", " delete Bokeh.index[id];\n", " }\n", " }\n", " }\n", " });\n", " // Destroy server and session\n", " var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n", " cell.notebook.kernel.execute(cmd);\n", " }\n", " }\n", "\n", " /**\n", " * Handle when a new output is added\n", " */\n", " function handleAddOutput(event, handle) {\n", " var output_area = handle.output_area;\n", " var output = handle.output;\n", "\n", " // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n", " if ((output.output_type != \"display_data\") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n", " return\n", " }\n", "\n", " var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n", "\n", " if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n", " toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n", " // store reference to embed id on output_area\n", " output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n", " }\n", " if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n", " var bk_div = document.createElement(\"div\");\n", " bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n", " var script_attrs = bk_div.children[0].attributes;\n", " for (var i = 0; i < script_attrs.length; i++) {\n", " toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n", " }\n", " // store reference to server id on output_area\n", " output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n", " }\n", " }\n", "\n", " function register_renderer(events, OutputArea) {\n", "\n", " function append_mime(data, metadata, element) {\n", " // create a DOM node to render to\n", " var toinsert = this.create_output_subarea(\n", " metadata,\n", " CLASS_NAME,\n", " EXEC_MIME_TYPE\n", " );\n", " this.keyboard_manager.register_events(toinsert);\n", " // Render to node\n", " var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n", " render(props, toinsert[toinsert.length - 1]);\n", " element.append(toinsert);\n", " return toinsert\n", " }\n", "\n", " /* Handle when an output is cleared or removed */\n", " events.on('clear_output.CodeCell', handleClearOutput);\n", " events.on('delete.Cell', handleClearOutput);\n", "\n", " /* Handle when a new output is added */\n", " events.on('output_added.OutputArea', handleAddOutput);\n", "\n", " /**\n", " * Register the mime type and append_mime function with output_area\n", " */\n", " OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n", " /* Is output safe? */\n", " safe: true,\n", " /* Index of renderer in `output_area.display_order` */\n", " index: 0\n", " });\n", " }\n", "\n", " // register the mime type if in Jupyter Notebook environment and previously unregistered\n", " if (root.Jupyter !== undefined) {\n", " var events = require('base/js/events');\n", " var OutputArea = require('notebook/js/outputarea').OutputArea;\n", "\n", " if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n", " register_renderer(events, OutputArea);\n", " }\n", " }\n", "\n", " \n", " if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n", " root._bokeh_timeout = Date.now() + 5000;\n", " root._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", " var el = document.getElementById(\"1001\");\n", " if (el != null) {\n", " el.textContent = \"BokehJS is loading...\";\n", " }\n", " if (root.Bokeh !== undefined) {\n", " if (el != null) {\n", " el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n", " }\n", " } else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(display_loaded, 100)\n", " }\n", " }\n", "\n", "\n", " function run_callbacks() {\n", " try {\n", " root._bokeh_onload_callbacks.forEach(function(callback) {\n", " if (callback != null)\n", " callback();\n", " });\n", " } finally {\n", " delete root._bokeh_onload_callbacks\n", " }\n", " console.debug(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(css_urls, js_urls, callback) {\n", " if (css_urls == null) css_urls = [];\n", " if (js_urls == null) js_urls = [];\n", "\n", " root._bokeh_onload_callbacks.push(callback);\n", " if (root._bokeh_is_loading > 0) {\n", " console.debug(\"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.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " root._bokeh_is_loading = css_urls.length + js_urls.length;\n", "\n", " function on_load() {\n", " root._bokeh_is_loading--;\n", " if (root._bokeh_is_loading === 0) {\n", " console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n", " run_callbacks()\n", " }\n", " }\n", "\n", " function on_error() {\n", " console.error(\"failed to load \" + url);\n", " }\n", "\n", " for (var i = 0; i < css_urls.length; i++) {\n", " var url = css_urls[i];\n", " const element = document.createElement(\"link\");\n", " element.onload = on_load;\n", " element.onerror = on_error;\n", " element.rel = \"stylesheet\";\n", " element.type = \"text/css\";\n", " element.href = url;\n", " console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n", " document.body.appendChild(element);\n", " }\n", "\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " var element = document.createElement('script');\n", " element.onload = on_load;\n", " element.onerror = on_error;\n", " element.async = false;\n", " element.src = url;\n", " console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.head.appendChild(element);\n", " }\n", " };var element = document.getElementById(\"1001\");\n", " if (element == null) {\n", " console.error(\"Bokeh: ERROR: autoload.js configured with elementid '1001' but no matching script tag was found. \")\n", " return false;\n", " }\n", "\n", " function inject_raw_css(css) {\n", " const element = document.createElement(\"style\");\n", " element.appendChild(document.createTextNode(css));\n", " document.body.appendChild(element);\n", " }\n", "\n", " var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-1.3.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.3.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.3.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-1.3.4.min.js\"];\n", " var css_urls = [];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " \n", " },\n", " function(Bokeh) {} // ensure no trailing comma for IE\n", " ];\n", "\n", " function run_inline_js() {\n", " \n", " if ((root.Bokeh !== undefined) || (force === true)) {\n", " for (var i = 0; i < inline_js.length; i++) {\n", " inline_js[i].call(root, root.Bokeh);\n", " }if (force === true) {\n", " display_loaded();\n", " }} else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(run_inline_js, 100);\n", " } else if (!root._bokeh_failed_load) {\n", " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", " root._bokeh_failed_load = true;\n", " } else if (force !== true) {\n", " var cell = $(document.getElementById(\"1001\")).parents('.cell').data().cell;\n", " cell.output_area.append_execute_result(NB_LOAD_WARNING)\n", " }\n", "\n", " }\n", "\n", " if (root._bokeh_is_loading === 0) {\n", " console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(css_urls, js_urls, function() {\n", " console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(window));" ], "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n function now() {\n return new Date();\n }\n\n var force = true;\n\n if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\n \n\n \n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._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 var el = document.getElementById(\"1001\");\n if (el != null) {\n el.textContent = \"BokehJS is loading...\";\n }\n if (root.Bokeh !== undefined) {\n if (el != null) {\n el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n }\n } else if (Date.now() < root._bokeh_timeout) {\n setTimeout(display_loaded, 100)\n }\n }\n\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) {\n if (callback != null)\n callback();\n });\n } finally {\n delete root._bokeh_onload_callbacks\n }\n console.debug(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(css_urls, js_urls, callback) {\n if (css_urls == null) css_urls = [];\n if (js_urls == null) js_urls = [];\n\n root._bokeh_onload_callbacks.push(callback);\n if (root._bokeh_is_loading > 0) {\n console.debug(\"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.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n root._bokeh_is_loading = css_urls.length + js_urls.length;\n\n function on_load() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n run_callbacks()\n }\n }\n\n function on_error() {\n console.error(\"failed to load \" + url);\n }\n\n for (var i = 0; i < css_urls.length; i++) {\n var url = css_urls[i];\n const element = document.createElement(\"link\");\n element.onload = on_load;\n element.onerror = on_error;\n element.rel = \"stylesheet\";\n element.type = \"text/css\";\n element.href = url;\n console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n document.body.appendChild(element);\n }\n\n for (var i = 0; i < js_urls.length; i++) {\n var url = js_urls[i];\n var element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error;\n element.async = false;\n element.src = url;\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.head.appendChild(element);\n }\n };var element = document.getElementById(\"1001\");\n if (element == null) {\n console.error(\"Bokeh: ERROR: autoload.js configured with elementid '1001' but no matching script tag was found. \")\n return false;\n }\n\n function inject_raw_css(css) {\n const element = document.createElement(\"style\");\n element.appendChild(document.createTextNode(css));\n document.body.appendChild(element);\n }\n\n var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-1.3.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.3.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.3.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-1.3.4.min.js\"];\n var css_urls = [];\n\n var inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n \n function(Bokeh) {\n \n },\n function(Bokeh) {} // ensure no trailing comma for IE\n ];\n\n function run_inline_js() {\n \n if ((root.Bokeh !== undefined) || (force === true)) {\n for (var i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }if (force === true) {\n display_loaded();\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n } else if (force !== true) {\n var cell = $(document.getElementById(\"1001\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\n }\n\n }\n\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n run_inline_js();\n } else {\n load_libs(css_urls, js_urls, function() {\n console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n run_inline_js();\n });\n }\n}(window));" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "\n", "import bokeh.models\n", "import bokeh.plotting\n", "import bokeh.io\n", "bokeh.io.output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Types of data for plots\n", "\n", "Let us first consider the different kinds of data we may encounter as we think about constructing a plot.\n", "\n", "- **Quantitative** data may have continuously varying (and therefore ordered) values.\n", "- **Categorical** data has discrete, unordered values that a variable can take.\n", "- **Ordinal** data has discrete, ordered values. Integers are a classic example.\n", "- **Temporal** data refers to time, which can be represented as dates.\n", "\n", "In practice, ordinal data can be cast as quantitative or treated as categorical with an ordering enforced on the categories (e.g., categorical data `[1, 2, 3]` becomes `['1', '2', '3']`.). Temporal data can also be cast as quantitative, (e.g., seconds from the start time). We will therefore focus out attention on quantitative and categorical data.\n", "\n", "When we made scatter plots (note lowercase \"scatter;\" we actually used `hv.Points` because we had two independent variables) in the previous lesson, both types of data were quantitative. We did actually incorporate categorical information in the form of colors of the glyph (insomniacs and normal sleepers being colored differently) and in tooltips.\n", "\n", "But what if we wanted a single type of measurement, as percent correct in the facial identification, but were interested in how well insomniacs versus normal sleepers performed. Here, we have the quantitative percent correct data and the categorical sleeper type. One of our axes is now categorical.\n", "\n", "Note that this kind of plot is commonly encountered in the biological sciences. We repeat a measurement many times for given test conditions and wish to compare the results. The different conditions are the categories, and the axis along which the conditions are represented is called a categorical axis. The quantitative axis contains the result of the measurements from each condition.\n", "\n", "
\n", "\n", "_The rest of this lesson is mostly for reference so you can see how to handle categorical axes with Bokeh. In practice, we will be using [Bokeh-catplot](https://github.com/justinbois/bokeh-catplot) and [HoloViews](https://holoviews.org/) to do this and it is done for your automatically. You may therefore skip the rest of this notebook if you like._" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Making a bar graph with Bokeh\n", "\n", "To demonstrate how to set up a categorical axis with Bokeh, I will make a bar graph of the mean percent correct for insomniacs and normal sleepers. But before I even begin this, I will give you the following piece of advice: *Don't make bar graphs.* More on that in a moment." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setting up a data frame for plotting\n", "\n", "Before we do that, we need to set up a data frame to make the plot. We start by reading in the data set and computing the `'insomnia'` column, which gives `True`s and `False`s, as we've done in the preceding parts of this lesson." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('../data/gfmt_sleep.csv', na_values='*')\n", "df['insomnia'] = df['sci'] <= 16" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For convenience in plotting the categorical axis, we would rather not have the values on the axis be `True` or `False`, but something more descriptive, like _insomniac_ and _normal_. So, let's make a column in the data frame, `'sleeper'` that has that for us. We use the `apply()` method of the data frame to apply a function that returns the string `'insomniac'` if the entry is in the `'insomnia'` column is `True` and `'normal'` otherwise." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "df['sleeper'] = df['insomnia'].apply(lambda x: 'insomniac' if x else 'normal')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we need to make a data frame that has the mean percent correct for each of the two categories of sleeper. We have decided that it is the mean of the respective measurements that will set the height of the bars." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sleeperpercent correct
0insomniac76.100000
1normal81.461039
\n", "
" ], "text/plain": [ " sleeper percent correct\n", "0 insomniac 76.100000\n", "1 normal 81.461039" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_mean = df.groupby('sleeper')['percent correct'].mean().reset_index()\n", "\n", "# Take a look\n", "df_mean" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we're ready to make the bar graph. Note that we now have only two data points that we are showing on the plot. We have decided to throw out a **lot** of information from the data we collected to display only two values. Does this strike you as a terrible idea? It should. Don't do this. We're just doing it to show how categorical axes are set up using Bokeh." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setting up categorical axes\n", "\n", "To set up a categorical axis, you need to specify the `x_range` (or `y_range` if you want the y-axis to be categorical) as a list with the categories you want on the axis when you instantiate the figure. I will make a horizontal bar graph, so I will specify `y_range`. I also want my quantiative axis (x in this case) to go from zero to 100, since it signifies a percent. Also, when I instantiate this figure, because it is not very tall and I do not want the reset tool cut off, I will also explicitly set the tools I want in the toolbar." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "p = bokeh.plotting.figure(\n", " height=200,\n", " width=400,\n", " x_axis_label='percent correct',\n", " x_range=[0, 100],\n", " y_range=df_mean['sleeper'].unique(),\n", " tools='save'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have the figure, we can put the bars on. The `p.hbar()` method populates the figure with horizontal bar glyphs. The `right` kwarg says what column of the data source dictates how far to the right to show the bar, while the `height` kwarg says how think the bars are.\n", "\n", "I will also ensure the quantitative axis starts at zero and turn off the grid lines on the categorical axis, which is commonly done." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " \n", " var docs_json = {\"b0953dd8-96b0-4188-82ae-b13c3022756f\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1011\",\"type\":\"LinearAxis\"}],\"center\":[{\"id\":\"1015\",\"type\":\"Grid\"},{\"id\":\"1019\",\"type\":\"Grid\"}],\"left\":[{\"id\":\"1016\",\"type\":\"CategoricalAxis\"}],\"plot_height\":200,\"plot_width\":400,\"renderers\":[{\"id\":\"1027\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"1029\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"1021\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"1003\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"1007\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"1005\",\"type\":\"FactorRange\"},\"y_scale\":{\"id\":\"1009\",\"type\":\"CategoricalScale\"}},\"id\":\"1002\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"height\":{\"value\":0.6},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"right\":{\"field\":\"percent correct\"},\"y\":{\"field\":\"sleeper\"}},\"id\":\"1026\",\"type\":\"HBar\"},{\"attributes\":{\"data_source\":{\"id\":\"1023\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1025\",\"type\":\"HBar\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1026\",\"type\":\"HBar\"},\"selection_glyph\":null,\"view\":{\"id\":\"1028\",\"type\":\"CDSView\"}},\"id\":\"1027\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"source\":{\"id\":\"1023\",\"type\":\"ColumnDataSource\"}},\"id\":\"1028\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"1007\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1032\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"callback\":null,\"data\":{\"index\":[0,1],\"percent correct\":{\"__ndarray__\":\"ZmZmZmYGU0AH9o6pgV1UQA==\",\"dtype\":\"float64\",\"shape\":[2]},\"sleeper\":[\"insomniac\",\"normal\"]},\"selected\":{\"id\":\"1035\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1036\",\"type\":\"UnionRenderers\"}},\"id\":\"1023\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"dimension\":1,\"grid_line_color\":null,\"ticker\":{\"id\":\"1017\",\"type\":\"CategoricalTicker\"}},\"id\":\"1019\",\"type\":\"Grid\"},{\"attributes\":{\"text\":\"\"},\"id\":\"1029\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"1009\",\"type\":\"CategoricalScale\"},{\"attributes\":{},\"id\":\"1035\",\"type\":\"Selection\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"height\":{\"value\":0.6},\"line_color\":{\"value\":\"#1f77b4\"},\"right\":{\"field\":\"percent correct\"},\"y\":{\"field\":\"sleeper\"}},\"id\":\"1025\",\"type\":\"HBar\"},{\"attributes\":{\"axis_label\":\"percent correct\",\"formatter\":{\"id\":\"1032\",\"type\":\"BasicTickFormatter\"},\"ticker\":{\"id\":\"1012\",\"type\":\"BasicTicker\"}},\"id\":\"1011\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1012\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"1034\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{},\"id\":\"1036\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"ticker\":{\"id\":\"1012\",\"type\":\"BasicTicker\"}},\"id\":\"1015\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1017\",\"type\":\"CategoricalTicker\"},{\"attributes\":{},\"id\":\"1020\",\"type\":\"SaveTool\"},{\"attributes\":{\"formatter\":{\"id\":\"1034\",\"type\":\"CategoricalTickFormatter\"},\"ticker\":{\"id\":\"1017\",\"type\":\"CategoricalTicker\"}},\"id\":\"1016\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1020\",\"type\":\"SaveTool\"}]},\"id\":\"1021\",\"type\":\"Toolbar\"},{\"attributes\":{\"callback\":null,\"factors\":[\"insomniac\",\"normal\"]},\"id\":\"1005\",\"type\":\"FactorRange\"},{\"attributes\":{\"callback\":null,\"end\":100},\"id\":\"1003\",\"type\":\"Range1d\"}],\"root_ids\":[\"1002\"]},\"title\":\"Bokeh Application\",\"version\":\"1.3.4\"}};\n", " var render_items = [{\"docid\":\"b0953dd8-96b0-4188-82ae-b13c3022756f\",\"roots\":{\"1002\":\"8e89c0ca-5459-46b5-912a-c820d05db6a8\"}}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n", "\n", " }\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " var attempts = 0;\n", " var timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " clearInterval(timer);\n", " }\n", " attempts++;\n", " if (attempts > 100) {\n", " console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n", " clearInterval(timer);\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "1002" } }, "output_type": "display_data" } ], "source": [ "p.hbar(\n", " source=df_mean,\n", " y='sleeper',\n", " right='percent correct',\n", " height=0.6,\n", ")\n", "\n", "# Turn off gridlines on categorical axis\n", "p.ygrid.grid_line_color = None\n", "\n", "bokeh.io.show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We similarly make vertical bar graphs specifying `x_range` and using `p.vbar()`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " \n", " var docs_json = {\"325a73dc-802f-43cb-affb-ead1c1557084\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1094\",\"type\":\"CategoricalAxis\"}],\"center\":[{\"id\":\"1097\",\"type\":\"Grid\"},{\"id\":\"1102\",\"type\":\"Grid\"}],\"left\":[{\"id\":\"1098\",\"type\":\"LinearAxis\"}],\"plot_height\":250,\"plot_width\":250,\"renderers\":[{\"id\":\"1120\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"1130\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"1109\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"1086\",\"type\":\"FactorRange\"},\"x_scale\":{\"id\":\"1090\",\"type\":\"CategoricalScale\"},\"y_range\":{\"id\":\"1088\",\"type\":\"Range1d\"},\"y_scale\":{\"id\":\"1092\",\"type\":\"LinearScale\"}},\"id\":\"1085\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"overlay\":{\"id\":\"1138\",\"type\":\"BoxAnnotation\"}},\"id\":\"1105\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"1136\",\"type\":\"Selection\"},{\"attributes\":{\"formatter\":{\"id\":\"1133\",\"type\":\"CategoricalTickFormatter\"},\"ticker\":{\"id\":\"1095\",\"type\":\"CategoricalTicker\"}},\"id\":\"1094\",\"type\":\"CategoricalAxis\"},{\"attributes\":{},\"id\":\"1092\",\"type\":\"LinearScale\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1103\",\"type\":\"PanTool\"},{\"id\":\"1104\",\"type\":\"WheelZoomTool\"},{\"id\":\"1105\",\"type\":\"BoxZoomTool\"},{\"id\":\"1106\",\"type\":\"SaveTool\"},{\"id\":\"1107\",\"type\":\"ResetTool\"},{\"id\":\"1108\",\"type\":\"HelpTool\"}]},\"id\":\"1109\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1090\",\"type\":\"CategoricalScale\"},{\"attributes\":{},\"id\":\"1099\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"1095\",\"type\":\"CategoricalTicker\"},{\"attributes\":{},\"id\":\"1135\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"callback\":null,\"data\":{\"index\":[0,1],\"percent correct\":{\"__ndarray__\":\"ZmZmZmYGU0AH9o6pgV1UQA==\",\"dtype\":\"float64\",\"shape\":[2]},\"sleeper\":[\"insomniac\",\"normal\"]},\"selected\":{\"id\":\"1136\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1137\",\"type\":\"UnionRenderers\"}},\"id\":\"1116\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"dimension\":1,\"ticker\":{\"id\":\"1099\",\"type\":\"BasicTicker\"}},\"id\":\"1102\",\"type\":\"Grid\"},{\"attributes\":{\"callback\":null,\"factors\":[\"normal\",\"insomniac\"]},\"id\":\"1086\",\"type\":\"FactorRange\"},{\"attributes\":{\"callback\":null,\"end\":100},\"id\":\"1088\",\"type\":\"Range1d\"},{\"attributes\":{\"axis_label\":\"percent correct\",\"formatter\":{\"id\":\"1135\",\"type\":\"BasicTickFormatter\"},\"ticker\":{\"id\":\"1099\",\"type\":\"BasicTicker\"}},\"id\":\"1098\",\"type\":\"LinearAxis\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"percent correct\"},\"width\":{\"value\":0.6},\"x\":{\"field\":\"sleeper\"}},\"id\":\"1118\",\"type\":\"VBar\"},{\"attributes\":{\"source\":{\"id\":\"1116\",\"type\":\"ColumnDataSource\"}},\"id\":\"1121\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"1104\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"grid_line_color\":null,\"ticker\":{\"id\":\"1095\",\"type\":\"CategoricalTicker\"}},\"id\":\"1097\",\"type\":\"Grid\"},{\"attributes\":{\"data_source\":{\"id\":\"1116\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1118\",\"type\":\"VBar\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1119\",\"type\":\"VBar\"},\"selection_glyph\":null,\"view\":{\"id\":\"1121\",\"type\":\"CDSView\"}},\"id\":\"1120\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1103\",\"type\":\"PanTool\"},{\"attributes\":{\"text\":\"\"},\"id\":\"1130\",\"type\":\"Title\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"percent correct\"},\"width\":{\"value\":0.6},\"x\":{\"field\":\"sleeper\"}},\"id\":\"1119\",\"type\":\"VBar\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"1138\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1133\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{},\"id\":\"1107\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"1137\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1106\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1108\",\"type\":\"HelpTool\"}],\"root_ids\":[\"1085\"]},\"title\":\"Bokeh Application\",\"version\":\"1.3.4\"}};\n", " var render_items = [{\"docid\":\"325a73dc-802f-43cb-affb-ead1c1557084\",\"roots\":{\"1085\":\"00a3630a-7755-47c4-801f-af08a02948a4\"}}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n", "\n", " }\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " var attempts = 0;\n", " var timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " clearInterval(timer);\n", " }\n", " attempts++;\n", " if (attempts > 100) {\n", " console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n", " clearInterval(timer);\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "1085" } }, "output_type": "display_data" } ], "source": [ "p = bokeh.plotting.figure(\n", " height=250,\n", " width=250,\n", " x_range=df_mean['sleeper'].unique()[::-1],\n", " y_range=[0, 100],\n", " y_axis_label='percent correct',\n", ")\n", "\n", "p.vbar(\n", " source=df_mean,\n", " x='sleeper',\n", " top='percent correct',\n", " width=0.6\n", ")\n", "\n", "p.xgrid.grid_line_color = None\n", "\n", "bokeh.io.show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nested categorical axes\n", "\n", "We may wish to make a bar graph where we have four bars, normal and insomniac for males and also normal and insomniac for females. To start, we will have to re-make the `df_mean` data frame, now grouping by gender and sleeper. Furthermore, it will be nicer to label the categories as \"female\" and \"male\" instead of \"f\" and \"m\"." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
gendersleeperpercent correct
0femaleinsomniac73.947368
1femalenormal82.045455
2maleinsomniac82.916667
3malenormal80.000000
\n", "
" ], "text/plain": [ " gender sleeper percent correct\n", "0 female insomniac 73.947368\n", "1 female normal 82.045455\n", "2 male insomniac 82.916667\n", "3 male normal 80.000000" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[\"gender\"] = df[\"gender\"].apply(lambda x: \"female\" if x == \"f\" else \"male\")\n", "\n", "df_mean = df.groupby([\"gender\", \"sleeper\"])[\"percent correct\"].mean().reset_index()\n", "\n", "# Take a look\n", "df_mean" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because of the way Bokeh handles nested categories, we need to create a new column that has a tuple corresponding to the nested category. To make the tuple, we can again apply a function, this time to each entire row of the data frame (which requires the `axis=1` kwarg of `df_mean.apply()`." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
gendersleeperpercent correctcats
0femaleinsomniac73.947368(female, insomniac)
1femalenormal82.045455(female, normal)
2maleinsomniac82.916667(male, insomniac)
3malenormal80.000000(male, normal)
\n", "
" ], "text/plain": [ " gender sleeper percent correct cats\n", "0 female insomniac 73.947368 (female, insomniac)\n", "1 female normal 82.045455 (female, normal)\n", "2 male insomniac 82.916667 (male, insomniac)\n", "3 male normal 80.000000 (male, normal)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_mean['cats'] = df_mean.apply(lambda x: (x['gender'], x['sleeper']), axis=1)\n", "\n", "# Take a look\n", "df_mean" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we need to set up **factors**, which give the nested categories. We could extract them from the `'cats'` column of the data frame as\n", "\n", "```python\n", "factors = list(df_mean.cats)\n", "```\n", "\n", "Instead, we will specify them by hand to ensure they are ordered as we would like." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "factors = [\n", " (\"female\", \"normal\"),\n", " (\"female\", \"insomniac\"),\n", " (\"male\", \"normal\"),\n", " (\"male\", \"insomniac\"),\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, to use these factors in a `y_range` (or `x_range`), we need to convert them to a **factor range** using `bokeh.models.FactorRange()`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "p = bokeh.plotting.figure(\n", " height=200,\n", " width=400,\n", " x_axis_label='percent correct',\n", " x_range=[0, 100],\n", " y_range=bokeh.models.FactorRange(*factors),\n", " tools='save'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we are ready to add the bars, taking care to specify the `'cats'` column for our y-values." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " \n", " var docs_json = {\"86b88e58-504e-42d9-bfa2-236c432ffecd\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1202\",\"type\":\"LinearAxis\"}],\"center\":[{\"id\":\"1206\",\"type\":\"Grid\"},{\"id\":\"1210\",\"type\":\"Grid\"}],\"left\":[{\"id\":\"1207\",\"type\":\"CategoricalAxis\"}],\"plot_height\":200,\"plot_width\":400,\"renderers\":[{\"id\":\"1218\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"1237\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"1212\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"1195\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"1198\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"1193\",\"type\":\"FactorRange\"},\"y_scale\":{\"id\":\"1200\",\"type\":\"CategoricalScale\"}},\"id\":\"1194\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"callback\":null,\"data\":{\"cats\":[[\"female\",\"insomniac\"],[\"female\",\"normal\"],[\"male\",\"insomniac\"],[\"male\",\"normal\"]],\"gender\":[\"female\",\"female\",\"male\",\"male\"],\"index\":[0,1,2,3],\"percent correct\":{\"__ndarray__\":\"ymsor6F8UkCjiy666IJUQKuqqqqqulRAAAAAAAAAVEA=\",\"dtype\":\"float64\",\"shape\":[4]},\"sleeper\":[\"insomniac\",\"normal\",\"insomniac\",\"normal\"]},\"selected\":{\"id\":\"1243\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1244\",\"type\":\"UnionRenderers\"}},\"id\":\"1214\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"dimension\":1,\"grid_line_color\":null,\"ticker\":{\"id\":\"1208\",\"type\":\"CategoricalTicker\"}},\"id\":\"1210\",\"type\":\"Grid\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1211\",\"type\":\"SaveTool\"}]},\"id\":\"1212\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1242\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"height\":{\"value\":0.6},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"right\":{\"field\":\"percent correct\"},\"y\":{\"field\":\"cats\"}},\"id\":\"1217\",\"type\":\"HBar\"},{\"attributes\":{\"callback\":null,\"factors\":[[\"female\",\"normal\"],[\"female\",\"insomniac\"],[\"male\",\"normal\"],[\"male\",\"insomniac\"]]},\"id\":\"1193\",\"type\":\"FactorRange\"},{\"attributes\":{},\"id\":\"1211\",\"type\":\"SaveTool\"},{\"attributes\":{\"text\":\"\"},\"id\":\"1237\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"1198\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1244\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"callback\":null,\"end\":100},\"id\":\"1195\",\"type\":\"Range1d\"},{\"attributes\":{\"axis_label\":\"percent correct\",\"formatter\":{\"id\":\"1240\",\"type\":\"BasicTickFormatter\"},\"ticker\":{\"id\":\"1203\",\"type\":\"BasicTicker\"}},\"id\":\"1202\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1200\",\"type\":\"CategoricalScale\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"height\":{\"value\":0.6},\"line_color\":{\"value\":\"#1f77b4\"},\"right\":{\"field\":\"percent correct\"},\"y\":{\"field\":\"cats\"}},\"id\":\"1216\",\"type\":\"HBar\"},{\"attributes\":{\"formatter\":{\"id\":\"1242\",\"type\":\"CategoricalTickFormatter\"},\"ticker\":{\"id\":\"1208\",\"type\":\"CategoricalTicker\"}},\"id\":\"1207\",\"type\":\"CategoricalAxis\"},{\"attributes\":{},\"id\":\"1243\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1203\",\"type\":\"BasicTicker\"},{\"attributes\":{\"data_source\":{\"id\":\"1214\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1216\",\"type\":\"HBar\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1217\",\"type\":\"HBar\"},\"selection_glyph\":null,\"view\":{\"id\":\"1219\",\"type\":\"CDSView\"}},\"id\":\"1218\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1240\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"ticker\":{\"id\":\"1203\",\"type\":\"BasicTicker\"}},\"id\":\"1206\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1208\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"source\":{\"id\":\"1214\",\"type\":\"ColumnDataSource\"}},\"id\":\"1219\",\"type\":\"CDSView\"}],\"root_ids\":[\"1194\"]},\"title\":\"Bokeh Application\",\"version\":\"1.3.4\"}};\n", " var render_items = [{\"docid\":\"86b88e58-504e-42d9-bfa2-236c432ffecd\",\"roots\":{\"1194\":\"71b72f34-90a8-4e16-9f28-5a90ff60b0b2\"}}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n", "\n", " }\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " var attempts = 0;\n", " var timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " clearInterval(timer);\n", " }\n", " attempts++;\n", " if (attempts > 100) {\n", " console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n", " clearInterval(timer);\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "1194" } }, "output_type": "display_data" } ], "source": [ "p.hbar(\n", " source=df_mean,\n", " y='cats',\n", " right='percent correct',\n", " height=0.6,\n", ")\n", "\n", "p.ygrid.grid_line_color = None\n", "\n", "bokeh.io.show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing environment" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPython 3.7.4\n", "IPython 7.1.1\n", "\n", "pandas 0.24.2\n", "bokeh 1.3.4\n", "jupyterlab 1.1.4\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -p pandas,bokeh,jupyterlab" ] } ], "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.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }