{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 4.5: Heat maps with Bokeh\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A [heat map](https://en.wikipedia.org/wiki/Heat_map) is a type of plot where magnitude is expressed in terms of color. You can see an example of a heatmap generated using Bokeh [here](https://docs.bokeh.org/en/latest/docs/gallery/unemployment.html).\n", "\n", "**a)** Write a function with call signature `heatmap(x, y, z)` (you can add any kwargs you like) that make a heat map from x, y, z data. For simplicity for this function, assume `x` and `y` are categorical variables.\n", "\n", "**b)** 96 well plates are often used in analyzing biochemical reactions. Some absorbance data from a 96 well plate experiment are in the file `~git/bootcamp/data/96_well.csv`. Use your `heatmap()` function to make a display of the data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution\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", " const 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", " const JS_MIME_TYPE = 'application/javascript';\n", " const HTML_MIME_TYPE = 'text/html';\n", " const EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n", " const CLASS_NAME = 'output_bokeh rendered_html';\n", "\n", " /**\n", " * Render data to the DOM node\n", " */\n", " function render(props, node) {\n", " const 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", " const cell = handle.cell;\n", "\n", " const id = cell.output_area._bokeh_element_id;\n", " const 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", " const cmd_clean = \"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_clean, {\n", " iopub: {\n", " output: function(msg) {\n", " const 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", " const cmd_destroy = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n", " cell.notebook.kernel.execute(cmd_destroy);\n", " }\n", " }\n", "\n", " /**\n", " * Handle when a new output is added\n", " */\n", " function handleAddOutput(event, handle) {\n", " const output_area = handle.output_area;\n", " const output = handle.output;\n", "\n", " // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n", " if ((output.output_type != \"display_data\") || (!Object.prototype.hasOwnProperty.call(output.data, EXEC_MIME_TYPE))) {\n", " return\n", " }\n", "\n", " const 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", " const bk_div = document.createElement(\"div\");\n", " bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n", " const script_attrs = bk_div.children[0].attributes;\n", " for (let i = 0; i < script_attrs.length; i++) {\n", " toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n", " toinsert[toinsert.length - 1].firstChild.textContent = bk_div.children[0].textContent\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", " const 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", " const 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", " const events = require('base/js/events');\n", " const 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", " const 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", " const el = document.getElementById(\"1002\");\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(url) {\n", " console.error(\"failed to load \" + url);\n", " }\n", "\n", " for (let i = 0; i < css_urls.length; i++) {\n", " const url = css_urls[i];\n", " const element = document.createElement(\"link\");\n", " element.onload = on_load;\n", " element.onerror = on_error.bind(null, url);\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 (let i = 0; i < js_urls.length; i++) {\n", " const url = js_urls[i];\n", " const element = document.createElement('script');\n", " element.onload = on_load;\n", " element.onerror = on_error.bind(null, url);\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", " };\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", " \n", " const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js\"];\n", " const css_urls = [];\n", " \n", "\n", " const inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " function(Bokeh) {\n", " \n", " \n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " \n", " if (root.Bokeh !== undefined || force === true) {\n", " \n", " for (let i = 0; i < inline_js.length; i++) {\n", " inline_js[i].call(root, root.Bokeh);\n", " }\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", " const cell = $(document.getElementById(\"1002\")).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 const 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 const 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 const el = document.getElementById(\"1002\");\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(url) {\n console.error(\"failed to load \" + url);\n }\n\n for (let i = 0; i < css_urls.length; i++) {\n const url = css_urls[i];\n const element = document.createElement(\"link\");\n element.onload = on_load;\n element.onerror = on_error.bind(null, url);\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 (let i = 0; i < js_urls.length; i++) {\n const url = js_urls[i];\n const element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error.bind(null, url);\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 };\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 \n const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js\"];\n const css_urls = [];\n \n\n const inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n function(Bokeh) {\n \n \n }\n ];\n\n function run_inline_js() {\n \n if (root.Bokeh !== undefined || force === true) {\n \n for (let i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }\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 const cell = $(document.getElementById(\"1002\")).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 numpy as np\n", "import pandas as pd\n", "\n", "import bokeh.io\n", "import bokeh.models\n", "import bokeh.palettes\n", "import bokeh.plotting\n", "\n", "bokeh.io.output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**a)** In our `heatmap()` function, we will allow for tooltips when hovering, inclusion of a colorbar, and specification of palette. Importantly, we make sure the x- and y- values are categorical. We then use `rect` glyphs to color the heat map." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def heatmap(\n", " x,\n", " y,\n", " z,\n", " x_range=None,\n", " y_range=None,\n", " palette=bokeh.palettes.Viridis256,\n", " z_range=(None, None),\n", " x_label=\"x\",\n", " y_label=\"y\",\n", " z_label=\"z\",\n", " flip_x_axis=False,\n", " flip_y_axis=False,\n", " colorbar=True,\n", " **kwargs\n", "):\n", " \"\"\"Create a heatmap for x, y, z data where the x and y data are categorial.\n", " \n", " Parameters\n", " ----------\n", " x : array_like\n", " x-values for heat map. Assumed to be categorical. Any entries are\n", " converted to strings.\n", " y : array_like\n", " y-values for heat map. Assumed to be categorical. Any entries are\n", " converted to strings.\n", " z : array_like\n", " z-values for heat map. These data are quantitative and displayed\n", " with color.\n", " x_range : array_like\n", " Array of unique values that determines the values of the x-axis.\n", " y_range : array_like\n", " Array of unique values that determines the values of the y-axis.\n", " palette : List of hex colors, default bokeh.palettes.Viridis256\n", " Color palette to use to make linear color mapper for heat map.\n", " z_range : 2-tuple, default (None, None)\n", " Range of allowed z-values. If an entry is None, the min or max\n", " is used.\n", " x_label : str, defualt \"z\"\n", " Label to be used in tool tips for the x-values.\n", " y_label : str, defualt \"z\"\n", " Label to be used in tool tips for the y-values.\n", " z_label : str, defualt \"z\"\n", " Label to be used in tool tips for the z-values.\n", " flip_x_axis : bool, default False\n", " If True, x-axis is reversed.\n", " flip_y_axis : bool, default False\n", " If True, y-axis is reversed.\n", " colorbar : bool, default True\n", " If True, display color bar.\n", " kwargs : dict\n", " All other kwargs are passed to bokeh.plotting.figure() when\n", " setting up the plot.\n", " \n", " Returns\n", " -------\n", " output : Bokeh plotting object\n", " Heatmap plot.\n", " \"\"\"\n", " # Convert x and y values to strings; assuming evenly spread\n", " x_str = [str(x_val) for x_val in x]\n", " y_str = [str(y_val) for y_val in y]\n", "\n", " # Ranges of z-values\n", " z_min = z.min() if z_range[0] is None else z_range[0]\n", " z_max = z.max() if z_range[1] is None else z_range[1]\n", "\n", " # Categorical axis values\n", " if x_range is None:\n", " x_range = [str(x_val) for x_val in sorted(np.unique(x))]\n", " if y_range is None:\n", " y_range = [str(y_val) for y_val in sorted(np.unique(y))]\n", " if flip_x_axis:\n", " x_range = x_range[::-1]\n", " if flip_y_axis:\n", " y_range = y_range[::-1]\n", "\n", " # Set up defaults\n", " x_axis_label = kwargs.pop(\"x_axis_label\", x_label)\n", " y_axis_label = kwargs.pop(\"y_axis_label\", y_label)\n", " tools = kwargs.pop(\"tools\", \"pan,box_zoom,wheel_zoom,reset,hover,save\")\n", " tooltips = kwargs.pop(\n", " \"tooltips\", [(x_label, \"@x\"), (y_label, \"@y\"), (z_label, \"@z\")]\n", " )\n", " toolbar_location = kwargs.pop(\"toolbar_location\", \"above\")\n", " frame_height = kwargs.pop(\"frame_height\", None)\n", " frame_width = kwargs.pop(\"frame_width\", None)\n", "\n", " # Adjust frame heights and widths to have square rectangles\n", " if frame_width is not None:\n", " if frame_height is None:\n", " frame_height = frame_width * len(y_range) // len(x_range)\n", " else:\n", " if frame_height is None:\n", " frame_height = 250\n", " frame_width = frame_height * len(x_range) // len(y_range)\n", "\n", " # Data source\n", " source = bokeh.models.ColumnDataSource(\n", " dict(x_str=x_str, y_str=y_str, x=x, y=y, z=z)\n", " )\n", "\n", " # Color mapper\n", " mapper = bokeh.models.LinearColorMapper(palette=palette, low=z_min, high=z_max)\n", "\n", " # Figure\n", " p = bokeh.plotting.figure(\n", " x_range=x_range,\n", " y_range=y_range,\n", " frame_width=frame_width,\n", " frame_height=frame_height,\n", " x_axis_label=x_axis_label,\n", " y_axis_label=y_axis_label,\n", " tools=tools,\n", " tooltips=tooltips,\n", " toolbar_location=toolbar_location,\n", " **kwargs\n", " )\n", "\n", " p.rect(\n", " x=\"x_str\",\n", " y=\"y_str\",\n", " width=frame_width / frame_height * len(y_range) / len(x_range),\n", " height=frame_height / frame_width * len(x_range) / len(y_range),\n", " source=source,\n", " fill_color={\"field\": \"z\", \"transform\": mapper},\n", " line_color=None,\n", " )\n", "\n", " # Add color bar\n", " color_bar = bokeh.models.ColorBar(\n", " color_mapper=mapper, major_label_text_font_size=\"8px\", border_line_color=None,\n", " )\n", " p.add_layout(color_bar, \"right\")\n", "\n", " return p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**b)** Let's put it to use!" ] }, { "cell_type": "code", "execution_count": 3, "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", " const docs_json = {\"315d5937-0dfe-44ec-8119-811aba42a2bb\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1014\"}],\"center\":[{\"id\":\"1016\"},{\"id\":\"1019\"}],\"frame_height\":250,\"frame_width\":375,\"left\":[{\"id\":\"1017\"}],\"renderers\":[{\"id\":\"1038\"}],\"right\":[{\"id\":\"1040\"}],\"title\":{\"id\":\"1041\"},\"toolbar\":{\"id\":\"1027\"},\"toolbar_location\":\"above\",\"x_range\":{\"id\":\"1006\"},\"x_scale\":{\"id\":\"1010\"},\"y_range\":{\"id\":\"1008\"},\"y_scale\":{\"id\":\"1012\"}},\"id\":\"1005\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"1044\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"overlay\":{\"id\":\"1026\"}},\"id\":\"1021\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"1045\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"1046\",\"type\":\"NoOverlap\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1048\"},\"group\":null,\"major_label_policy\":{\"id\":\"1049\"},\"ticker\":{\"id\":\"1015\"}},\"id\":\"1014\",\"type\":\"CategoricalAxis\"},{\"attributes\":{\"color_mapper\":{\"id\":\"1004\"},\"coordinates\":null,\"group\":null,\"major_label_policy\":{\"id\":\"1046\"},\"major_label_text_font_size\":\"8px\"},\"id\":\"1040\",\"type\":\"ColorBar\"},{\"attributes\":{},\"id\":\"1015\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"coordinates\":null,\"group\":null},\"id\":\"1041\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"1048\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"axis\":{\"id\":\"1017\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"1019\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1025\",\"type\":\"SaveTool\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1044\"},\"group\":null,\"major_label_policy\":{\"id\":\"1045\"},\"ticker\":{\"id\":\"1018\"}},\"id\":\"1017\",\"type\":\"CategoricalAxis\"},{\"attributes\":{},\"id\":\"1049\",\"type\":\"AllLabels\"},{\"attributes\":{\"tools\":[{\"id\":\"1020\"},{\"id\":\"1021\"},{\"id\":\"1022\"},{\"id\":\"1023\"},{\"id\":\"1024\"},{\"id\":\"1025\"}]},\"id\":\"1027\",\"type\":\"Toolbar\"},{\"attributes\":{\"source\":{\"id\":\"1003\"}},\"id\":\"1039\",\"type\":\"CDSView\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"1026\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"field\":\"z\",\"transform\":{\"id\":\"1004\"}},\"hatch_alpha\":{\"value\":0.1},\"height\":{\"value\":1.0},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":null},\"width\":{\"value\":1.0},\"x\":{\"field\":\"x_str\"},\"y\":{\"field\":\"y_str\"}},\"id\":\"1036\",\"type\":\"Rect\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"field\":\"z\",\"transform\":{\"id\":\"1004\"}},\"hatch_alpha\":{\"value\":0.2},\"height\":{\"value\":1.0},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":null},\"width\":{\"value\":1.0},\"x\":{\"field\":\"x_str\"},\"y\":{\"field\":\"y_str\"}},\"id\":\"1037\",\"type\":\"Rect\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1003\"},\"glyph\":{\"id\":\"1035\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1037\"},\"nonselection_glyph\":{\"id\":\"1036\"},\"view\":{\"id\":\"1039\"}},\"id\":\"1038\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1050\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"axis\":{\"id\":\"1014\"},\"coordinates\":null,\"group\":null,\"ticker\":null},\"id\":\"1016\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1051\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1022\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"factors\":[\"H\",\"G\",\"F\",\"E\",\"D\",\"C\",\"B\",\"A\"]},\"id\":\"1008\",\"type\":\"FactorRange\"},{\"attributes\":{},\"id\":\"1023\",\"type\":\"ResetTool\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"column\",\"@x\"],[\"row\",\"@y\"],[\"absorbance\",\"@z\"]]},\"id\":\"1024\",\"type\":\"HoverTool\"},{\"attributes\":{},\"id\":\"1020\",\"type\":\"PanTool\"},{\"attributes\":{\"data\":{\"x\":[1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12],\"x_str\":[\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"2\",\"3\",\"3\",\"3\",\"3\",\"3\",\"3\",\"3\",\"3\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"5\",\"5\",\"5\",\"5\",\"5\",\"5\",\"5\",\"5\",\"6\",\"6\",\"6\",\"6\",\"6\",\"6\",\"6\",\"6\",\"7\",\"7\",\"7\",\"7\",\"7\",\"7\",\"7\",\"7\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"9\",\"9\",\"9\",\"9\",\"9\",\"9\",\"9\",\"9\",\"10\",\"10\",\"10\",\"10\",\"10\",\"10\",\"10\",\"10\",\"11\",\"11\",\"11\",\"11\",\"11\",\"11\",\"11\",\"11\",\"12\",\"12\",\"12\",\"12\",\"12\",\"12\",\"12\",\"12\"],\"y\":[\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\"],\"y_str\":[\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\"],\"z\":{\"__ndarray__\":\"16NwPQrX+z8UrkfhehT+P3E9CtejcP0/mpmZmZmZ/T9xPQrXo3D9P6RwPQrXo/w/exSuR+F6/D8fhetRuB75P1yPwvUoXPs/j8L1KFyP/j+amZmZmZn9P4/C9Shcj/4/cT0K16Nw/T/D9Shcj8L9P1yPwvUoXPs/ZmZmZmZm+j/NzMzMzMz8P65H4XoUrv8/4XoUrkfh/j+amZmZmZn9P5qZmZmZmf0/uB6F61G4/j/NzMzMzMz8PxSuR+F6FPo/PQrXo3A9/j+amZmZmZn9Px+F61G4Hv0/rkfhehSu/z+4HoXrUbj+P1yPwvUoXP8/9ihcj8L1/D/hehSuR+H6P7gehetRuP4/hetRuB6F/z9SuB6F61H8P+F6FK5H4f4/FK5H4XoU/j/hehSuR+H+P3sUrkfhevw/7FG4HoXr/T+amZmZmZn9P9ejcD0K1/8/FK5H4XoU/j/sUbgehev9PzMzMzMzM/8/16NwPQrX/z9I4XoUrkf9P/YoXI/C9fw/H4XrUbge/T+F61G4HoX/PwAAAAAAAABA4XoUrkfh/j+F61G4HoX/P4/C9Shcj/4/pHA9Ctej/D8zMzMzMzP7PzMzMzMzM/8/4XoUrkfh/j+uR+F6FK7/P6RwPQrXowBAhetRuB6F/z8K16NwPQr/Px+F61G4Hv0/j8L1KFyP/j/D9Shcj8L9P5qZmZmZmf0/ZmZmZmZm/j+PwvUoXI/+P4XrUbgehf8/4XoUrkfh/j8pXI/C9Sj8P0jhehSuR/0/PQrXo3A9AECF61G4HoX/PwrXo3A9Cv8/9ihcj8L1/D8pXI/C9SgAQMP1KFyPwv0/rkfhehSu+z8UrkfhehT6P0jhehSuR/0/zczMzMzM/D/NzMzMzMz8P+F6FK5H4f4/H4XrUbge/T8zMzMzMzP7PxSuR+F6FPo/pHA9Ctej/D/2KFyPwvX8P2ZmZmZmZvo/w/UoXI/C+T/hehSuR+H6Pylcj8L1KOw/XI/C9Shc7z+4HoXrUbjuPwAAAAAAAPA/\",\"dtype\":\"float64\",\"order\":\"little\",\"shape\":[96]}},\"selected\":{\"id\":\"1051\"},\"selection_policy\":{\"id\":\"1050\"}},\"id\":\"1003\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1012\",\"type\":\"CategoricalScale\"},{\"attributes\":{\"high\":2.08,\"low\":0.88,\"palette\":[\"#440154\",\"#440255\",\"#440357\",\"#450558\",\"#45065A\",\"#45085B\",\"#46095C\",\"#460B5E\",\"#460C5F\",\"#460E61\",\"#470F62\",\"#471163\",\"#471265\",\"#471466\",\"#471567\",\"#471669\",\"#47186A\",\"#48196B\",\"#481A6C\",\"#481C6E\",\"#481D6F\",\"#481E70\",\"#482071\",\"#482172\",\"#482273\",\"#482374\",\"#472575\",\"#472676\",\"#472777\",\"#472878\",\"#472A79\",\"#472B7A\",\"#472C7B\",\"#462D7C\",\"#462F7C\",\"#46307D\",\"#46317E\",\"#45327F\",\"#45347F\",\"#453580\",\"#453681\",\"#443781\",\"#443982\",\"#433A83\",\"#433B83\",\"#433C84\",\"#423D84\",\"#423E85\",\"#424085\",\"#414186\",\"#414286\",\"#404387\",\"#404487\",\"#3F4587\",\"#3F4788\",\"#3E4888\",\"#3E4989\",\"#3D4A89\",\"#3D4B89\",\"#3D4C89\",\"#3C4D8A\",\"#3C4E8A\",\"#3B508A\",\"#3B518A\",\"#3A528B\",\"#3A538B\",\"#39548B\",\"#39558B\",\"#38568B\",\"#38578C\",\"#37588C\",\"#37598C\",\"#365A8C\",\"#365B8C\",\"#355C8C\",\"#355D8C\",\"#345E8D\",\"#345F8D\",\"#33608D\",\"#33618D\",\"#32628D\",\"#32638D\",\"#31648D\",\"#31658D\",\"#31668D\",\"#30678D\",\"#30688D\",\"#2F698D\",\"#2F6A8D\",\"#2E6B8E\",\"#2E6C8E\",\"#2E6D8E\",\"#2D6E8E\",\"#2D6F8E\",\"#2C708E\",\"#2C718E\",\"#2C728E\",\"#2B738E\",\"#2B748E\",\"#2A758E\",\"#2A768E\",\"#2A778E\",\"#29788E\",\"#29798E\",\"#287A8E\",\"#287A8E\",\"#287B8E\",\"#277C8E\",\"#277D8E\",\"#277E8E\",\"#267F8E\",\"#26808E\",\"#26818E\",\"#25828E\",\"#25838D\",\"#24848D\",\"#24858D\",\"#24868D\",\"#23878D\",\"#23888D\",\"#23898D\",\"#22898D\",\"#228A8D\",\"#228B8D\",\"#218C8D\",\"#218D8C\",\"#218E8C\",\"#208F8C\",\"#20908C\",\"#20918C\",\"#1F928C\",\"#1F938B\",\"#1F948B\",\"#1F958B\",\"#1F968B\",\"#1E978A\",\"#1E988A\",\"#1E998A\",\"#1E998A\",\"#1E9A89\",\"#1E9B89\",\"#1E9C89\",\"#1E9D88\",\"#1E9E88\",\"#1E9F88\",\"#1EA087\",\"#1FA187\",\"#1FA286\",\"#1FA386\",\"#20A485\",\"#20A585\",\"#21A685\",\"#21A784\",\"#22A784\",\"#23A883\",\"#23A982\",\"#24AA82\",\"#25AB81\",\"#26AC81\",\"#27AD80\",\"#28AE7F\",\"#29AF7F\",\"#2AB07E\",\"#2BB17D\",\"#2CB17D\",\"#2EB27C\",\"#2FB37B\",\"#30B47A\",\"#32B57A\",\"#33B679\",\"#35B778\",\"#36B877\",\"#38B976\",\"#39B976\",\"#3BBA75\",\"#3DBB74\",\"#3EBC73\",\"#40BD72\",\"#42BE71\",\"#44BE70\",\"#45BF6F\",\"#47C06E\",\"#49C16D\",\"#4BC26C\",\"#4DC26B\",\"#4FC369\",\"#51C468\",\"#53C567\",\"#55C666\",\"#57C665\",\"#59C764\",\"#5BC862\",\"#5EC961\",\"#60C960\",\"#62CA5F\",\"#64CB5D\",\"#67CC5C\",\"#69CC5B\",\"#6BCD59\",\"#6DCE58\",\"#70CE56\",\"#72CF55\",\"#74D054\",\"#77D052\",\"#79D151\",\"#7CD24F\",\"#7ED24E\",\"#81D34C\",\"#83D34B\",\"#86D449\",\"#88D547\",\"#8BD546\",\"#8DD644\",\"#90D643\",\"#92D741\",\"#95D73F\",\"#97D83E\",\"#9AD83C\",\"#9DD93A\",\"#9FD938\",\"#A2DA37\",\"#A5DA35\",\"#A7DB33\",\"#AADB32\",\"#ADDC30\",\"#AFDC2E\",\"#B2DD2C\",\"#B5DD2B\",\"#B7DD29\",\"#BADE27\",\"#BDDE26\",\"#BFDF24\",\"#C2DF22\",\"#C5DF21\",\"#C7E01F\",\"#CAE01E\",\"#CDE01D\",\"#CFE11C\",\"#D2E11B\",\"#D4E11A\",\"#D7E219\",\"#DAE218\",\"#DCE218\",\"#DFE318\",\"#E1E318\",\"#E4E318\",\"#E7E419\",\"#E9E419\",\"#ECE41A\",\"#EEE51B\",\"#F1E51C\",\"#F3E51E\",\"#F6E61F\",\"#F8E621\",\"#FAE622\",\"#FDE724\"]},\"id\":\"1004\",\"type\":\"LinearColorMapper\"},{\"attributes\":{\"factors\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\"]},\"id\":\"1006\",\"type\":\"FactorRange\"},{\"attributes\":{},\"id\":\"1010\",\"type\":\"CategoricalScale\"},{\"attributes\":{},\"id\":\"1018\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"fill_color\":{\"field\":\"z\",\"transform\":{\"id\":\"1004\"}},\"height\":{\"value\":1.0},\"line_color\":{\"value\":null},\"width\":{\"value\":1.0},\"x\":{\"field\":\"x_str\"},\"y\":{\"field\":\"y_str\"}},\"id\":\"1035\",\"type\":\"Rect\"}],\"root_ids\":[\"1005\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n", " const render_items = [{\"docid\":\"315d5937-0dfe-44ec-8119-811aba42a2bb\",\"root_ids\":[\"1005\"],\"roots\":{\"1005\":\"98f20520-443c-43e0-828f-79785bd9d8da\"}}];\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", " let attempts = 0;\n", " const timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " clearInterval(timer);\n", " embed_document(root);\n", " } else {\n", " attempts++;\n", " if (attempts > 100) {\n", " clearInterval(timer);\n", " console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n", " }\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "1005" } }, "output_type": "display_data" } ], "source": [ "df = pd.read_csv('data/96_well.csv')\n", "\n", "p = heatmap(\n", " df[\"column\"],\n", " df[\"row\"],\n", " df[\"absorbance\"],\n", " x_range=[str(x) for x in range(1, 13)],\n", " y_range=[a for a in reversed(\"ABCDEFGH\")],\n", " x_label='column',\n", " y_label='row',\n", " z_label='absorbance',\n", " x_axis_label=None,\n", " y_axis_label=None,\n", ")\n", "\n", "bokeh.io.show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Very nice!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing environment" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [ "hide-input" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python implementation: CPython\n", "Python version : 3.9.12\n", "IPython version : 8.3.0\n", "\n", "numpy : 1.21.5\n", "pandas : 1.4.2\n", "bokeh : 2.4.2\n", "jupyterlab: 3.3.2\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -p numpy,pandas,bokeh,jupyterlab" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }