{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 第7章 Bokehを使いこなそう\n", "\n", "### 7-3: 色の調整" ] }, { "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(global) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " var force = true;\n", "\n", " if (typeof (window._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n", " window._bokeh_onload_callbacks = [];\n", " window._bokeh_is_loading = undefined;\n", " }\n", "\n", "\n", " \n", " if (typeof (window._bokeh_timeout) === \"undefined\" || force === true) {\n", " window._bokeh_timeout = Date.now() + 5000;\n", " window._bokeh_failed_load = false;\n", " }\n", "\n", " var NB_LOAD_WARNING = {'data': {'text/html':\n", " \"
\\n\"+\n", " \"

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

\\n\"+\n", " \"\\n\"+\n", " \"\\n\"+\n", " \"from bokeh.resources import INLINE\\n\"+\n", " \"output_notebook(resources=INLINE)\\n\"+\n", " \"\\n\"+\n", " \"
\"}};\n", "\n", " function display_loaded() {\n", " if (window.Bokeh !== undefined) {\n", " var el = document.getElementById(\"6a5122f4-3eec-4b89-a7e3-bf1e5c9ebc2a\");\n", " el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n", " } else if (Date.now() < window._bokeh_timeout) {\n", " setTimeout(display_loaded, 100)\n", " }\n", " }\n", "\n", " function run_callbacks() {\n", " window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n", " delete window._bokeh_onload_callbacks\n", " console.info(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(js_urls, callback) {\n", " window._bokeh_onload_callbacks.push(callback);\n", " if (window._bokeh_is_loading > 0) {\n", " console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " window._bokeh_is_loading = js_urls.length;\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " var s = document.createElement('script');\n", " s.src = url;\n", " s.async = false;\n", " s.onreadystatechange = s.onload = function() {\n", " window._bokeh_is_loading--;\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: all BokehJS libraries loaded\");\n", " run_callbacks()\n", " }\n", " };\n", " s.onerror = function() {\n", " console.warn(\"failed to load library \" + url);\n", " };\n", " console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", " }\n", " };var element = document.getElementById(\"6a5122f4-3eec-4b89-a7e3-bf1e5c9ebc2a\");\n", " if (element == null) {\n", " console.log(\"Bokeh: ERROR: autoload.js configured with elementid '6a5122f4-3eec-4b89-a7e3-bf1e5c9ebc2a' 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.5.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.js\"];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " \n", " },\n", " \n", " function(Bokeh) {\n", " \n", " document.getElementById(\"6a5122f4-3eec-4b89-a7e3-bf1e5c9ebc2a\").textContent = \"BokehJS is loading...\";\n", "\n", " },\n", " function(Bokeh) {\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.css\");\n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " \n", " if ((window.Bokeh !== undefined) || (force === true)) {\n", " for (var i = 0; i < inline_js.length; i++) {\n", " inline_js[i](window.Bokeh);\n", " }if (force === true) {\n", " display_loaded();\n", " }} else if (Date.now() < window._bokeh_timeout) {\n", " setTimeout(run_inline_js, 100);\n", " } else if (!window._bokeh_failed_load) {\n", " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", " window._bokeh_failed_load = true;\n", " } else if (force !== true) {\n", " var cell = $(document.getElementById(\"6a5122f4-3eec-4b89-a7e3-bf1e5c9ebc2a\")).parents('.cell').data().cell;\n", " cell.output_area.append_execute_result(NB_LOAD_WARNING)\n", " }\n", "\n", " }\n", "\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(js_urls, function() {\n", " console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(this));" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.3.1:カラーネームによる色の設定\n", "from bokeh.plotting import figure, output_notebook, show\n", "\n", "output_notebook()\n", "p = figure(plot_width=200, plot_height=200)\n", "p.circle([0], [0], size=30, color=\"red\")\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.3.2:16進数カラーコードによる色の設定\n", "p = figure(plot_width=200, plot_height=200)\n", "p.circle([0], [0], size=30, color=\"#FF0000\")\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.3.3:RGBカラーコードによる色の設定\n", "p = figure(plot_width=200, plot_height=200)\n", "p.circle([0], [0], size=30, color=(255, 0, 0))\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.3.4:オブジェクトの不透明度を設定\n", "p = figure(plot_width=200, plot_height=200)\n", "p.circle([0, 0.5, 1], [0, 0, 0], size=30, color=(0, 0, 255))\n", "p.circle([0.25, 0.75], [0, 0], size=50, color=(255, 0, 0, 0.5))\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.3.5:alphaによる透過設定\n", "p = figure(plot_width=200, plot_height=200)\n", "p.circle([0, 0.5, 1], [0, 0, 0], size=30, color=\"blue\")\n", "p.circle([0.25, 0.75], [0, 0], size=50, color=\"red\", alpha=0.5)\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.3.6:fill_colorを設定\n", "p = figure(plot_width=200, plot_height=200)\n", "p.circle([0], [0], size=30, fill_color=\"red\", line_width=5)\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.3.7:line_colorを設定\n", "p = figure(plot_width=200, plot_height=200)\n", "p.circle([0], [0], size=30, fill_color=\"red\", line_color=\"black\", line_width=5)\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['#08519c', '#3182bd', '#6baed6', '#9ecae1', '#c6dbef', '#eff3ff']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト7.3.8:Blues6を指定してインポート\n", "from bokeh.palettes import Blues6\n", "\n", "Blues6" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{3: ['#3182bd', '#9ecae1', '#deebf7'],\n", " 4: ['#2171b5', '#6baed6', '#bdd7e7', '#eff3ff'],\n", " 5: ['#08519c', '#3182bd', '#6baed6', '#bdd7e7', '#eff3ff'],\n", " 6: ['#08519c', '#3182bd', '#6baed6', '#9ecae1', '#c6dbef', '#eff3ff'],\n", " 7: ['#084594',\n", " '#2171b5',\n", " '#4292c6',\n", " '#6baed6',\n", " '#9ecae1',\n", " '#c6dbef',\n", " '#eff3ff'],\n", " 8: ['#084594',\n", " '#2171b5',\n", " '#4292c6',\n", " '#6baed6',\n", " '#9ecae1',\n", " '#c6dbef',\n", " '#deebf7',\n", " '#f7fbff'],\n", " 9: ['#08306b',\n", " '#08519c',\n", " '#2171b5',\n", " '#4292c6',\n", " '#6baed6',\n", " '#9ecae1',\n", " '#c6dbef',\n", " '#deebf7',\n", " '#f7fbff']}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# リスト7.3.10:Bluesを指定してインポート\n", "from bokeh.palettes import Blues\n", "\n", "Blues" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.3.12:Blues6による着色\n", "p = figure(plot_width=200, plot_height=200, x_range=(0, 2), y_range=(-0.5, 5.5))\n", "for i, color in enumerate(Blues6):\n", " p.square(0.5, i, color=color, size=20)\n", " p.text(1, i, text=[color], text_baseline=\"middle\")\n", "show(p)" ] }, { "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.2" } }, "nbformat": 4, "nbformat_minor": 2 }