{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 第7章 Bokehを使いこなそう\n", "\n", "### 7-1: さまざまな図形の描画" ] }, { "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(\"5f266e9f-5d8d-47cd-849b-6e12196f1ee4\");\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(\"5f266e9f-5d8d-47cd-849b-6e12196f1ee4\");\n", " if (element == null) {\n", " console.log(\"Bokeh: ERROR: autoload.js configured with elementid '5f266e9f-5d8d-47cd-849b-6e12196f1ee4' 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(\"5f266e9f-5d8d-47cd-849b-6e12196f1ee4\").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(\"5f266e9f-5d8d-47cd-849b-6e12196f1ee4\")).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.1.1:テキストの描画\n", "from bokeh.plotting import figure, output_notebook, show\n", "\n", "output_notebook()\n", "p = figure(plot_width=200, plot_height=200, x_range=(0.5, 2.5))\n", "p.text([1, 2], [1, 1], text=[\"text1\", \"text2\"])\n", "p.x([1, 2], [1, 1], size=10)\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.1.2:circle()メソッドによる円形の描画\n", "p = figure(plot_width=200, plot_height=200)\n", "p.circle([1, 2], [1, 2])\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.1.4:長方形の描画\n", "from numpy import pi\n", "\n", "p = figure(plot_width=400, plot_height=400, x_range=(-6, 6))\n", "# quad\n", "p.quad(top=[12], bottom=[10], left=[-1], right=[1], alpha=0.5)\n", "# rect\n", "p.rect(x=[0], y=[8], width=2, height=2, angle=pi * 0.25, alpha=0.5)\n", "# vbar\n", "p.vbar(x=[0], bottom=[4], top=[6], width=2, alpha=0.5)\n", "# hbar\n", "p.hbar(y=[2], left=[-1], right=[1], height=2, alpha=0.5)\n", "# 注釈\n", "# quad\n", "p.text([-4], [11], text=[\"quad\"], text_baseline=\"middle\")\n", "p.text(\n", " [0, 0, -1, 1],\n", " [12, 10, 11, 11],\n", " text=[\"top=12\", \"bottom=10\", \"left=-1\", \"right=1\"],\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\n", "# rect\n", "p.text([-4], [8], text=[\"rect\"], text_baseline=\"middle\")\n", "p.text([0], [8], text=[\"x=0, y=8\"], text_align=\"center\", text_baseline=\"middle\")\n", "p.text(\n", " [-1],\n", " [8.5],\n", " text=[\"width=2\"],\n", " angle=pi * 0.25,\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\n", "p.text(\n", " [1],\n", " [8.5],\n", " text=[\"height=2\"],\n", " angle=pi * -0.25,\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\n", "p.text([0], [7], text=[\"angle=pi*0.25\"], text_align=\"center\", text_baseline=\"middle\")\n", "# vbar\n", "p.text([-4], [5], text=[\"vbar\"], text_baseline=\"middle\")\n", "p.text([0], [5], text=[\"x=0, width=2\"], text_align=\"center\", text_baseline=\"middle\")\n", "p.text(\n", " [0, 0],\n", " [6, 4],\n", " text=[\"top=6\", \"bottom=4\"],\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\n", "# hbar\n", "p.text([-4], [2], text=[\"hbar\"], text_baseline=\"middle\")\n", "p.text(\n", " [0, 0],\n", " [2.2, 1.8],\n", " text=[\"y=2,\", \"height=2\"],\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\n", "p.text(\n", " [-1.5, 1.5],\n", " [2, 2],\n", " text=[\"left=-1\", \"right=1\"],\n", " angle=pi * 0.5,\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\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.1.5:多角形の描画\n", "p = figure(plot_width=400, plot_height=400, x_range=(-0.5, 5.5))\n", "# patch\n", "p.patch([0, 2, 1], [0, 0, 1], alpha=0.5)\n", "# patches\n", "p.patches([[3, 4, 3], [4, 5, 5]], [[0, 0, 1], [1, 1, 0]], alpha=0.5)\n", "# 注釈\n", "# patch\n", "p.text(\n", " [1, 1, 1],\n", " [0.6, 0.5, 0.4],\n", " text=[\"patch\", \"x=[0, 2, 1]\", \"y=[0, 0, 1]\"],\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\n", "p.text(\n", " [0, 2, 1],\n", " [0, 0, 1],\n", " text=[\"(0, 0)\", \"(2, 0)\", \"(1, 1)\"],\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\n", "# patches\n", "p.text(\n", " [4, 4, 4],\n", " [0.6, 0.5, 0.4],\n", " text=[\"patches\", \"xs=[[3, 4, 3], [4, 5, 5]]\", \"ys=[[0, 0, 1], [1, 1, 0]]\"],\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\n", "p.text(\n", " [3, 4, 3],\n", " [0, 0, 1],\n", " text=[\"(3, 0)\", \"(4, 0)\", \"(3, 1)\"],\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\n", "p.text(\n", " [4, 5, 5],\n", " [1, 1, 0],\n", " text=[\"(4, 1)\", \"(5, 1)\", \"(5, 0)\"],\n", " text_align=\"center\",\n", " text_baseline=\"middle\",\n", ")\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.1.6:楕円の描画\n", "p = figure(plot_width=400, plot_height=200, x_range=(-0.5, 1.5), y_range=(-0.5, 0.5))\n", "# oval\n", "p.oval(x=[0], y=[0], width=1, height=1, alpha=0.5)\n", "# ellipse\n", "p.ellipse(x=[1], y=[0], width=1, height=1, alpha=0.5)\n", "# 注釈\n", "# oval\n", "p.text(x=[0], y=[0], text=[\"oval\"], text_align=\"center\", text_baseline=\"middle\")\n", "# ellipse\n", "p.text(x=[1], y=[0], text=[\"ellipse\"], text_align=\"center\", text_baseline=\"middle\")\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.1.7:円形の描画\n", "p = figure(plot_width=400, plot_height=400, x_range=(-0.5, 1.5), y_range=(0.5, 2.5))\n", "p.arc(x=[0], y=[2], radius=0.5, start_angle=0, end_angle=pi * 1.5)\n", "p.wedge(x=[0], y=[1], radius=0.5, start_angle=pi * 1.5, end_angle=0)\n", "p.annulus(x=[1], y=[2], inner_radius=0.3, outer_radius=0.5)\n", "p.annular_wedge(\n", " x=[1], y=[1], inner_radius=0.3, outer_radius=0.5, start_angle=0, end_angle=pi * 1\n", ")\n", "# 注釈\n", "# arc\n", "p.text(x=[0], y=[2], text=[\"arc\"], text_align=\"center\", text_baseline=\"middle\")\n", "# wedge\n", "p.text(x=[0], y=[1], text=[\"wedge\"], text_align=\"center\", text_baseline=\"bottom\")\n", "# annulus\n", "p.text(x=[1], y=[2], text=[\"annulus\"], text_align=\"center\", text_baseline=\"middle\")\n", "# annular_wedge\n", "p.text(x=[1], y=[1], text=[\"annular_wedge\"], text_align=\"center\", text_baseline=\"top\")\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.1.8:line()メソッドの利用\n", "p = figure(plot_width=200, plot_height=200)\n", "p.line([0, 1], [0, 1])\n", "p.line([1, 2], [0, 1])\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.1.9:multi_line()メソッドの利用\n", "p = figure(plot_width=200, plot_height=200)\n", "p.multi_line([[0, 1], [1, 2]], [[0, 1], [0, 1]])\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.1.10:segment()メソッドの利用\n", "p = figure(plot_width=200, plot_height=200)\n", "p.segment(x0=[0, 1], y0=[0, 0], x1=[1, 2], y1=[1, 1])\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.1.12:辞書を利用\n", "data_dict = {\"x\": [1, 2], \"y\": [1, 1], \"text\": [\"a\", \"b\"]}\n", "p = figure(plot_width=200, plot_height=200, x_range=(0.5, 2.5))\n", "p.circle(x=\"x\", y=\"y\", source=data_dict)\n", "p.line(x=\"x\", y=\"y\", source=data_dict)\n", "p.text(x=\"x\", y=\"y\", text=\"text\", source=data_dict)\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# リスト7.1.13:DataFrameを利用\n", "import pandas as pd\n", "\n", "df = pd.DataFrame(data_dict)\n", "p = figure(plot_width=200, plot_height=200, x_range=(0.5, 2.5))\n", "p.circle(x=\"x\", y=\"y\", source=df)\n", "p.line(x=\"x\", y=\"y\", source=df)\n", "p.text(x=\"x\", y=\"y\", text=\"text\", source=df)\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 }