{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Pandas and numpy for data manipulation\n", "import pandas as pd\n", "import numpy as np\n", "\n", "from scipy.stats import gaussian_kde" ] }, { "cell_type": "code", "execution_count": 2, "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 !== undefined) {\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 element_id = msg.content.text.trim();\n", " Bokeh.index[element_id].model.document.clear();\n", " delete Bokeh.index[element_id];\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[0].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[0].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[0]);\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(\"de0b2ab2-0525-46f0-acae-c2fc35c4d1a8\");\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) { callback() });\n", " }\n", " finally {\n", " delete root._bokeh_onload_callbacks\n", " }\n", " console.info(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(js_urls, callback) {\n", " root._bokeh_onload_callbacks.push(callback);\n", " if (root._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", " root._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", " root._bokeh_is_loading--;\n", " if (root._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(\"de0b2ab2-0525-46f0-acae-c2fc35c4d1a8\");\n", " if (element == null) {\n", " console.log(\"Bokeh: ERROR: autoload.js configured with elementid 'de0b2ab2-0525-46f0-acae-c2fc35c4d1a8' but no matching script tag was found. \")\n", " return false;\n", " }\n", "\n", " var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.14.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.14.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.14.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.12.14.min.js\"];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " \n", " },\n", " function(Bokeh) {\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.14.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.14.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.14.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.14.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.14.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.14.min.css\");\n", " }\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(\"de0b2ab2-0525-46f0-acae-c2fc35c4d1a8\")).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.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", "}(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(\"de0b2ab2-0525-46f0-acae-c2fc35c4d1a8\");\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) { callback() });\n }\n finally {\n delete root._bokeh_onload_callbacks\n }\n console.info(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(js_urls, callback) {\n root._bokeh_onload_callbacks.push(callback);\n if (root._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 root._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 root._bokeh_is_loading--;\n if (root._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(\"de0b2ab2-0525-46f0-acae-c2fc35c4d1a8\");\n if (element == null) {\n console.log(\"Bokeh: ERROR: autoload.js configured with elementid 'de0b2ab2-0525-46f0-acae-c2fc35c4d1a8' but no matching script tag was found. \")\n return false;\n }\n\n var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.14.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.14.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.14.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.12.14.min.js\"];\n\n var inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n \n function(Bokeh) {\n \n },\n function(Bokeh) {\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.14.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.14.min.css\");\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.14.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.14.min.css\");\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.14.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.14.min.css\");\n }\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(\"de0b2ab2-0525-46f0-acae-c2fc35c4d1a8\")).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.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}(window));" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from bokeh.io import show, output_notebook, push_notebook\n", "from bokeh.plotting import figure\n", "\n", "from bokeh.models import CategoricalColorMapper, HoverTool, ColumnDataSource, Panel\n", "from bokeh.models.widgets import CheckboxGroup, Slider, RangeSlider, Tabs, CheckboxButtonGroup\n", "\n", "from bokeh.layouts import column, row, WidgetBox\n", "from bokeh.palettes import Category20_16\n", "\n", "from bokeh.application.handlers import FunctionHandler\n", "from bokeh.application import Application\n", "\n", "output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Import and Inspection" ] }, { "cell_type": "code", "execution_count": 3, "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", " \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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0yearmonthdaydep_timesched_dep_timedep_delayarr_timesched_arr_timearr_delay...flighttailnumorigindestair_timedistancehourminutetime_hourname
01201311517.05152.0830.081911.0...1545N14228EWRIAH227.014005152013-01-01 05:00:00United Air Lines Inc.
12201311533.05294.0850.083020.0...1714N24211LGAIAH227.014165292013-01-01 05:00:00United Air Lines Inc.
23201311542.05402.0923.085033.0...1141N619AAJFKMIA160.010895402013-01-01 05:00:00American Airlines Inc.
34201311544.0545-1.01004.01022-18.0...725N804JBJFKBQN183.015765452013-01-01 05:00:00JetBlue Airways
45201311554.0600-6.0812.0837-25.0...461N668DNLGAATL116.0762602013-01-01 06:00:00Delta Air Lines Inc.
56201311554.0558-4.0740.072812.0...1696N39463EWRORD150.07195582013-01-01 05:00:00United Air Lines Inc.
67201311555.0600-5.0913.085419.0...507N516JBEWRFLL158.01065602013-01-01 06:00:00JetBlue Airways
78201311557.0600-3.0709.0723-14.0...5708N829ASLGAIAD53.0229602013-01-01 06:00:00ExpressJet Airlines Inc.
89201311557.0600-3.0838.0846-8.0...79N593JBJFKMCO140.0944602013-01-01 06:00:00JetBlue Airways
910201311558.0600-2.0753.07458.0...301N3ALAALGAORD138.0733602013-01-01 06:00:00American Airlines Inc.
\n", "

10 rows × 21 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 year month day dep_time sched_dep_time dep_delay \\\n", "0 1 2013 1 1 517.0 515 2.0 \n", "1 2 2013 1 1 533.0 529 4.0 \n", "2 3 2013 1 1 542.0 540 2.0 \n", "3 4 2013 1 1 544.0 545 -1.0 \n", "4 5 2013 1 1 554.0 600 -6.0 \n", "5 6 2013 1 1 554.0 558 -4.0 \n", "6 7 2013 1 1 555.0 600 -5.0 \n", "7 8 2013 1 1 557.0 600 -3.0 \n", "8 9 2013 1 1 557.0 600 -3.0 \n", "9 10 2013 1 1 558.0 600 -2.0 \n", "\n", " arr_time sched_arr_time arr_delay ... flight \\\n", "0 830.0 819 11.0 ... 1545 \n", "1 850.0 830 20.0 ... 1714 \n", "2 923.0 850 33.0 ... 1141 \n", "3 1004.0 1022 -18.0 ... 725 \n", "4 812.0 837 -25.0 ... 461 \n", "5 740.0 728 12.0 ... 1696 \n", "6 913.0 854 19.0 ... 507 \n", "7 709.0 723 -14.0 ... 5708 \n", "8 838.0 846 -8.0 ... 79 \n", "9 753.0 745 8.0 ... 301 \n", "\n", " tailnum origin dest air_time distance hour minute time_hour \\\n", "0 N14228 EWR IAH 227.0 1400 5 15 2013-01-01 05:00:00 \n", "1 N24211 LGA IAH 227.0 1416 5 29 2013-01-01 05:00:00 \n", "2 N619AA JFK MIA 160.0 1089 5 40 2013-01-01 05:00:00 \n", "3 N804JB JFK BQN 183.0 1576 5 45 2013-01-01 05:00:00 \n", "4 N668DN LGA ATL 116.0 762 6 0 2013-01-01 06:00:00 \n", "5 N39463 EWR ORD 150.0 719 5 58 2013-01-01 05:00:00 \n", "6 N516JB EWR FLL 158.0 1065 6 0 2013-01-01 06:00:00 \n", "7 N829AS LGA IAD 53.0 229 6 0 2013-01-01 06:00:00 \n", "8 N593JB JFK MCO 140.0 944 6 0 2013-01-01 06:00:00 \n", "9 N3ALAA LGA ORD 138.0 733 6 0 2013-01-01 06:00:00 \n", "\n", " name \n", "0 United Air Lines Inc. \n", "1 United Air Lines Inc. \n", "2 American Airlines Inc. \n", "3 JetBlue Airways \n", "4 Delta Air Lines Inc. \n", "5 United Air Lines Inc. \n", "6 JetBlue Airways \n", "7 ExpressJet Airlines Inc. \n", "8 JetBlue Airways \n", "9 American Airlines Inc. \n", "\n", "[10 rows x 21 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ " # Read in data\n", "flights = pd.read_csv('data/flights.csv')\n", "carriers = pd.read_csv('data/by_carrier.csv')\n", "airlines = pd.read_csv('data/airlines.csv')\n", "\n", "flights = flights.merge(airlines, how = 'left', on = 'carrier')\n", "\n", "available_carriers = list(flights['name'].unique())\n", "airline_colors = list(carriers['color'].unique())\n", "\n", "available_carriers = list(flights['name'].unique())\n", "# Filter flight delays between -60 and 120 minutes\n", "flights = flights[flights['arr_delay'].between(-60, 120)]\n", "flights.head(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Original Histogram Application" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def modify_doc(doc):\n", " \n", " def make_dataset(carrier_list, range_start = -60, range_end = 120, bin_width = 5):\n", "\n", " by_carrier = pd.DataFrame(columns=['proportion', 'left', 'right', \n", " 'f_proportion', 'f_interval',\n", " 'name', 'color'])\n", " range_extent = range_end - range_start\n", "\n", " # Iterate through all the carriers\n", " for i, carrier_name in enumerate(carrier_list):\n", "\n", " # Subset to the carrier\n", " subset = flights[flights['name'] == carrier_name]\n", "\n", " # Create a histogram with 5 minute bins\n", " arr_hist, edges = np.histogram(subset['arr_delay'], \n", " bins = int(range_extent / bin_width), \n", " range = [range_start, range_end])\n", "\n", " # Divide the counts by the total to get a proportion\n", " arr_df = pd.DataFrame({'proportion': arr_hist / np.sum(arr_hist), 'left': edges[:-1], 'right': edges[1:] })\n", "\n", " # Format the proportion \n", " arr_df['f_proportion'] = ['%0.5f' % proportion for proportion in arr_df['proportion']]\n", "\n", " # Format the interval\n", " arr_df['f_interval'] = ['%d to %d minutes' % (left, right) for left, right in zip(arr_df['left'], arr_df['right'])]\n", "\n", " # Assign the carrier for labels\n", " arr_df['name'] = carrier_name\n", "\n", " # Color each carrier differently\n", " arr_df['color'] = Category20_16[i]\n", "\n", " # Add to the overall dataframe\n", " by_carrier = by_carrier.append(arr_df)\n", "\n", " # Overall dataframe\n", " by_carrier = by_carrier.sort_values(['name', 'left'])\n", "\n", " return ColumnDataSource(by_carrier)\n", " \n", " def style(p):\n", " # Title \n", " p.title.align = 'center'\n", " p.title.text_font_size = '20pt'\n", " p.title.text_font = 'serif'\n", "\n", " # Axis titles\n", " p.xaxis.axis_label_text_font_size = '14pt'\n", " p.xaxis.axis_label_text_font_style = 'bold'\n", " p.yaxis.axis_label_text_font_size = '14pt'\n", " p.yaxis.axis_label_text_font_style = 'bold'\n", "\n", " # Tick labels\n", " p.xaxis.major_label_text_font_size = '12pt'\n", " p.yaxis.major_label_text_font_size = '12pt'\n", "\n", " return p\n", " \n", " def make_plot(src):\n", " # Blank plot with correct labels\n", " p = figure(plot_width = 700, plot_height = 700, \n", " title = 'Histogram of Arrival Delays by Carrier',\n", " x_axis_label = 'Delay (min)', y_axis_label = 'Proportion')\n", "\n", " # Quad glyphs to create a histogram\n", " p.quad(source = src, bottom = 0, top = 'proportion', left = 'left', right = 'right',\n", " color = 'color', fill_alpha = 0.7, hover_fill_color = 'color', legend = 'name',\n", " hover_fill_alpha = 1.0, line_color = 'black')\n", "\n", " # Hover tool with vline mode\n", " hover = HoverTool(tooltips=[('Carrier', '@name'), \n", " ('Delay', '@f_interval'),\n", " ('Proportion', '@f_proportion')],\n", " mode='vline')\n", "\n", " p.add_tools(hover)\n", "\n", " # Styling\n", " p = style(p)\n", "\n", " return p\n", " \n", " def update(attr, old, new):\n", " carriers_to_plot = [carrier_selection.labels[i] for i in carrier_selection.active]\n", " \n", " new_src = make_dataset(carriers_to_plot,\n", " range_start = range_select.value[0],\n", " range_end = range_select.value[1],\n", " bin_width = binwidth_select.value)\n", "\n", " src.data.update(new_src.data)\n", "\n", " \n", " carrier_selection = CheckboxGroup(labels=available_carriers, active = [0, 1])\n", " carrier_selection.on_change('active', update)\n", " \n", " binwidth_select = Slider(start = 1, end = 30, \n", " step = 1, value = 5,\n", " title = 'Delay Width (min)')\n", " binwidth_select.on_change('value', update)\n", " \n", " range_select = RangeSlider(start = -60, end = 180, value = (-60, 120),\n", " step = 5, title = 'Delay Range (min)')\n", " range_select.on_change('value', update)\n", " \n", " \n", " \n", " initial_carriers = [carrier_selection.labels[i] for i in carrier_selection.active]\n", " \n", " src = make_dataset(initial_carriers,\n", " range_start = range_select.value[0],\n", " range_end = range_select.value[1],\n", " bin_width = binwidth_select.value)\n", " \n", " p = make_plot(src)\n", " \n", " # Put controls in a single element\n", " controls = WidgetBox(carrier_selection, binwidth_select, range_select)\n", " \n", " # Create a row layout\n", " layout = row(controls, p)\n", " \n", " # Make a tab with the layout \n", " tab = Panel(child=layout, title = 'Delay Histogram')\n", " tabs = Tabs(tabs=[tab])\n", " \n", " doc.add_root(tabs)\n", " \n", "# Set up an application\n", "handler = FunctionHandler(modify_doc)\n", "app = Application(handler)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.bokehjs_exec.v0+json": "", "text/html": [ "\n", "" ] }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "server_id": "5fc0fa5d71d849959f9de433f88a08f0" } }, "output_type": "display_data" } ], "source": [ "show(app)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function to Make Data for Density Plot" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def make_kde_dataset(carrier_list, range_start, range_end, bandwidth=None):\n", " xs = []\n", " ys = []\n", " colors = []\n", " labels = []\n", " \n", " for i, carrier in enumerate(carrier_list):\n", " subset = flights[flights['name'] == carrier]\n", " subset = subset[subset['arr_delay'].between(range_start, range_end)]\n", " \n", " kde = gaussian_kde(subset['arr_delay'], bw_method=bandwidth)\n", " # Evenly space x values\n", " x = np.linspace(range_start, range_end, 100)\n", " # Evaluate pdf at every value of x\n", " y = kde.pdf(x)\n", " \n", " # Append the values to plot\n", " xs.append(list(x))\n", " ys.append(list(y))\n", " \n", " # Append the colors and label\n", " colors.append(airline_colors[i])\n", " labels.append(carrier)\n", " \n", " kernel_source = ColumnDataSource(data={'x': xs, 'y': ys, 'color': colors, 'label': labels})\n", " return kernel_source" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example of Density Plot in Bokeh" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " \n", " var docs_json = {\"682909b6-da61-49d1-b451-ebff55f2f1a6\":{\"roots\":{\"references\":[{\"attributes\":{\"items\":[{\"id\":\"39b3758e-8139-4418-8bf9-e4f8d9c81c02\",\"type\":\"LegendItem\"}],\"plot\":{\"id\":\"501d32c4-6dc9-49d7-8e51-30f100a8bb8b\",\"subtype\":\"Figure\",\"type\":\"Plot\"}},\"id\":\"4ab84ef2-3c93-4850-9f46-21aa47e57e80\",\"type\":\"Legend\"},{\"attributes\":{},\"id\":\"40602d8c-c820-4bbd-9d98-d35238db57ab\",\"type\":\"BasicTicker\"},{\"attributes\":{\"data_source\":{\"id\":\"aa00a7aa-989b-43fd-b037-c3896dbff7b7\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"6342ea48-e111-48c8-9ace-2f276026429c\",\"type\":\"MultiLine\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"e3fdc96a-52ff-4f2b-a241-3850de2835e3\",\"type\":\"MultiLine\"},\"selection_glyph\":null,\"view\":{\"id\":\"35ffa649-0db7-42cd-9d58-2a1413199f53\",\"type\":\"CDSView\"}},\"id\":\"8261d0a0-9163-4b77-99c8-d25574eda559\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"line_width\":{\"value\":3},\"xs\":{\"field\":\"x\"},\"ys\":{\"field\":\"y\"}},\"id\":\"e3fdc96a-52ff-4f2b-a241-3850de2835e3\",\"type\":\"MultiLine\"},{\"attributes\":{\"plot\":null,\"text\":\"Density Plots of Airline Delays\"},\"id\":\"3871a116-db17-4344-8b77-5d1641a61f77\",\"type\":\"Title\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"501d32c4-6dc9-49d7-8e51-30f100a8bb8b\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"40602d8c-c820-4bbd-9d98-d35238db57ab\",\"type\":\"BasicTicker\"}},\"id\":\"51718264-152c-4988-a154-b6a6228a2615\",\"type\":\"Grid\"},{\"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},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"3348c287-2032-4391-a0dc-dfd8e9a42b24\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"9f8d5826-1fcb-4a54-b6e8-a9c93cfcb212\",\"type\":\"PanTool\"},{\"id\":\"dab7f579-096c-4076-b1bd-9db957856901\",\"type\":\"WheelZoomTool\"},{\"id\":\"c9f363ce-f8ef-4a3d-9590-bd10eae9c38c\",\"type\":\"BoxZoomTool\"},{\"id\":\"a7ab3238-4cd3-40b0-9708-8a7de7dbd49a\",\"type\":\"SaveTool\"},{\"id\":\"8e3677f7-d256-4bba-8bfc-35071a96e3c7\",\"type\":\"ResetTool\"},{\"id\":\"ba120379-8f13-4a15-a616-4c0a75233edb\",\"type\":\"HelpTool\"}]},\"id\":\"347287d5-12c8-4da0-ae82-158ee7b065e5\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"9f8d5826-1fcb-4a54-b6e8-a9c93cfcb212\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"5adc3050-456e-4dd8-9d03-560082522fe7\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"callback\":null},\"id\":\"47cae002-17e7-4962-8f70-5f4362b92660\",\"type\":\"DataRange1d\"},{\"attributes\":{\"overlay\":{\"id\":\"3348c287-2032-4391-a0dc-dfd8e9a42b24\",\"type\":\"BoxAnnotation\"}},\"id\":\"c9f363ce-f8ef-4a3d-9590-bd10eae9c38c\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"a7ab3238-4cd3-40b0-9708-8a7de7dbd49a\",\"type\":\"SaveTool\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\",\"color\",\"label\"],\"data\":{\"color\":[\"#c5b0d5\",\"#8c564b\",\"#aec7e8\",\"#ffbb78\",\"#c49c94\"],\"label\":[\"United Air Lines Inc.\",\"American Airlines Inc.\",\"JetBlue Airways\",\"Delta Air Lines Inc.\",\"ExpressJet Airlines Inc.\"],\"x\":[[-60.0,-58.18181818181818,-56.36363636363637,-54.54545454545455,-52.72727272727273,-50.90909090909091,-49.09090909090909,-47.27272727272727,-45.45454545454545,-43.63636363636364,-41.81818181818182,-40.0,-38.18181818181819,-36.36363636363636,-34.54545454545455,-32.72727272727273,-30.90909090909091,-29.090909090909093,-27.272727272727273,-25.454545454545453,-23.63636363636364,-21.81818181818182,-20.0,-18.18181818181818,-16.363636363636367,-14.545454545454547,-12.727272727272727,-10.909090909090914,-9.090909090909093,-7.272727272727273,-5.454545454545453,-3.6363636363636402,-1.8181818181818201,0.0,1.818181818181813,3.636363636363633,5.454545454545453,7.272727272727266,9.090909090909093,10.909090909090907,12.72727272727272,14.545454545454547,16.36363636363636,18.181818181818173,20.0,21.818181818181813,23.63636363636364,25.454545454545453,27.272727272727266,29.090909090909093,30.909090909090907,32.72727272727272,34.54545454545455,36.36363636363636,38.18181818181817,40.0,41.81818181818181,43.636363636363626,45.45454545454545,47.272727272727266,49.09090909090909,50.90909090909091,52.72727272727272,54.54545454545455,56.36363636363636,58.18181818181817,60.0,61.81818181818181,63.636363636363626,65.45454545454545,67.27272727272727,69.0909090909091,70.9090909090909,72.72727272727272,74.54545454545453,76.36363636363635,78.18181818181819,80.0,81.81818181818181,83.63636363636363,85.45454545454544,87.27272727272728,89.0909090909091,90.9090909090909,92.72727272727272,94.54545454545453,96.36363636363635,98.18181818181819,100.0,101.81818181818181,103.63636363636363,105.45454545454544,107.27272727272728,109.0909090909091,110.9090909090909,112.72727272727272,114.54545454545453,116.36363636363635,118.18181818181819,120.0],[-60.0,-58.18181818181818,-56.36363636363637,-54.54545454545455,-52.72727272727273,-50.90909090909091,-49.09090909090909,-47.27272727272727,-45.45454545454545,-43.63636363636364,-41.81818181818182,-40.0,-38.18181818181819,-36.36363636363636,-34.54545454545455,-32.72727272727273,-30.90909090909091,-29.090909090909093,-27.272727272727273,-25.454545454545453,-23.63636363636364,-21.81818181818182,-20.0,-18.18181818181818,-16.363636363636367,-14.545454545454547,-12.727272727272727,-10.909090909090914,-9.090909090909093,-7.272727272727273,-5.454545454545453,-3.6363636363636402,-1.8181818181818201,0.0,1.818181818181813,3.636363636363633,5.454545454545453,7.272727272727266,9.090909090909093,10.909090909090907,12.72727272727272,14.545454545454547,16.36363636363636,18.181818181818173,20.0,21.818181818181813,23.63636363636364,25.454545454545453,27.272727272727266,29.090909090909093,30.909090909090907,32.72727272727272,34.54545454545455,36.36363636363636,38.18181818181817,40.0,41.81818181818181,43.636363636363626,45.45454545454545,47.272727272727266,49.09090909090909,50.90909090909091,52.72727272727272,54.54545454545455,56.36363636363636,58.18181818181817,60.0,61.81818181818181,63.636363636363626,65.45454545454545,67.27272727272727,69.0909090909091,70.9090909090909,72.72727272727272,74.54545454545453,76.36363636363635,78.18181818181819,80.0,81.81818181818181,83.63636363636363,85.45454545454544,87.27272727272728,89.0909090909091,90.9090909090909,92.72727272727272,94.54545454545453,96.36363636363635,98.18181818181819,100.0,101.81818181818181,103.63636363636363,105.45454545454544,107.27272727272728,109.0909090909091,110.9090909090909,112.72727272727272,114.54545454545453,116.36363636363635,118.18181818181819,120.0],[-60.0,-58.18181818181818,-56.36363636363637,-54.54545454545455,-52.72727272727273,-50.90909090909091,-49.09090909090909,-47.27272727272727,-45.45454545454545,-43.63636363636364,-41.81818181818182,-40.0,-38.18181818181819,-36.36363636363636,-34.54545454545455,-32.72727272727273,-30.90909090909091,-29.090909090909093,-27.272727272727273,-25.454545454545453,-23.63636363636364,-21.81818181818182,-20.0,-18.18181818181818,-16.363636363636367,-14.545454545454547,-12.727272727272727,-10.909090909090914,-9.090909090909093,-7.272727272727273,-5.454545454545453,-3.6363636363636402,-1.8181818181818201,0.0,1.818181818181813,3.636363636363633,5.454545454545453,7.272727272727266,9.090909090909093,10.909090909090907,12.72727272727272,14.545454545454547,16.36363636363636,18.181818181818173,20.0,21.818181818181813,23.63636363636364,25.454545454545453,27.272727272727266,29.090909090909093,30.909090909090907,32.72727272727272,34.54545454545455,36.36363636363636,38.18181818181817,40.0,41.81818181818181,43.636363636363626,45.45454545454545,47.272727272727266,49.09090909090909,50.90909090909091,52.72727272727272,54.54545454545455,56.36363636363636,58.18181818181817,60.0,61.81818181818181,63.636363636363626,65.45454545454545,67.27272727272727,69.0909090909091,70.9090909090909,72.72727272727272,74.54545454545453,76.36363636363635,78.18181818181819,80.0,81.81818181818181,83.63636363636363,85.45454545454544,87.27272727272728,89.0909090909091,90.9090909090909,92.72727272727272,94.54545454545453,96.36363636363635,98.18181818181819,100.0,101.81818181818181,103.63636363636363,105.45454545454544,107.27272727272728,109.0909090909091,110.9090909090909,112.72727272727272,114.54545454545453,116.36363636363635,118.18181818181819,120.0],[-60.0,-58.18181818181818,-56.36363636363637,-54.54545454545455,-52.72727272727273,-50.90909090909091,-49.09090909090909,-47.27272727272727,-45.45454545454545,-43.63636363636364,-41.81818181818182,-40.0,-38.18181818181819,-36.36363636363636,-34.54545454545455,-32.72727272727273,-30.90909090909091,-29.090909090909093,-27.272727272727273,-25.454545454545453,-23.63636363636364,-21.81818181818182,-20.0,-18.18181818181818,-16.363636363636367,-14.545454545454547,-12.727272727272727,-10.909090909090914,-9.090909090909093,-7.272727272727273,-5.454545454545453,-3.6363636363636402,-1.8181818181818201,0.0,1.818181818181813,3.636363636363633,5.454545454545453,7.272727272727266,9.090909090909093,10.909090909090907,12.72727272727272,14.545454545454547,16.36363636363636,18.181818181818173,20.0,21.818181818181813,23.63636363636364,25.454545454545453,27.272727272727266,29.090909090909093,30.909090909090907,32.72727272727272,34.54545454545455,36.36363636363636,38.18181818181817,40.0,41.81818181818181,43.636363636363626,45.45454545454545,47.272727272727266,49.09090909090909,50.90909090909091,52.72727272727272,54.54545454545455,56.36363636363636,58.18181818181817,60.0,61.81818181818181,63.636363636363626,65.45454545454545,67.27272727272727,69.0909090909091,70.9090909090909,72.72727272727272,74.54545454545453,76.36363636363635,78.18181818181819,80.0,81.81818181818181,83.63636363636363,85.45454545454544,87.27272727272728,89.0909090909091,90.9090909090909,92.72727272727272,94.54545454545453,96.36363636363635,98.18181818181819,100.0,101.81818181818181,103.63636363636363,105.45454545454544,107.27272727272728,109.0909090909091,110.9090909090909,112.72727272727272,114.54545454545453,116.36363636363635,118.18181818181819,120.0],[-60.0,-58.18181818181818,-56.36363636363637,-54.54545454545455,-52.72727272727273,-50.90909090909091,-49.09090909090909,-47.27272727272727,-45.45454545454545,-43.63636363636364,-41.81818181818182,-40.0,-38.18181818181819,-36.36363636363636,-34.54545454545455,-32.72727272727273,-30.90909090909091,-29.090909090909093,-27.272727272727273,-25.454545454545453,-23.63636363636364,-21.81818181818182,-20.0,-18.18181818181818,-16.363636363636367,-14.545454545454547,-12.727272727272727,-10.909090909090914,-9.090909090909093,-7.272727272727273,-5.454545454545453,-3.6363636363636402,-1.8181818181818201,0.0,1.818181818181813,3.636363636363633,5.454545454545453,7.272727272727266,9.090909090909093,10.909090909090907,12.72727272727272,14.545454545454547,16.36363636363636,18.181818181818173,20.0,21.818181818181813,23.63636363636364,25.454545454545453,27.272727272727266,29.090909090909093,30.909090909090907,32.72727272727272,34.54545454545455,36.36363636363636,38.18181818181817,40.0,41.81818181818181,43.636363636363626,45.45454545454545,47.272727272727266,49.09090909090909,50.90909090909091,52.72727272727272,54.54545454545455,56.36363636363636,58.18181818181817,60.0,61.81818181818181,63.636363636363626,65.45454545454545,67.27272727272727,69.0909090909091,70.9090909090909,72.72727272727272,74.54545454545453,76.36363636363635,78.18181818181819,80.0,81.81818181818181,83.63636363636363,85.45454545454544,87.27272727272728,89.0909090909091,90.9090909090909,92.72727272727272,94.54545454545453,96.36363636363635,98.18181818181819,100.0,101.81818181818181,103.63636363636363,105.45454545454544,107.27272727272728,109.0909090909091,110.9090909090909,112.72727272727272,114.54545454545453,116.36363636363635,118.18181818181819,120.0]],\"y\":[[0.00014466041736498498,0.0002364954522898681,0.000347774990266434,0.000483385291281645,0.0006575364618456427,0.0008865991944367095,0.0011802128671712926,0.001538760515788394,0.0019626673391530233,0.002463229269604687,0.0030593801809616706,0.0037681888945031816,0.004603238822257505,0.005573453925147396,0.0066716054687032305,0.007869457323090355,0.009141016785653612,0.010488477486146144,0.011928537530150416,0.013449940999435464,0.014997200780826975,0.01649620302323868,0.017871282305778377,0.01904303436589916,0.019944835094683887,0.020530414364744575,0.020761233818503068,0.020641699980656795,0.020264638920007513,0.019751611030943323,0.0191377976380114,0.018362912986985926,0.017381449513139427,0.01624098572021199,0.015043282480383146,0.013867826179704829,0.012753193149557966,0.011715883553701494,0.01075544486671288,0.009858938350949613,0.009015292827553046,0.00822318063705608,0.007488074035275637,0.0068145300022379,0.0062096340884544,0.0056923328380857835,0.0052768977066730205,0.0049416572855750275,0.004632634396541463,0.0043055646313532306,0.0039584753942562305,0.0036233347650728095,0.003329774868563954,0.0030801075558501857,0.002858842017543781,0.0026560735931795633,0.002476157022300669,0.00233072037737908,0.0022234174121159344,0.002136178870459286,0.002038007357065514,0.0019135654299813401,0.0017765969005740204,0.0016530473669294802,0.0015539092811474862,0.0014667043576398107,0.001377359339582791,0.0012939732237935433,0.0012357222475536125,0.0012033271225863433,0.001176531702091481,0.0011391080734937077,0.0010927621694749856,0.0010483876427048993,0.001014717062769121,0.000991990477533279,0.0009690793783641916,0.0009311948097025324,0.0008761961562175788,0.0008204701125392954,0.000782818808866859,0.000764077386124822,0.0007493601006745936,0.0007280521273110535,0.000702355127189353,0.0006769225601522338,0.0006510852065250269,0.0006243808581670266,0.0006004353278867102,0.0005807155689362987,0.0005604506345906042,0.0005343581201910858,0.0005024887350391871,0.0004680110903191319,0.0004330217952468899,0.0003993268722162912,0.00036801826372917767,0.00033297704188571016,0.0002808707140791067,0.0002063678574353452],[0.00020814027045655418,0.0003315331314602124,0.0004894958130608881,0.0006926493646989727,0.0009561905799029896,0.001293751459975217,0.0017163701319572054,0.0022342090509621286,0.0028552525424329874,0.003585386818959031,0.0044330716889335285,0.005406606298684828,0.006498280757503075,0.007676904068308571,0.008908148953881324,0.010185016852216504,0.011533080011582543,0.012979234624156892,0.014507648539778715,0.01604360758561718,0.01748649977837628,0.0187592473902591,0.0198174497272879,0.020615727040802192,0.021087560275014926,0.0211750246685585,0.02087995927580036,0.020279505065668238,0.01948623076776634,0.018589239429444616,0.017626568772398807,0.01660523971940216,0.015537084520536705,0.014449813125182221,0.013376266814885018,0.012344417962295375,0.01136714084356683,0.010433575527024196,0.009520100403641256,0.008618145969242819,0.007751760434270299,0.00696570664223791,0.006292306537715663,0.005729056923540934,0.005250511976887876,0.00483646053013675,0.004477871304256394,0.00415783289065906,0.0038467257371325885,0.0035284631181794167,0.0032178084523542976,0.0029382171428621027,0.002694823152871724,0.0024801389863887014,0.0022909934794857964,0.0021266048142415807,0.0019802938286567467,0.001844161952356454,0.0017187663111479378,0.001613996776786584,0.001540337498339001,0.001496710188067346,0.0014663421246984912,0.0014293794724669696,0.0013794391289952282,0.0013231457382961295,0.0012670212661789863,0.00121186089648727,0.0011571286662112352,0.0011039518128958458,0.0010541432662943219,0.0010100821648125842,0.000974875951544414,0.0009495479659227572,0.0009296835814627545,0.0009060529412651934,0.0008694403095414792,0.0008169632676850766,0.0007557908020685697,0.0006994902040154111,0.000658182788190995,0.0006325026459348776,0.0006179354275933482,0.0006124467907140267,0.000616546893406089,0.0006268724756932705,0.0006344342501002998,0.0006316022511303142,0.0006188427484012037,0.0006032497422398394,0.0005914166516436265,0.0005837179383750923,0.0005746676617059557,0.0005582553529925325,0.0005318905789043947,0.0004948506896717602,0.00044465230208297324,0.00037748389462215014,0.0002937390025867139,0.00020282074715985082],[7.067167819032317e-05,0.00010881381864865087,0.00015043717287551301,0.000196093230522124,0.0002519405718891634,0.0003274819554618171,0.0004310902617781955,0.0005662790475826105,0.0007326509112510572,0.0009327309366017327,0.001179508551624075,0.001498857054742168,0.0019259139848225846,0.0024971963495106658,0.003239938630584265,0.004164807310393156,0.005275613756837798,0.006592037981708867,0.0081511121901783,0.009975316953371038,0.012041936274558547,0.014273109869973603,0.01653672240141069,0.018666174195168406,0.02050033773571951,0.02190813939485065,0.022791605858853668,0.023110084773698275,0.022914781645599413,0.022320650444710318,0.02142582328225777,0.02028717418431215,0.01897262722422206,0.017581401429200054,0.016196246464308253,0.01485702639271494,0.01358813679940127,0.012415568755020182,0.011350972866156736,0.010384912212990033,0.009505244854415184,0.008711918352183011,0.008008018533965582,0.007383844810038528,0.006818677552865653,0.00629460781246705,0.005803391808412189,0.005346807181207217,0.004936752171522795,0.0045880864127587325,0.004303459485327453,0.004066963504830944,0.0038545901897382583,0.003649199470921983,0.0034481245234065355,0.0032599401655888454,0.003091531970670075,0.002936665050969811,0.002779962719415168,0.002612517472589371,0.0024424989524890963,0.0022901617135036726,0.0021694201587359914,0.0020755409835307385,0.001994906543298868,0.0019194286114242705,0.0018453251916807259,0.0017682975133858385,0.0016865845878201676,0.0016014927947296414,0.0015150313521727684,0.0014335410790228325,0.00136762371389809,0.0013209927571516805,0.0012845168406064606,0.0012464010744364015,0.0012042153789307943,0.0011623674103365044,0.0011222788149360056,0.0010820504697046892,0.0010419940025375338,0.0010036743347061456,0.0009671933075895601,0.0009336587829111081,0.0009060281823904483,0.0008850019298192763,0.0008670948786840257,0.0008477397003692974,0.0008245078314126922,0.0007966522982613902,0.0007638702949866429,0.0007288196691489991,0.0006985654814383049,0.0006780674473902215,0.0006635244207763145,0.0006454963674274051,0.0006144059572741144,0.0005592374710785557,0.00046849456255608696,0.0003438387502499659],[0.0001608498402996718,0.00026183069967405617,0.0003780783508129276,0.0005079314660107884,0.0006629064806400309,0.0008657422676135325,0.0011383871725144307,0.0014853011351359502,0.0018957400511049561,0.00237622378821708,0.002971759988169328,0.0037376576740606655,0.004695319893430941,0.00582953307719274,0.007126588086334973,0.008581049554388976,0.01014900961181248,0.011755868637985731,0.013383798336043494,0.015079786419750316,0.016839139712574887,0.018534300791588597,0.019988156341039825,0.02108822892813851,0.021810463052034122,0.022170500576388245,0.022196143447162024,0.021922992985528526,0.021377779976229774,0.020579809100794273,0.019571100048565656,0.018431195457036403,0.017250410883479072,0.016080692590445388,0.014919247490089538,0.013742873044248816,0.01255212482965984,0.011387781658612244,0.01030389190777444,0.009322800085306565,0.008425090910150137,0.0075799071552835225,0.006774700571073234,0.006022066858705241,0.005347093604502438,0.004763113948019598,0.004263759179321536,0.003842015689926016,0.0034980380961328345,0.0032241072039690963,0.002995900734897164,0.0027853359822128205,0.002581706710165311,0.0023908426095692686,0.0022174162717694085,0.0020645329332928835,0.001940142603977394,0.0018461570035166529,0.0017686548200895721,0.0016890283425388438,0.001601204686783912,0.0015133822955535675,0.001436878725928263,0.0013755730235299598,0.0013195420356053153,0.0012509364579172061,0.0011662639783432435,0.0010853710514860382,0.0010281849790304025,0.0009937792838175484,0.0009682694321493658,0.0009406886697925853,0.0009068672655351772,0.0008667035247892487,0.0008236553018770418,0.0007844499594030059,0.0007521896039091678,0.0007209773022151456,0.0006863856801248298,0.0006571707415785611,0.0006449705060344306,0.0006475888230896582,0.0006522270260578598,0.0006485221393065669,0.0006328792266776212,0.0006076354704942128,0.0005792177921139445,0.0005536378087153928,0.000533802335278741,0.0005195055789730102,0.0005051777713980135,0.00048111372185509623,0.0004441022060983455,0.00040283034421640475,0.00036820597494553205,0.0003437155985165173,0.0003254098535387796,0.0003030501309358363,0.0002620660876005722,0.00019606842882948343],[1.3546843109799824e-05,2.412340704098241e-05,3.866473234620265e-05,5.826117686488847e-05,8.585596283565666e-05,0.00012653755673787222,0.00018748158425231008,0.00027894915753259663,0.00041594254111220917,0.0006171778231708161,0.0008994431734020321,0.0012724986694075595,0.001743200637230778,0.002328758976330535,0.0030666073294800117,0.004008208678122415,0.005196597824773416,0.006641649694502198,0.00831258106470649,0.010153906677861407,0.0121048222664018,0.01409529776301472,0.016024107890883054,0.017757562485770904,0.019169388711632433,0.020189208926283383,0.020813655964161914,0.021075689108274172,0.02100580774411723,0.020619423189946157,0.019935939618821435,0.019001470880624982,0.017888077092658376,0.01667574086987017,0.015435649059074819,0.014219573750910817,0.013057325397167108,0.011964367891444398,0.010949881701286173,0.010017001447736127,0.009163445702168378,0.008389171992279485,0.007700951129797585,0.007105499701793456,0.0065999265389473875,0.006171060773698116,0.005803069357450936,0.005484167883771777,0.005205009994786794,0.004952449826224054,0.004710272913912726,0.004468247993547614,0.004227884862279932,0.0039992881526234965,0.0037946303458648322,0.0036222308908224325,0.0034813759129323192,0.0033618631175930027,0.0032524698382756165,0.003150454255869176,0.0030583511707470696,0.0029717767216328544,0.0028776779023459618,0.0027678532538533696,0.0026494523675893193,0.00253769146749945,0.0024397910102254266,0.002350500351376688,0.0022639556149196415,0.0021844214010841904,0.002120166556666129,0.002069172700781828,0.002017881531650497,0.001955506561548634,0.0018843392208015772,0.0018140893534725757,0.0017505689850752025,0.0016922682707817152,0.001635365704070038,0.001579305397039176,0.0015265522091727445,0.0014783850874079386,0.0014342516213190167,0.0013953256916498763,0.001364678126040611,0.0013421775746138855,0.0013212760025591403,0.0012922620047356717,0.0012488234066682098,0.0011926183064822672,0.0011329414506768137,0.00108148890893179,0.0010450749408753895,0.001020806567976501,0.0009969234521079342,0.0009584080517245342,0.000892481947787064,0.0007906228346336139,0.000650650404122317,0.0004828879518115329]]}},\"id\":\"aa00a7aa-989b-43fd-b037-c3896dbff7b7\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"dab7f579-096c-4076-b1bd-9db957856901\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"4c972253-10e7-4750-8472-f1f60abb7ba4\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"8e3677f7-d256-4bba-8bfc-35071a96e3c7\",\"type\":\"ResetTool\"},{\"attributes\":{\"axis_label\":\"Delay (min)\",\"formatter\":{\"id\":\"e2480d2e-c430-4816-a81d-55e3189be115\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"501d32c4-6dc9-49d7-8e51-30f100a8bb8b\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"3a7971be-96f3-44c7-ab8f-2b490079b0d2\",\"type\":\"BasicTicker\"}},\"id\":\"a39c8be3-d358-46c5-b148-a3269afdc5e4\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"3a7971be-96f3-44c7-ab8f-2b490079b0d2\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"ba120379-8f13-4a15-a616-4c0a75233edb\",\"type\":\"HelpTool\"},{\"attributes\":{\"callback\":null},\"id\":\"785568d4-9f15-473d-9983-55772c29fe8f\",\"type\":\"DataRange1d\"},{\"attributes\":{\"source\":{\"id\":\"aa00a7aa-989b-43fd-b037-c3896dbff7b7\",\"type\":\"ColumnDataSource\"}},\"id\":\"35ffa649-0db7-42cd-9d58-2a1413199f53\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"b80b283b-dfd5-485f-b55a-f595a189fec8\",\"type\":\"LinearScale\"},{\"attributes\":{\"line_color\":{\"field\":\"color\"},\"line_width\":{\"value\":3},\"xs\":{\"field\":\"x\"},\"ys\":{\"field\":\"y\"}},\"id\":\"6342ea48-e111-48c8-9ace-2f276026429c\",\"type\":\"MultiLine\"},{\"attributes\":{\"label\":{\"field\":\"label\"},\"renderers\":[{\"id\":\"8261d0a0-9163-4b77-99c8-d25574eda559\",\"type\":\"GlyphRenderer\"}]},\"id\":\"39b3758e-8139-4418-8bf9-e4f8d9c81c02\",\"type\":\"LegendItem\"},{\"attributes\":{\"plot\":{\"id\":\"501d32c4-6dc9-49d7-8e51-30f100a8bb8b\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"3a7971be-96f3-44c7-ab8f-2b490079b0d2\",\"type\":\"BasicTicker\"}},\"id\":\"567170bf-5b19-40f3-974b-78ab6c07ff07\",\"type\":\"Grid\"},{\"attributes\":{\"axis_label\":\"Density\",\"formatter\":{\"id\":\"5adc3050-456e-4dd8-9d03-560082522fe7\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"501d32c4-6dc9-49d7-8e51-30f100a8bb8b\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"40602d8c-c820-4bbd-9d98-d35238db57ab\",\"type\":\"BasicTicker\"}},\"id\":\"1bbe232b-33d3-4a44-956b-eefe963d3891\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"e2480d2e-c430-4816-a81d-55e3189be115\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"below\":[{\"id\":\"a39c8be3-d358-46c5-b148-a3269afdc5e4\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"1bbe232b-33d3-4a44-956b-eefe963d3891\",\"type\":\"LinearAxis\"}],\"plot_height\":500,\"plot_width\":500,\"renderers\":[{\"id\":\"a39c8be3-d358-46c5-b148-a3269afdc5e4\",\"type\":\"LinearAxis\"},{\"id\":\"567170bf-5b19-40f3-974b-78ab6c07ff07\",\"type\":\"Grid\"},{\"id\":\"1bbe232b-33d3-4a44-956b-eefe963d3891\",\"type\":\"LinearAxis\"},{\"id\":\"51718264-152c-4988-a154-b6a6228a2615\",\"type\":\"Grid\"},{\"id\":\"3348c287-2032-4391-a0dc-dfd8e9a42b24\",\"type\":\"BoxAnnotation\"},{\"id\":\"4ab84ef2-3c93-4850-9f46-21aa47e57e80\",\"type\":\"Legend\"},{\"id\":\"8261d0a0-9163-4b77-99c8-d25574eda559\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"3871a116-db17-4344-8b77-5d1641a61f77\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"347287d5-12c8-4da0-ae82-158ee7b065e5\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"47cae002-17e7-4962-8f70-5f4362b92660\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"4c972253-10e7-4750-8472-f1f60abb7ba4\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"785568d4-9f15-473d-9983-55772c29fe8f\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"b80b283b-dfd5-485f-b55a-f595a189fec8\",\"type\":\"LinearScale\"}},\"id\":\"501d32c4-6dc9-49d7-8e51-30f100a8bb8b\",\"subtype\":\"Figure\",\"type\":\"Plot\"}],\"root_ids\":[\"501d32c4-6dc9-49d7-8e51-30f100a8bb8b\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.14\"}};\n", " var render_items = [{\"docid\":\"682909b6-da61-49d1-b451-ebff55f2f1a6\",\"elementid\":\"e43fc0b7-857a-4a10-9342-ec47a5421be6\",\"modelid\":\"501d32c4-6dc9-49d7-8e51-30f100a8bb8b\"}];\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": "501d32c4-6dc9-49d7-8e51-30f100a8bb8b" } }, "output_type": "display_data" } ], "source": [ "carrier_list = available_carriers[:5]\n", "kde_src = make_kde_dataset(carrier_list, range_start=-60, range_end=120)\n", "p = figure(plot_width = 500, plot_height = 500, title = 'Density Plots of Airline Delays',\n", " x_axis_label = 'Delay (min)', y_axis_label = 'Density')\n", "\n", "p.multi_line('x', 'y', color = 'color', legend = 'label', line_width = 3, source = kde_src)\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Density Plot in Application" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def modify_doc(doc):\n", " \n", " def make_dataset(carrier_list, range_start = -60, range_end = 120, bin_width = 5):\n", "\n", " by_carrier = pd.DataFrame(columns=['proportion', 'left', 'right', \n", " 'f_proportion', 'f_interval',\n", " 'name', 'color'])\n", " range_extent = range_end - range_start\n", "\n", " # Iterate through all the carriers\n", " for i, carrier_name in enumerate(carrier_list):\n", "\n", " # Subset to the carrier\n", " subset = flights[flights['name'] == carrier_name]\n", "\n", " # Create a histogram with 5 minute bins\n", " arr_hist, edges = np.histogram(subset['arr_delay'], \n", " bins = int(range_extent / bin_width), \n", " range = [range_start, range_end])\n", "\n", " # Divide the counts by the total to get a proportion\n", " arr_df = pd.DataFrame({'proportion': arr_hist / np.sum(arr_hist), 'left': edges[:-1], 'right': edges[1:] })\n", "\n", " # Format the proportion \n", " arr_df['f_proportion'] = ['%0.5f' % proportion for proportion in arr_df['proportion']]\n", "\n", " # Format the interval\n", " arr_df['f_interval'] = ['%d to %d minutes' % (left, right) for left, right in zip(arr_df['left'], arr_df['right'])]\n", "\n", " # Assign the carrier for labels\n", " arr_df['name'] = carrier_name\n", "\n", " # Color each carrier differently\n", " arr_df['color'] = Category20_16[i]\n", "\n", " # Add to the overall dataframe\n", " by_carrier = by_carrier.append(arr_df)\n", "\n", " # Overall dataframe\n", " by_carrier = by_carrier.sort_values(['name', 'left'])\n", "\n", " return ColumnDataSource(by_carrier)\n", " \n", " \n", " def make_kde_dataset(carrier_list, range_start, range_end, bandwidth):\n", " \n", " xs = []\n", " ys = []\n", " colors = []\n", " labels = []\n", "\n", " for i, carrier in enumerate(carrier_list):\n", " subset = flights[flights['name'] == carrier]\n", " subset = subset[subset['arr_delay'].between(range_start, range_end)]\n", "\n", " kde = gaussian_kde(subset['arr_delay'], bw_method=bandwidth)\n", " # Evenly space x values\n", " x = np.linspace(range_start, range_end, 100)\n", " # Evaluate pdf at every value of x\n", " y = kde.pdf(x)\n", "\n", " # Append the values to plot\n", " xs.append(list(x))\n", " ys.append(list(y))\n", "\n", " # Append the colors and label\n", " colors.append(airline_colors[i])\n", " labels.append(carrier)\n", "\n", " kernel_source = ColumnDataSource(data={'x': xs, 'y': ys, 'color': colors, 'label': labels})\n", " \n", " return kernel_source\n", " \n", " def style(p):\n", " # Title \n", " p.title.align = 'center'\n", " p.title.text_font_size = '20pt'\n", " p.title.text_font = 'serif'\n", "\n", " # Axis titles\n", " p.xaxis.axis_label_text_font_size = '14pt'\n", " p.xaxis.axis_label_text_font_style = 'bold'\n", " p.yaxis.axis_label_text_font_size = '14pt'\n", " p.yaxis.axis_label_text_font_style = 'bold'\n", "\n", " # Tick labels\n", " p.xaxis.major_label_text_font_size = '12pt'\n", " p.yaxis.major_label_text_font_size = '12pt'\n", "\n", " return p\n", " \n", " def make_plot(src):\n", " # Blank plot with correct labels\n", " p = figure(plot_width = 700, plot_height = 700, \n", " title = 'Histogram of Arrival Delays by Carrier',\n", " x_axis_label = 'Delay (min)', y_axis_label = 'Proportion')\n", "\n", " # Quad glyphs to create a histogram\n", " p.quad(source = src, bottom = 0, top = 'proportion', left = 'left', right = 'right',\n", " color = 'color', fill_alpha = 0.7, hover_fill_color = 'color', legend = 'name',\n", " hover_fill_alpha = 1.0, line_color = 'black')\n", "\n", " # Hover tool with vline mode\n", " hover = HoverTool(tooltips=[('Carrier', '@name'), \n", " ('Delay', '@f_interval'),\n", " ('Proportion', '@f_proportion')],\n", " mode='vline')\n", "\n", " p.add_tools(hover)\n", "\n", " # Styling\n", " p = style(p)\n", "\n", " return p\n", " \n", " def make_kde_plot(kde_src):\n", " p = figure(plot_width = 700, plot_height = 700,\n", " title = 'Density Plot of Arrival Delays by Carrier',\n", " x_axis_label = 'Delay (min)', y_axis_label = 'Density')\n", " \n", " \n", " p.multi_line('x', 'y', color = 'color', legend = 'label', \n", " line_width = 3,\n", " source = kde_src)\n", " \n", " # Hover tool with next line policy\n", " hover = HoverTool(tooltips=[('Carrier', '@label'), \n", " ('Delay', '$x'),\n", " ('Density', '$y')],\n", " line_policy = 'next')\n", " \n", " # Add the hover tool and styling\n", " p.add_tools(hover)\n", " \n", " p = style(p)\n", " \n", " return p\n", " \n", " def update(attr, old, new):\n", " carriers_to_plot = [carrier_selection.labels[i] for i in carrier_selection.active]\n", " \n", " new_src = make_dataset(carriers_to_plot,\n", " range_start = range_select.value[0],\n", " range_end = range_select.value[1],\n", " bin_width = binwidth_select.value)\n", " \n", " if bandwidth_choose.active == []:\n", " bandwidth = None\n", " else:\n", " bandwidth = bandwidth_select.value\n", " \n", " new_kde_src = make_kde_dataset(carriers_to_plot,\n", " range_start = range_select.value[0],\n", " range_end = range_select.value[1],\n", " bandwidth = bandwidth)\n", "\n", " src.data.update(new_src.data)\n", " kde_src.data.update(new_kde_src.data)\n", " \n", " \n", " carrier_selection = CheckboxGroup(labels=available_carriers, active = [0, 1])\n", " carrier_selection.on_change('active', update)\n", " \n", " binwidth_select = Slider(start = 1, end = 30, \n", " step = 1, value = 5,\n", " title = 'Delay Width (min)')\n", " binwidth_select.on_change('value', update)\n", " \n", " range_select = RangeSlider(start = -60, end = 180, value = (-60, 120),\n", " step = 5, title = 'Delay Range (min)')\n", " range_select.on_change('value', update)\n", " \n", " \n", " bandwidth_select = Slider(start = 0.1, end = 5, \n", " step = 0.1, value = 0.5,\n", " title = 'Bandwidth for Density Plot')\n", " bandwidth_select.on_change('value', update)\n", " \n", " bandwidth_choose = CheckboxButtonGroup(labels=['Choose Bandwidth (Auto)'], active = [])\n", " bandwidth_choose.on_change('active', update)\n", "\n", " initial_carriers = [carrier_selection.labels[i] for i in carrier_selection.active]\n", " \n", " src = make_dataset(initial_carriers,\n", " range_start = range_select.value[0],\n", " range_end = range_select.value[1],\n", " bin_width = binwidth_select.value)\n", " \n", " kde_src = make_kde_dataset(initial_carriers, \n", " range_start = range_select.value[0],\n", " range_end = range_select.value[1],\n", " bandwidth = bandwidth_select.value) \n", " \n", " p = make_plot(src)\n", " kde_p = make_kde_plot(kde_src)\n", " \n", " # Put controls in a single element\n", " controls = WidgetBox(carrier_selection, binwidth_select, range_select, \n", " bandwidth_select, bandwidth_choose)\n", " \n", " # Create a row layout\n", " layout = row(controls, p, kde_p)\n", " \n", " # Make a tab with the layout \n", " tab = Panel(child=layout, title = 'Histogram and Density Plots')\n", " tabs = Tabs(tabs=[tab])\n", " \n", " doc.add_root(tabs)\n", " \n", "# Set up an application\n", "handler = FunctionHandler(modify_doc)\n", "app = Application(handler)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "application/vnd.bokehjs_exec.v0+json": "", "text/html": [ "\n", "" ] }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "server_id": "0dd984b093764ecb9204efbcc01b9e13" } }, "output_type": "display_data" } ], "source": [ "show(app)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Shaded Density Plots in Application" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }