{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualisierung von Netzwerkgraphen\n", "\n", "Bokeh unterstützt nativ die Erstellung von Netzwerkgraphen mit konfigurierbaren Interaktionen zwischen Kanten und Knoten." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Edge- und Node-Renderer \n", "\n", "Das Hauptmerkmal von `GraphRenderer` ist, dass es separate GlyphRenderer für Diagrammknoten und Diagrammkanten gibt. Dies ermöglicht das Anpassen der Knoten durch Ändern einer Eigenschaft des `node_renderer`. Es ist möglich, den üblichen Kreisknoten-Glyph durch eine beliebige XYGlyph-Instanz zu ersetzen, z.B. durch einen rechteckige oder elliptische Glyphe. Ebenso können die Stileigenschaften der Kanten über die `edge_renderer`-Eigenschaft geändert werden. Aktuell können Kanten jedoch nur als MultiLine-Zeichen gerendert werden.\n", "\n", "Für die Datenquellen, die zu diesen beiden Sub-Renderern gehören, gelten einige Anforderungen:\n", "\n", "* Die `ColumnDataSource`, die dem `node_renderer` zugeordnet ist, muss eine Spalte mit dem Namen `\"index\"` haben, die die eindeutigen Indizes der Knoten enthält.\n", "* Die `ColumnDataSource`, die dem `edge_renderer` zugeordnet ist, enthält zwei erforderliche Spalten: `\"start\"` und `\"end\"`. Diese Spalten enthalten die Knotenindizes für den Anfang und das Ende der Kanten.\n", "\n", "Es ist möglich, diesen Datenquellen zusätzliche Metadaten hinzuzufügen, um ein vektorisiertes Glyphen-Styling hinzuzufügen oder Daten für Callbacks oder Tooltips verfügbar zu machen.\n", "\n", "Im Folgenden zeige ich ein Code-Snippet, das:\n", "\n", "* einen Knoten elliptisch darstellt\n", "* `height`- und `width`-Attribute der Ellipse festlegt\n", "* das `fill_color`-Attribut der Ellipse festlegt" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(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", " 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(\"aaf012cd-698d-4332-9411-c846424fa65f\");\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", " 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", " const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-3.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-3.1.1.min.js\"];\n", " const css_urls = [];\n", "\n", " const inline_js = [ function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", "function(Bokeh) {\n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " if (root.Bokeh !== undefined || force === true) {\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(\"aaf012cd-698d-4332-9411-c846424fa65f\")).parents('.cell').data().cell;\n", " cell.output_area.append_execute_result(NB_LOAD_WARNING)\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": "(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 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(\"aaf012cd-698d-4332-9411-c846424fa65f\");\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 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 const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-3.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.1.1.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-3.1.1.min.js\"];\n const css_urls = [];\n\n const inline_js = [ function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\nfunction(Bokeh) {\n }\n ];\n\n function run_inline_js() {\n if (root.Bokeh !== undefined || force === true) {\n for (let i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }\nif (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(\"aaf012cd-698d-4332-9411-c846424fa65f\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\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 math\n", "\n", "from bokeh.io import output_notebook, show\n", "from bokeh.plotting import figure\n", "\n", "\n", "output_notebook()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import math\n", "\n", "from bokeh.models import Ellipse, GraphRenderer\n", "from bokeh.palettes import Spectral8\n", "from bokeh.plotting import figure\n", "\n", "\n", "N = 8\n", "node_indices = list(range(N))\n", "\n", "plot = figure(\n", " title=\"Graph Layout Demonstration\",\n", " x_range=(-1.1, 1.1),\n", " y_range=(-1.1, 1.1),\n", " tools=\"\",\n", " toolbar_location=None,\n", ")\n", "\n", "graph = GraphRenderer()\n", "\n", "graph.node_renderer.glyph = Ellipse(\n", " height=0.1, width=0.2, fill_color=\"fill_color\"\n", ")\n", "graph.node_renderer.data_source.data = dict(\n", " index=node_indices, fill_color=Spectral8\n", ")\n", "\n", "graph.edge_renderer.data_source.data = dict(start=[0] * N, end=node_indices)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Durch Ausführen des obigen Code-Snippets wird kein Diagramm gerendert, da wir nicht angegeben haben, wie das Diagramm im 2D-Raum angeordnet werden soll. Wie das geht, erfahren Sie im folgenden Abschnitt." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Layout Providers\n", "\n", "Bokeh verwendet ein separates `LayoutProvider`-Modell, um die Koordinaten eines Graphen im kartesischen Raum anzugeben. Derzeit ist das `StaticLayoutProvider`-Modell der einzige integrierte Provider, der ein Wörterbuch mit (x,y)-Koordinaten für die Knoten enthält.\n", "\n", "In unserem Beispiel wird dem obigen Codeausschnitt ein Provider hinzugefügt mit:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " const docs_json = {\"4a47be6a-4f74-44bb-aab3-882bb184467f\":{\"version\":\"3.1.1\",\"title\":\"Bokeh Application\",\"defs\":[],\"roots\":[{\"type\":\"object\",\"name\":\"Figure\",\"id\":\"p1073\",\"attributes\":{\"x_range\":{\"type\":\"object\",\"name\":\"Range1d\",\"id\":\"p1083\",\"attributes\":{\"start\":-1.1,\"end\":1.1}},\"y_range\":{\"type\":\"object\",\"name\":\"Range1d\",\"id\":\"p1085\",\"attributes\":{\"start\":-1.1,\"end\":1.1}},\"x_scale\":{\"type\":\"object\",\"name\":\"LinearScale\",\"id\":\"p1087\"},\"y_scale\":{\"type\":\"object\",\"name\":\"LinearScale\",\"id\":\"p1089\"},\"title\":{\"type\":\"object\",\"name\":\"Title\",\"id\":\"p1076\",\"attributes\":{\"text\":\"Graph Layout Demonstration\"}},\"renderers\":[{\"type\":\"object\",\"name\":\"GraphRenderer\",\"id\":\"p1106\",\"attributes\":{\"layout_provider\":{\"type\":\"object\",\"name\":\"StaticLayoutProvider\",\"id\":\"p1152\",\"attributes\":{\"graph_layout\":{\"type\":\"map\",\"entries\":[[0,[1.0,0.0]],[1,[0.7071067811865476,0.7071067811865475]],[2,[6.123233995736766e-17,1.0]],[3,[-0.7071067811865475,0.7071067811865476]],[4,[-1.0,1.2246467991473532e-16]],[5,[-0.7071067811865477,-0.7071067811865475]],[6,[-1.8369701987210297e-16,-1.0]],[7,[0.7071067811865474,-0.7071067811865477]]]}}},\"node_renderer\":{\"type\":\"object\",\"name\":\"GlyphRenderer\",\"id\":\"p1120\",\"attributes\":{\"data_source\":{\"type\":\"object\",\"name\":\"ColumnDataSource\",\"id\":\"p1117\",\"attributes\":{\"selected\":{\"type\":\"object\",\"name\":\"Selection\",\"id\":\"p1118\",\"attributes\":{\"indices\":[],\"line_indices\":[]}},\"selection_policy\":{\"type\":\"object\",\"name\":\"UnionRenderers\",\"id\":\"p1119\"},\"data\":{\"type\":\"map\",\"entries\":[[\"index\",[0,1,2,3,4,5,6,7]],[\"color\",[\"#3288bd\",\"#66c2a5\",\"#abdda4\",\"#e6f598\",\"#fee08b\",\"#fdae61\",\"#f46d43\",\"#d53e4f\"]]]}}},\"view\":{\"type\":\"object\",\"name\":\"CDSView\",\"id\":\"p1121\",\"attributes\":{\"filter\":{\"type\":\"object\",\"name\":\"AllIndices\",\"id\":\"p1122\"}}},\"glyph\":{\"type\":\"object\",\"name\":\"Ellipse\",\"id\":\"p1137\",\"attributes\":{\"width\":{\"type\":\"value\",\"value\":0.2},\"height\":{\"type\":\"value\",\"value\":0.1},\"fill_color\":{\"type\":\"field\",\"field\":\"color\"}}}}},\"edge_renderer\":{\"type\":\"object\",\"name\":\"GlyphRenderer\",\"id\":\"p1112\",\"attributes\":{\"data_source\":{\"type\":\"object\",\"name\":\"ColumnDataSource\",\"id\":\"p1109\",\"attributes\":{\"selected\":{\"type\":\"object\",\"name\":\"Selection\",\"id\":\"p1110\",\"attributes\":{\"indices\":[],\"line_indices\":[]}},\"selection_policy\":{\"type\":\"object\",\"name\":\"UnionRenderers\",\"id\":\"p1111\"},\"data\":{\"type\":\"map\",\"entries\":[[\"start\",[0,0,0,0,0,0,0,0]],[\"end\",[0,1,2,3,4,5,6,7]]]}}},\"view\":{\"type\":\"object\",\"name\":\"CDSView\",\"id\":\"p1113\",\"attributes\":{\"filter\":{\"type\":\"object\",\"name\":\"AllIndices\",\"id\":\"p1114\"}}},\"glyph\":{\"type\":\"object\",\"name\":\"MultiLine\",\"id\":\"p1108\"}}},\"selection_policy\":{\"type\":\"object\",\"name\":\"NodesOnly\",\"id\":\"p1115\"},\"inspection_policy\":{\"type\":\"object\",\"name\":\"NodesOnly\",\"id\":\"p1107\"}}}],\"toolbar\":{\"type\":\"object\",\"name\":\"Toolbar\",\"id\":\"p1081\"},\"toolbar_location\":null,\"left\":[{\"type\":\"object\",\"name\":\"LinearAxis\",\"id\":\"p1098\",\"attributes\":{\"ticker\":{\"type\":\"object\",\"name\":\"BasicTicker\",\"id\":\"p1101\",\"attributes\":{\"mantissas\":[1,2,5]}},\"formatter\":{\"type\":\"object\",\"name\":\"BasicTickFormatter\",\"id\":\"p1100\"},\"major_label_policy\":{\"type\":\"object\",\"name\":\"AllLabels\",\"id\":\"p1099\"}}}],\"below\":[{\"type\":\"object\",\"name\":\"LinearAxis\",\"id\":\"p1091\",\"attributes\":{\"ticker\":{\"type\":\"object\",\"name\":\"BasicTicker\",\"id\":\"p1094\",\"attributes\":{\"mantissas\":[1,2,5]}},\"formatter\":{\"type\":\"object\",\"name\":\"BasicTickFormatter\",\"id\":\"p1093\"},\"major_label_policy\":{\"type\":\"object\",\"name\":\"AllLabels\",\"id\":\"p1092\"}}}],\"center\":[{\"type\":\"object\",\"name\":\"Grid\",\"id\":\"p1097\",\"attributes\":{\"axis\":{\"id\":\"p1091\"}}},{\"type\":\"object\",\"name\":\"Grid\",\"id\":\"p1104\",\"attributes\":{\"dimension\":1,\"axis\":{\"id\":\"p1098\"}}}]}}],\"callbacks\":{\"type\":\"map\"}}};\n", " const render_items = [{\"docid\":\"4a47be6a-4f74-44bb-aab3-882bb184467f\",\"roots\":{\"p1073\":\"ffb9c06a-948b-46a3-8b96-99d52e4cc6d8\"},\"root_ids\":[\"p1073\"]}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\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": "p1073" } }, "output_type": "display_data" } ], "source": [ "from bokeh.models import Ellipse, GraphRenderer, StaticLayoutProvider\n", "from bokeh.palettes import Spectral8\n", "\n", "\n", "N = 8\n", "node_indices = list(range(N))\n", "\n", "p = figure(\n", " title=\"Graph Layout Demonstration\",\n", " x_range=(-1.1, 1.1),\n", " y_range=(-1.1, 1.1),\n", " tools=\"\",\n", " toolbar_location=None,\n", ")\n", "\n", "graph = GraphRenderer()\n", "\n", "graph.node_renderer.data_source.add(node_indices, \"index\")\n", "graph.node_renderer.data_source.add(Spectral8, \"color\")\n", "graph.node_renderer.glyph = Ellipse(height=0.1, width=0.2, fill_color=\"color\")\n", "\n", "graph.edge_renderer.data_source.data = dict(start=[0] * N, end=node_indices)\n", "\n", "### start of layout code\n", "circ = [i * 2 * math.pi / 8 for i in node_indices]\n", "x = [math.cos(i) for i in circ]\n", "y = [math.sin(i) for i in circ]\n", "\n", "graph_layout = dict(zip(node_indices, zip(x, y)))\n", "graph.layout_provider = StaticLayoutProvider(graph_layout=graph_layout)\n", "\n", "p.renderers.append(graph)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Explizite Pfade \n", "\n", "Standardmäßig werden vom `StaticLayout`-Provider gerade Pfade zwischen den angegebenen Knoten gezeichnet. Um explizite Kantenpfade bereitzustellen, können jedoch auch Listen mit Pfaden für die `edge_renderer` angegeben werden mit `\"xs\"` und `\"ys\"`-Spalten der `ColumnDataSource`. Beachtet jedoch, dass einerseits diese Pfade in derselben Reihenfolge sein sollten wie die Punkte `\"start\"` und `\"end\"` und andererseits, dass es keine Validierung gibt, die mit den Knotenpositionen übereinstimmt. Ihr solltet daher sehr vorsichtig sein, wenn ihr explizite Pfade festlegt.\n", "\n", "Im folgenden Beispiel erweitern wir das obige Beispiel um quadratische Bezierpfade zwischen den Knoten:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " const docs_json = {\"893be2ab-5f82-4796-be4c-9add82a77829\":{\"version\":\"3.1.1\",\"title\":\"Bokeh Application\",\"defs\":[],\"roots\":[{\"type\":\"object\",\"name\":\"Figure\",\"id\":\"p1372\",\"attributes\":{\"x_range\":{\"type\":\"object\",\"name\":\"Range1d\",\"id\":\"p1382\",\"attributes\":{\"start\":-1.1,\"end\":1.1}},\"y_range\":{\"type\":\"object\",\"name\":\"Range1d\",\"id\":\"p1384\",\"attributes\":{\"start\":-1.1,\"end\":1.1}},\"x_scale\":{\"type\":\"object\",\"name\":\"LinearScale\",\"id\":\"p1386\"},\"y_scale\":{\"type\":\"object\",\"name\":\"LinearScale\",\"id\":\"p1388\"},\"title\":{\"type\":\"object\",\"name\":\"Title\",\"id\":\"p1375\",\"attributes\":{\"text\":\"Graph Layout Demonstration\"}},\"renderers\":[{\"type\":\"object\",\"name\":\"GraphRenderer\",\"id\":\"p1405\",\"attributes\":{\"layout_provider\":{\"type\":\"object\",\"name\":\"StaticLayoutProvider\",\"id\":\"p1451\",\"attributes\":{\"graph_layout\":{\"type\":\"map\",\"entries\":[[0,[1.0,0.0]],[1,[0.7071067811865476,0.7071067811865475]],[2,[6.123233995736766e-17,1.0]],[3,[-0.7071067811865475,0.7071067811865476]],[4,[-1.0,1.2246467991473532e-16]],[5,[-0.7071067811865477,-0.7071067811865475]],[6,[-1.8369701987210297e-16,-1.0]],[7,[0.7071067811865474,-0.7071067811865477]]]}}},\"node_renderer\":{\"type\":\"object\",\"name\":\"GlyphRenderer\",\"id\":\"p1419\",\"attributes\":{\"data_source\":{\"type\":\"object\",\"name\":\"ColumnDataSource\",\"id\":\"p1416\",\"attributes\":{\"selected\":{\"type\":\"object\",\"name\":\"Selection\",\"id\":\"p1417\",\"attributes\":{\"indices\":[],\"line_indices\":[]}},\"selection_policy\":{\"type\":\"object\",\"name\":\"UnionRenderers\",\"id\":\"p1418\"},\"data\":{\"type\":\"map\",\"entries\":[[\"index\",[0,1,2,3,4,5,6,7]],[\"color\",[\"#3288bd\",\"#66c2a5\",\"#abdda4\",\"#e6f598\",\"#fee08b\",\"#fdae61\",\"#f46d43\",\"#d53e4f\"]]]}}},\"view\":{\"type\":\"object\",\"name\":\"CDSView\",\"id\":\"p1420\",\"attributes\":{\"filter\":{\"type\":\"object\",\"name\":\"AllIndices\",\"id\":\"p1421\"}}},\"glyph\":{\"type\":\"object\",\"name\":\"Ellipse\",\"id\":\"p1436\",\"attributes\":{\"width\":{\"type\":\"value\",\"value\":0.2},\"height\":{\"type\":\"value\",\"value\":0.1},\"fill_color\":{\"type\":\"field\",\"field\":\"color\"}}}}},\"edge_renderer\":{\"type\":\"object\",\"name\":\"GlyphRenderer\",\"id\":\"p1411\",\"attributes\":{\"data_source\":{\"type\":\"object\",\"name\":\"ColumnDataSource\",\"id\":\"p1408\",\"attributes\":{\"selected\":{\"type\":\"object\",\"name\":\"Selection\",\"id\":\"p1409\",\"attributes\":{\"indices\":[],\"line_indices\":[]}},\"selection_policy\":{\"type\":\"object\",\"name\":\"UnionRenderers\",\"id\":\"p1410\"},\"data\":{\"type\":\"map\",\"entries\":[[\"start\",[0,0,0,0,0,0,0,0]],[\"end\",[0,1,2,3,4,5,6,7]],[\"xs\",[[1.0,0.9802,0.9607999999999999,0.9418,0.9232,0.9049999999999999,0.8872,0.8697999999999999,0.8528,0.8362,0.8200000000000001,0.8042,0.7888,0.7738,0.7591999999999999,0.7449999999999999,0.7311999999999999,0.7178,0.7048000000000001,0.6922000000000001,0.6800000000000002,0.6682000000000001,0.6568,0.6457999999999999,0.6352,0.625,0.6152,0.6057999999999999,0.5968,0.5882,0.58,0.5721999999999999,0.5648,0.5578,0.5511999999999999,0.545,0.5392,0.5338,0.5288,0.5242,0.52,0.5162,0.5128,0.5098,0.5072000000000001,0.5050000000000001,0.5032000000000001,0.5018,0.5008,0.5002,0.5,0.5002,0.5008,0.5018,0.5032,0.505,0.5072,0.5098,0.5128,0.5162,0.52,0.5242,0.5288,0.5338,0.5392,0.545,0.5512,0.5578000000000001,0.5648000000000001,0.5721999999999999,0.58,0.5882000000000001,0.5968,0.6057999999999999,0.6152,0.625,0.6352,0.6457999999999999,0.6568,0.6682000000000001,0.6800000000000002,0.6922000000000001,0.7047999999999999,0.7178,0.7311999999999999,0.7449999999999999,0.7591999999999999,0.7738,0.7888,0.8042,0.8200000000000001,0.8362,0.8528,0.8698000000000001,0.8872,0.905,0.9232,0.9418,0.9607999999999999,0.9802],[1.0,0.9801707106781187,0.9606828427124745,0.9415363961030678,0.9227313708498984,0.9042677669529663,0.8861455844122715,0.8683648232278139,0.850925483399594,0.8338275649276111,0.8170710678118656,0.8006559920523573,0.7845823376490862,0.7688501046020527,0.7534592929112562,0.7384099025766973,0.7237019335983755,0.7093353859762912,0.6953102597104442,0.6816265548008344,0.668284271247462,0.6552834090503269,0.6426239682094289,0.6303059487247683,0.6183293505963452,0.6066941738241592,0.5954004184082106,0.5844480843484993,0.5738371716450253,0.5635676802977887,0.5536396103067892,0.5440529616720271,0.5348077343935024,0.5259039284712149,0.5173415439051647,0.5091205806953522,0.5012410388417766,0.49370291834443836,0.4865062192033375,0.47965094141847386,0.47313708498984763,0.4669646499174587,0.4611336362013071,0.4556440438413927,0.45049587283771564,0.4456891231902759,0.4412237948990735,0.43709988796410837,0.4333174023853806,0.42987633816289006,0.42677669529663687,0.424018473786621,0.4216016736328425,0.41952629483530124,0.41779233739399724,0.4163998013089306,0.4153486865801013,0.4146389932075093,0.4142707211911546,0.4142438705310372,0.4145584412271571,0.41521443327951435,0.41621184668810896,0.4175506814529407,0.41923093757400987,0.4212526150513164,0.42361571388486013,0.42632023407464126,0.4293661756206596,0.43275353852291526,0.4364823227814083,0.4405525283961386,0.44496415536710626,0.4497172036943112,0.4548116733777534,0.46024756441743303,0.46602487681334986,0.472143610565504,0.47860376567389556,0.4854053421385244,0.4925483399593905,0.5000327591364939,0.5078585996698345,0.5160258615594125,0.5245345448052279,0.5333846494072805,0.5425761753655705,0.5521091226800979,0.5619834913508623,0.5721992813778644,0.5827564927611035,0.5936551255005801,0.6048951795962939,0.6164766550482451,0.6283995518564335,0.6406638700208592,0.6532696095415222,0.6662167704184226,0.6795053526515602,0.6931353562409353],[1.0,0.9801,0.9603999999999999,0.9409,0.9216,0.9025,0.8835999999999999,0.8648999999999999,0.8464,0.8281000000000001,0.81,0.7921,0.7744,0.7569,0.7395999999999999,0.7224999999999999,0.7055999999999999,0.6889,0.6724000000000001,0.6561000000000001,0.6400000000000001,0.6241000000000001,0.6084,0.5929,0.5776,0.5625,0.5476,0.5328999999999999,0.5184,0.5041,0.48999999999999994,0.4760999999999999,0.4623999999999999,0.4488999999999999,0.4355999999999999,0.42250000000000004,0.4096,0.39690000000000003,0.3844,0.3721,0.36,0.3481000000000001,0.3364000000000001,0.3249000000000001,0.31360000000000005,0.30250000000000005,0.2916,0.28090000000000004,0.27040000000000003,0.2601,0.25,0.2401,0.23040000000000002,0.2209,0.21159999999999998,0.20249999999999999,0.1936,0.18490000000000006,0.17640000000000006,0.16810000000000005,0.16000000000000006,0.15210000000000004,0.14440000000000003,0.13690000000000002,0.12960000000000002,0.12250000000000001,0.11560000000000001,0.1089,0.10239999999999999,0.09610000000000006,0.09000000000000005,0.08410000000000005,0.07840000000000004,0.07290000000000003,0.06760000000000004,0.06250000000000003,0.05760000000000003,0.05290000000000002,0.048400000000000026,0.04410000000000003,0.04000000000000002,0.03610000000000002,0.03240000000000006,0.028900000000000054,0.025600000000000053,0.02250000000000005,0.019600000000000048,0.016900000000000047,0.014400000000000046,0.012100000000000046,0.010000000000000045,0.008100000000000045,0.006400000000000045,0.004900000000000046,0.0036000000000000606,0.0025000000000000595,0.0016000000000000593,0.0009000000000000592,0.00040000000000005954,0.00010000000000006019],[1.0,0.9800292893218813,0.9601171572875253,0.9402636038969321,0.9204686291501015,0.9007322330470336,0.8810544155877283,0.8614351767721858,0.8418745166004061,0.822372435072389,0.8029289321881345,0.7835440079476428,0.7642176623509137,0.7449498953979473,0.7257407070887436,0.7065900974233026,0.6874980664016243,0.6684646140237087,0.649489740289556,0.6305734451991658,0.6117157287525382,0.5929165909496734,0.5741760317905712,0.5554940512752317,0.5368706494036549,0.5183058261758408,0.4997995815917894,0.4813519156515006,0.46296282835497465,0.44463231970221134,0.42636038969321066,0.4081470383279727,0.38999226560649747,0.3718960715287849,0.35385845609483496,0.335879419304648,0.3179589611582235,0.3000970816555617,0.28229378079666256,0.2645490585815261,0.24686291501015237,0.22923535008254148,0.21166636379869314,0.19415595615860748,0.17670412716228445,0.15931087680972417,0.14197620510092657,0.12470011203589171,0.10748259761461951,0.09032366183710996,0.07322330470336313,0.05618152621337899,0.03919832636715753,0.022273705164698787,0.0054076626060027,-0.011399801308930674,-0.028148686580101362,-0.0448389932075092,-0.061470721191154515,-0.07804387053103712,-0.09455844122715706,-0.11101443327951427,-0.12741184668810887,-0.1437506814529407,-0.16003093757400988,-0.17625261505131634,-0.19241571388486012,-0.20852023407464124,-0.22456617562065964,-0.24055353852291517,-0.2564823227814082,-0.27235252839613855,-0.28816415536710616,-0.3039172036943111,-0.31961167337775337,-0.335247564417433,-0.35082487681334984,-0.36634361056550396,-0.3818037656738955,-0.3972053421385244,-0.4125483399593905,-0.42783275913649393,-0.4430585996698344,-0.4582258615594125,-0.4733345448052278,-0.4883846494072805,-0.5033761753655704,-0.5183091226800978,-0.5331834913508624,-0.5479992813778642,-0.5627564927611035,-0.57745512550058,-0.5920951795962939,-0.606676655048245,-0.6211995518564333,-0.635663870020859,-0.650069609541522,-0.6644167704184225,-0.6787053526515602,-0.6929353562409352],[1.0,0.98,0.96,0.94,0.9199999999999999,0.9,0.8799999999999999,0.8599999999999999,0.8400000000000001,0.8200000000000001,0.8,0.78,0.76,0.74,0.72,0.7,0.6799999999999999,0.6599999999999999,0.6400000000000001,0.6200000000000001,0.6000000000000001,0.5800000000000001,0.56,0.54,0.52,0.5,0.48,0.4599999999999999,0.43999999999999995,0.42,0.3999999999999999,0.3799999999999999,0.35999999999999993,0.3399999999999999,0.31999999999999984,0.30000000000000004,0.28,0.26,0.24000000000000002,0.21999999999999997,0.19999999999999996,0.1800000000000001,0.16000000000000011,0.1400000000000001,0.12000000000000005,0.10000000000000003,0.08000000000000002,0.06000000000000005,0.040000000000000036,0.020000000000000018,0.0,-0.020000000000000018,-0.040000000000000036,-0.06000000000000005,-0.08000000000000007,-0.10000000000000009,-0.12000000000000008,-0.13999999999999993,-0.15999999999999995,-0.17999999999999994,-0.19999999999999996,-0.21999999999999997,-0.24000000000000002,-0.26,-0.28,-0.30000000000000004,-0.32000000000000006,-0.3400000000000001,-0.3600000000000001,-0.3799999999999999,-0.3999999999999999,-0.42,-0.43999999999999995,-0.4599999999999999,-0.48,-0.5,-0.52,-0.54,-0.56,-0.5800000000000001,-0.6000000000000001,-0.6200000000000001,-0.6399999999999999,-0.6599999999999999,-0.6799999999999999,-0.7,-0.72,-0.74,-0.76,-0.78,-0.8,-0.8200000000000001,-0.8400000000000001,-0.8600000000000001,-0.8799999999999999,-0.8999999999999999,-0.9199999999999999,-0.94,-0.96,-0.98],[1.0,0.9800292893218813,0.9601171572875253,0.9402636038969321,0.9204686291501015,0.9007322330470336,0.8810544155877283,0.8614351767721858,0.8418745166004061,0.822372435072389,0.8029289321881345,0.7835440079476428,0.7642176623509137,0.7449498953979473,0.7257407070887436,0.7065900974233026,0.6874980664016243,0.6684646140237087,0.649489740289556,0.6305734451991658,0.6117157287525382,0.5929165909496733,0.5741760317905712,0.5554940512752317,0.5368706494036548,0.5183058261758408,0.49979958159178933,0.4813519156515006,0.4629628283549746,0.44463231970221134,0.42636038969321066,0.40814703832797267,0.38999226560649747,0.37189607152878484,0.35385845609483496,0.33587941930464793,0.3179589611582234,0.30009708165556165,0.28229378079666256,0.2645490585815261,0.24686291501015234,0.22923535008254142,0.21166636379869308,0.19415595615860742,0.17670412716228442,0.15931087680972414,0.14197620510092654,0.12470011203589165,0.10748259761461945,0.09032366183710991,0.07322330470336308,0.056181526213378935,0.03919832636715748,0.022273705164698704,0.0054076626060026445,-0.011399801308930757,-0.028148686580101417,-0.04483899320750928,-0.0614707211911546,-0.0780438705310372,-0.09455844122715712,-0.11101443327951438,-0.12741184668810893,-0.14375068145294082,-0.16003093757400993,-0.17625261505131645,-0.19241571388486023,-0.20852023407464135,-0.22456617562065975,-0.24055353852291528,-0.2564823227814083,-0.27235252839613866,-0.28816415536710627,-0.3039172036943112,-0.3196116733777535,-0.3352475644174331,-0.35082487681334995,-0.36634361056550413,-0.3818037656738956,-0.3972053421385245,-0.4125483399593906,-0.42783275913649405,-0.4430585996698345,-0.45822586155941264,-0.47333454480522796,-0.4883846494072806,-0.5033761753655707,-0.5183091226800979,-0.5331834913508625,-0.5479992813778645,-0.5627564927611036,-0.5774551255005802,-0.592095179596294,-0.6066766550482452,-0.6211995518564335,-0.6356638700208592,-0.6500696095415223,-0.6644167704184226,-0.6787053526515604,-0.6929353562409354],[1.0,0.9801,0.9603999999999999,0.9409,0.9216,0.9025,0.8835999999999999,0.8648999999999999,0.8464,0.8281000000000001,0.81,0.7921,0.7744,0.7569,0.7395999999999999,0.7224999999999999,0.7055999999999999,0.6889,0.6724000000000001,0.6561000000000001,0.6400000000000001,0.6241000000000001,0.6084,0.5929,0.5776,0.5625,0.5476,0.5328999999999999,0.5184,0.5041,0.48999999999999994,0.4760999999999999,0.4623999999999999,0.4488999999999999,0.4355999999999999,0.42250000000000004,0.4096,0.39690000000000003,0.3844,0.37209999999999993,0.35999999999999993,0.3481,0.33640000000000003,0.3249,0.3136,0.3025,0.29159999999999997,0.2809,0.2704,0.26009999999999994,0.24999999999999994,0.24009999999999992,0.23039999999999994,0.22089999999999993,0.2115999999999999,0.2024999999999999,0.1935999999999999,0.18489999999999998,0.17639999999999997,0.16809999999999997,0.15999999999999998,0.15209999999999996,0.14439999999999992,0.1368999999999999,0.1295999999999999,0.1224999999999999,0.1155999999999999,0.10889999999999989,0.10239999999999988,0.09609999999999995,0.08999999999999994,0.08409999999999992,0.07839999999999991,0.07289999999999991,0.06759999999999991,0.062499999999999896,0.057599999999999894,0.05289999999999988,0.04839999999999988,0.04409999999999987,0.03999999999999986,0.03609999999999986,0.032399999999999894,0.028899999999999888,0.025599999999999883,0.022499999999999874,0.019599999999999868,0.016899999999999863,0.014399999999999857,0.012099999999999852,0.009999999999999846,0.008099999999999842,0.006399999999999838,0.004899999999999834,0.003599999999999844,0.0024999999999998387,0.0015999999999998335,0.0008999999999998288,0.0003999999999998243,9.999999999982014e-05],[1.0,0.9801707106781187,0.9606828427124745,0.9415363961030678,0.9227313708498984,0.9042677669529663,0.8861455844122715,0.8683648232278139,0.850925483399594,0.8338275649276111,0.8170710678118656,0.8006559920523573,0.7845823376490862,0.7688501046020527,0.7534592929112562,0.7384099025766973,0.7237019335983755,0.7093353859762912,0.6953102597104442,0.6816265548008344,0.668284271247462,0.6552834090503268,0.6426239682094289,0.6303059487247683,0.6183293505963451,0.6066941738241592,0.5954004184082106,0.5844480843484993,0.5738371716450252,0.5635676802977886,0.5536396103067892,0.5440529616720271,0.5348077343935024,0.5259039284712149,0.5173415439051647,0.509120580695352,0.5012410388417765,0.49370291834443836,0.4865062192033375,0.47965094141847386,0.4731370849898476,0.4669646499174587,0.46113363620130704,0.4556440438413927,0.45049587283771564,0.44568912319027587,0.4412237948990735,0.43709988796410837,0.4333174023853805,0.42987633816289,0.42677669529663687,0.42401847378662094,0.4216016736328424,0.41952629483530113,0.4177923373939972,0.41639980130893056,0.41534868658010127,0.41463899320750924,0.41427072119115455,0.4142438705310372,0.41455844122715707,0.4152144332795143,0.41621184668810884,0.4175506814529406,0.4192309375740098,0.4212526150513163,0.42361571388486,0.42632023407464115,0.4293661756206595,0.43275353852291515,0.4364823227814082,0.4405525283961385,0.44496415536710615,0.44971720369431106,0.4548116733777533,0.46024756441743286,0.46602487681334975,0.4721436105655039,0.47860376567389545,0.48540534213852427,0.49254833995939035,0.5000327591364938,0.5078585996698344,0.5160258615594124,0.5245345448052278,0.5333846494072804,0.5425761753655703,0.5521091226800977,0.5619834913508622,0.5721992813778641,0.5827564927611034,0.5936551255005799,0.6048951795962937,0.6164766550482449,0.6283995518564333,0.640663870020859,0.653269609541522,0.6662167704184224,0.67950535265156,0.693135356240935]]],[\"ys\",[[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],[0.0,7.071067811865475e-05,0.000282842712474619,0.0006363961030678927,0.001131370849898476,0.001767766952966369,0.0025455844122715707,0.0034648232278140833,0.004525483399593904,0.005727564927611034,0.007071067811865476,0.008555992052357225,0.010182337649086283,0.011950104602052654,0.013859292911256333,0.01590990257669732,0.018101933598375617,0.020435385976291225,0.022910259710444136,0.025526554800834364,0.028284271247461905,0.031183409050326738,0.0342239682094289,0.037405948724768365,0.04072935059634513,0.044194173824159216,0.047800418408210615,0.051548084348499314,0.05543717164502533,0.059467680297788636,0.06363961030678927,0.06795296167202722,0.07240773439350247,0.07700392847121502,0.0817415439051649,0.08662058069535206,0.09164103884177655,0.09680291834443834,0.10210621920333746,0.10755094141847388,0.11313708498984762,0.11886464991745861,0.12473363620130695,0.1307440438413926,0.1368958728377156,0.14318912319027588,0.14962379489907346,0.15619988796410833,0.16291740238538052,0.16977633816289003,0.17677669529663687,0.183918473786621,0.19120167363284246,0.1986262948353012,0.20619233739399725,0.21389980130893063,0.22174868658010133,0.22973899320750923,0.23787072119115454,0.24614387053103715,0.2545584412271571,0.2631144332795143,0.27181184668810887,0.2806506814529407,0.2896309375740099,0.29875261505131634,0.3080157138848601,0.3174202340746412,0.3269661756206596,0.3366535385229152,0.3464823227814082,0.35645252839613856,0.3665641553671062,0.3768172036943111,0.38721167337775336,0.397747564417433,0.4084248768133498,0.41924361056550397,0.4302037656738955,0.44130534213852435,0.4525483399593905,0.4639327591364939,0.47545859966983445,0.4871258615594125,0.4989345448052278,0.5108846494072805,0.5229761753655704,0.5352091226800978,0.5475834913508624,0.5600992813778642,0.5727564927611035,0.58555512550058,0.5984951795962938,0.611576655048245,0.6247995518564333,0.6381638700208591,0.6516696095415221,0.6653167704184225,0.6791053526515601,0.6930353562409352],[0.0,0.0001,0.0004,0.0009,0.0016,0.0025000000000000005,0.0036,0.004900000000000001,0.0064,0.0081,0.010000000000000002,0.0121,0.0144,0.016900000000000002,0.019600000000000003,0.0225,0.0256,0.028900000000000006,0.0324,0.0361,0.04000000000000001,0.04409999999999999,0.0484,0.0529,0.0576,0.0625,0.06760000000000001,0.0729,0.07840000000000001,0.0841,0.09,0.0961,0.1024,0.10890000000000001,0.11560000000000002,0.12249999999999998,0.1296,0.1369,0.1444,0.1521,0.16000000000000003,0.16809999999999997,0.17639999999999997,0.18489999999999998,0.1936,0.2025,0.2116,0.22089999999999999,0.2304,0.24009999999999998,0.25,0.2601,0.27040000000000003,0.28090000000000004,0.2916,0.30250000000000005,0.31360000000000005,0.32489999999999997,0.3364,0.34809999999999997,0.36,0.3721,0.3844,0.39690000000000003,0.4096,0.42250000000000004,0.43560000000000004,0.4489000000000001,0.4624000000000001,0.4760999999999999,0.48999999999999994,0.5041,0.5184,0.5328999999999999,0.5476,0.5625,0.5776,0.5929,0.6084,0.6241000000000001,0.6400000000000001,0.6561000000000001,0.6723999999999999,0.6889,0.7055999999999999,0.7224999999999999,0.7395999999999999,0.7569,0.7744,0.7921,0.81,0.8281000000000001,0.8464,0.8649000000000001,0.8835999999999999,0.9025,0.9216,0.9409,0.9603999999999999,0.9801],[0.0,7.071067811865475e-05,0.000282842712474619,0.0006363961030678928,0.001131370849898476,0.0017677669529663693,0.002545584412271571,0.0034648232278140837,0.004525483399593904,0.005727564927611035,0.007071067811865477,0.008555992052357225,0.010182337649086284,0.011950104602052656,0.013859292911256335,0.01590990257669732,0.018101933598375617,0.02043538597629123,0.02291025971044414,0.025526554800834367,0.02828427124746191,0.031183409050326745,0.0342239682094289,0.03740594872476837,0.04072935059634514,0.04419417382415922,0.04780041840821062,0.05154808434849932,0.05543717164502534,0.05946768029778865,0.06363961030678927,0.06795296167202723,0.07240773439350247,0.07700392847121504,0.08174154390516492,0.08662058069535207,0.09164103884177656,0.09680291834443835,0.10210621920333747,0.10755094141847389,0.11313708498984763,0.11886464991745863,0.12473363620130698,0.13074404384139263,0.1368958728377156,0.14318912319027588,0.1496237948990735,0.15619988796410836,0.16291740238538055,0.16977633816289006,0.1767766952966369,0.18391847378662102,0.1912016736328425,0.19862629483530125,0.20619233739399728,0.2138998013089307,0.22174868658010136,0.2297389932075093,0.2378707211911546,0.24614387053103717,0.2545584412271571,0.26311443327951434,0.2718118466881089,0.28065068145294075,0.2896309375740099,0.2987526150513164,0.30801571388486015,0.31742023407464126,0.32696617562065966,0.33665353852291524,0.3464823227814083,0.3564525283961386,0.36656415536710624,0.37681720369431115,0.3872116733777534,0.39774756441743303,0.4084248768133499,0.419243610565504,0.43020376567389557,0.4413053421385244,0.45254833995939053,0.46393275913649396,0.4754585996698345,0.48712586155941257,0.4989345448052279,0.5108846494072805,0.5229761753655705,0.5352091226800979,0.5475834913508624,0.5600992813778644,0.5727564927611035,0.5855551255005801,0.598495179596294,0.6115766550482451,0.6247995518564334,0.6381638700208592,0.6516696095415222,0.6653167704184226,0.6791053526515602,0.6930353562409353],[0.0,1.2246467991473533e-20,4.898587196589413e-20,1.1021821192326178e-19,1.9594348786357652e-19,3.0616169978683835e-19,4.408728476930471e-19,6.000769315822031e-19,7.837739514543061e-19,9.91963907309356e-19,1.2246467991473534e-18,1.4818226269682973e-18,1.7634913907721884e-18,2.069653090559027e-18,2.4003077263288125e-18,2.7554552980815448e-18,3.1350958058172244e-18,3.539229249535851e-18,3.967855629237424e-18,4.4209749449219455e-18,4.8985871965894135e-18,5.400692384239827e-18,5.927290507873189e-18,6.4783815674894986e-18,7.053965563088754e-18,7.654042494670958e-18,8.278612362236109e-18,8.927675165784206e-18,9.60123090531525e-18,1.029927958082924e-17,1.1021821192326179e-17,1.1768855739806065e-17,1.2540383223268897e-17,1.3336403642714678e-17,1.4156916998143405e-17,1.5001923289555074e-17,1.5871422516949696e-17,1.6765414680327266e-17,1.7683899779687782e-17,1.8626877815031244e-17,1.9594348786357654e-17,2.0586312693667004e-17,2.1602769536959306e-17,2.2643719316234557e-17,2.3709162031492757e-17,2.4799097682733903e-17,2.5913526269957994e-17,2.7052447793165032e-17,2.8215862252355015e-17,2.940376964752795e-17,3.061616997868383e-17,3.1853063245822657e-17,3.3114449448944435e-17,3.440032858804916e-17,3.571070066313682e-17,3.704556567420744e-17,3.8404923621261e-17,3.97887745042975e-17,4.119711832331696e-17,4.262995507831936e-17,4.4087284769304716e-17,4.556910739627301e-17,4.707542295922426e-17,4.860623145815845e-17,5.016153289307559e-17,5.1741327263975676e-17,5.3345614570858713e-17,5.4974394813724697e-17,5.662766799257362e-17,5.830543410740548e-17,6.00076931582203e-17,6.173444514501807e-17,6.348569006779878e-17,6.526142792656245e-17,6.706165872130906e-17,6.888638245203862e-17,7.073559911875113e-17,7.260930872144657e-17,7.450751126012497e-17,7.643020673478633e-17,7.837739514543062e-17,8.034907649205786e-17,8.234525077466802e-17,8.436591799326115e-17,8.641107814783723e-17,8.848073123839626e-17,9.057487726493823e-17,9.269351622746317e-17,9.483664812597103e-17,9.700427296046185e-17,9.919639073093561e-17,1.0141300143739233e-16,1.0365410507983198e-16,1.059197016582546e-16,1.0820979117266013e-16,1.1052437362304862e-16,1.1286344900942006e-16,1.1522701733177445e-16,1.176150785901118e-16,1.200276327844321e-16],[0.0,-7.071067811865475e-05,-0.000282842712474619,-0.0006363961030678927,-0.001131370849898476,-0.001767766952966369,-0.0025455844122715707,-0.0034648232278140833,-0.004525483399593904,-0.005727564927611034,-0.007071067811865476,-0.008555992052357225,-0.010182337649086283,-0.011950104602052654,-0.013859292911256333,-0.01590990257669732,-0.018101933598375617,-0.020435385976291225,-0.022910259710444136,-0.025526554800834364,-0.028284271247461905,-0.031183409050326738,-0.0342239682094289,-0.037405948724768365,-0.04072935059634513,-0.044194173824159216,-0.047800418408210615,-0.051548084348499314,-0.05543717164502533,-0.059467680297788636,-0.06363961030678927,-0.06795296167202722,-0.07240773439350247,-0.07700392847121502,-0.0817415439051649,-0.08662058069535206,-0.09164103884177655,-0.09680291834443834,-0.10210621920333746,-0.10755094141847388,-0.11313708498984762,-0.11886464991745861,-0.12473363620130695,-0.1307440438413926,-0.1368958728377156,-0.14318912319027588,-0.14962379489907346,-0.15619988796410833,-0.16291740238538052,-0.16977633816289003,-0.17677669529663687,-0.183918473786621,-0.19120167363284246,-0.1986262948353012,-0.20619233739399725,-0.21389980130893063,-0.22174868658010133,-0.22973899320750923,-0.23787072119115454,-0.24614387053103715,-0.2545584412271571,-0.2631144332795143,-0.27181184668810887,-0.2806506814529407,-0.2896309375740099,-0.29875261505131634,-0.3080157138848601,-0.3174202340746412,-0.3269661756206596,-0.3366535385229152,-0.3464823227814082,-0.35645252839613856,-0.3665641553671062,-0.3768172036943111,-0.38721167337775336,-0.397747564417433,-0.4084248768133498,-0.41924361056550397,-0.4302037656738955,-0.44130534213852435,-0.4525483399593905,-0.4639327591364939,-0.47545859966983445,-0.4871258615594125,-0.4989345448052278,-0.5108846494072805,-0.5229761753655704,-0.5352091226800978,-0.5475834913508624,-0.5600992813778642,-0.5727564927611035,-0.58555512550058,-0.5984951795962938,-0.611576655048245,-0.6247995518564333,-0.6381638700208591,-0.6516696095415221,-0.6653167704184225,-0.6791053526515601,-0.6930353562409352],[0.0,-0.0001,-0.0004,-0.0009,-0.0016,-0.0025000000000000005,-0.0036,-0.004900000000000001,-0.0064,-0.0081,-0.010000000000000002,-0.0121,-0.0144,-0.016900000000000002,-0.019600000000000003,-0.0225,-0.0256,-0.028900000000000006,-0.0324,-0.0361,-0.04000000000000001,-0.04409999999999999,-0.0484,-0.0529,-0.0576,-0.0625,-0.06760000000000001,-0.0729,-0.07840000000000001,-0.0841,-0.09,-0.0961,-0.1024,-0.10890000000000001,-0.11560000000000002,-0.12249999999999998,-0.1296,-0.1369,-0.1444,-0.1521,-0.16000000000000003,-0.16809999999999997,-0.17639999999999997,-0.18489999999999998,-0.1936,-0.2025,-0.2116,-0.22089999999999999,-0.2304,-0.24009999999999998,-0.25,-0.2601,-0.27040000000000003,-0.28090000000000004,-0.2916,-0.30250000000000005,-0.31360000000000005,-0.32489999999999997,-0.3364,-0.34809999999999997,-0.36,-0.3721,-0.3844,-0.39690000000000003,-0.4096,-0.42250000000000004,-0.43560000000000004,-0.4489000000000001,-0.4624000000000001,-0.4760999999999999,-0.48999999999999994,-0.5041,-0.5184,-0.5328999999999999,-0.5476,-0.5625,-0.5776,-0.5929,-0.6084,-0.6241000000000001,-0.6400000000000001,-0.6561000000000001,-0.6723999999999999,-0.6889,-0.7055999999999999,-0.7224999999999999,-0.7395999999999999,-0.7569,-0.7744,-0.7921,-0.81,-0.8281000000000001,-0.8464,-0.8649000000000001,-0.8835999999999999,-0.9025,-0.9216,-0.9409,-0.9603999999999999,-0.9801],[0.0,-7.071067811865477e-05,-0.00028284271247461907,-0.0006363961030678929,-0.0011313708498984763,-0.0017677669529663695,-0.0025455844122715715,-0.003464823227814084,-0.004525483399593905,-0.005727564927611036,-0.007071067811865478,-0.008555992052357226,-0.010182337649086286,-0.011950104602052657,-0.013859292911256337,-0.015909902576697322,-0.01810193359837562,-0.020435385976291232,-0.022910259710444143,-0.02552655480083437,-0.028284271247461912,-0.03118340905032675,-0.034223968209428905,-0.03740594872476837,-0.040729350596345144,-0.04419417382415923,-0.04780041840821063,-0.05154808434849933,-0.055437171645025346,-0.05946768029778866,-0.06363961030678929,-0.06795296167202723,-0.07240773439350248,-0.07700392847121505,-0.08174154390516493,-0.08662058069535208,-0.09164103884177657,-0.09680291834443837,-0.10210621920333748,-0.1075509414184739,-0.11313708498984765,-0.11886464991745864,-0.124733636201307,-0.13074404384139265,-0.13689587283771562,-0.1431891231902759,-0.1496237948990735,-0.15619988796410839,-0.16291740238538058,-0.1697763381628901,-0.17677669529663692,-0.18391847378662105,-0.19120167363284252,-0.19862629483530128,-0.2061923373939973,-0.21389980130893071,-0.22174868658010138,-0.22973899320750932,-0.23787072119115463,-0.24614387053103723,-0.25455844122715715,-0.2631144332795144,-0.2718118466881089,-0.2806506814529408,-0.28963093757400993,-0.29875261505131645,-0.3080157138848602,-0.3174202340746413,-0.3269661756206597,-0.3366535385229153,-0.34648232278140834,-0.35645252839613867,-0.3665641553671063,-0.3768172036943112,-0.3872116733777535,-0.3977475644174331,-0.40842487681334994,-0.41924361056550413,-0.4302037656738956,-0.44130534213852446,-0.4525483399593906,-0.463932759136494,-0.47545859966983456,-0.4871258615594127,-0.498934544805228,-0.5108846494072806,-0.5229761753655706,-0.5352091226800979,-0.5475834913508625,-0.5600992813778645,-0.5727564927611036,-0.5855551255005802,-0.598495179596294,-0.6115766550482452,-0.6247995518564335,-0.6381638700208593,-0.6516696095415223,-0.6653167704184226,-0.6791053526515604,-0.6930353562409354]]]]}}},\"view\":{\"type\":\"object\",\"name\":\"CDSView\",\"id\":\"p1412\",\"attributes\":{\"filter\":{\"type\":\"object\",\"name\":\"AllIndices\",\"id\":\"p1413\"}}},\"glyph\":{\"type\":\"object\",\"name\":\"MultiLine\",\"id\":\"p1407\"}}},\"selection_policy\":{\"type\":\"object\",\"name\":\"NodesOnly\",\"id\":\"p1414\"},\"inspection_policy\":{\"type\":\"object\",\"name\":\"NodesOnly\",\"id\":\"p1406\"}}}],\"toolbar\":{\"type\":\"object\",\"name\":\"Toolbar\",\"id\":\"p1380\"},\"toolbar_location\":null,\"left\":[{\"type\":\"object\",\"name\":\"LinearAxis\",\"id\":\"p1397\",\"attributes\":{\"ticker\":{\"type\":\"object\",\"name\":\"BasicTicker\",\"id\":\"p1400\",\"attributes\":{\"mantissas\":[1,2,5]}},\"formatter\":{\"type\":\"object\",\"name\":\"BasicTickFormatter\",\"id\":\"p1399\"},\"major_label_policy\":{\"type\":\"object\",\"name\":\"AllLabels\",\"id\":\"p1398\"}}}],\"below\":[{\"type\":\"object\",\"name\":\"LinearAxis\",\"id\":\"p1390\",\"attributes\":{\"ticker\":{\"type\":\"object\",\"name\":\"BasicTicker\",\"id\":\"p1393\",\"attributes\":{\"mantissas\":[1,2,5]}},\"formatter\":{\"type\":\"object\",\"name\":\"BasicTickFormatter\",\"id\":\"p1392\"},\"major_label_policy\":{\"type\":\"object\",\"name\":\"AllLabels\",\"id\":\"p1391\"}}}],\"center\":[{\"type\":\"object\",\"name\":\"Grid\",\"id\":\"p1396\",\"attributes\":{\"axis\":{\"id\":\"p1390\"}}},{\"type\":\"object\",\"name\":\"Grid\",\"id\":\"p1403\",\"attributes\":{\"dimension\":1,\"axis\":{\"id\":\"p1397\"}}}]}}],\"callbacks\":{\"type\":\"map\"}}};\n", " const render_items = [{\"docid\":\"893be2ab-5f82-4796-be4c-9add82a77829\",\"roots\":{\"p1372\":\"b7df0c26-e695-42d1-ba32-6703c13e500c\"},\"root_ids\":[\"p1372\"]}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\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": "p1372" } }, "output_type": "display_data" } ], "source": [ "import math\n", "\n", "from bokeh.io import show\n", "from bokeh.models import Ellipse, GraphRenderer, StaticLayoutProvider\n", "from bokeh.palettes import Spectral8\n", "from bokeh.plotting import figure\n", "\n", "\n", "N = 8\n", "node_indices = list(range(N))\n", "\n", "p = figure(\n", " title=\"Graph Layout Demonstration\",\n", " x_range=(-1.1, 1.1),\n", " y_range=(-1.1, 1.1),\n", " tools=\"\",\n", " toolbar_location=None,\n", ")\n", "\n", "graph = GraphRenderer()\n", "\n", "graph.node_renderer.data_source.add(node_indices, \"index\")\n", "graph.node_renderer.data_source.add(Spectral8, \"color\")\n", "graph.node_renderer.glyph = Ellipse(height=0.1, width=0.2, fill_color=\"color\")\n", "\n", "graph.edge_renderer.data_source.data = dict(start=[0] * N, end=node_indices)\n", "\n", "### start of layout code\n", "circ = [i * 2 * math.pi / 8 for i in node_indices]\n", "x = [math.cos(i) for i in circ]\n", "y = [math.sin(i) for i in circ]\n", "graph_layout = dict(zip(node_indices, zip(x, y)))\n", "graph.layout_provider = StaticLayoutProvider(graph_layout=graph_layout)\n", "\n", "\n", "### Draw quadratic bezier paths\n", "def bezier(start, end, control, steps):\n", " return [\n", " (1 - s) ** 2 * start + 2 * (1 - s) * s * control + s**2 * end\n", " for s in steps\n", " ]\n", "\n", "\n", "xs, ys = [], []\n", "sx, sy = graph_layout[0]\n", "steps = [i / 100.0 for i in range(100)]\n", "for node_index in node_indices:\n", " ex, ey = graph_layout[node_index]\n", " xs.append(bezier(sx, ex, 0, steps))\n", " ys.append(bezier(sy, ey, 0, steps))\n", "graph.edge_renderer.data_source.data[\"xs\"] = xs\n", "graph.edge_renderer.data_source.data[\"ys\"] = ys\n", "\n", "p.renderers.append(graph)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NetworkX-Integration\n", "\n", "Bokeh unterstützt das schnelle Zeichnen eines Netzwerkgraphen mit seiner NetworkX-Integration. `bokeh.models.graphs.from_networkx` akzeptiert ein `networkx.Graph`-Objekt und eine `networkx-Layout`-Methode, um eine konfigurierte `GraphRenderer`-Instanz zurückzugeben.\n", "\n", "Hier ist ein Beispiel für die Verwendung der `networkx.spring_layout`-Methode zum Layout des in NetworkX integrierten Datensatzes `karate_club_graph`:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " const docs_json = {\"2d0d92d1-4ce7-4b2e-b67c-d27881a1c538\":{\"version\":\"3.1.1\",\"title\":\"Bokeh Application\",\"defs\":[],\"roots\":[{\"type\":\"object\",\"name\":\"Figure\",\"id\":\"p1718\",\"attributes\":{\"x_range\":{\"type\":\"object\",\"name\":\"Range1d\",\"id\":\"p1728\",\"attributes\":{\"start\":-1.1,\"end\":1.1}},\"y_range\":{\"type\":\"object\",\"name\":\"Range1d\",\"id\":\"p1730\",\"attributes\":{\"start\":-1.1,\"end\":1.1}},\"x_scale\":{\"type\":\"object\",\"name\":\"LinearScale\",\"id\":\"p1732\"},\"y_scale\":{\"type\":\"object\",\"name\":\"LinearScale\",\"id\":\"p1734\"},\"title\":{\"type\":\"object\",\"name\":\"Title\",\"id\":\"p1721\",\"attributes\":{\"text\":\"Networkx Integration Demonstration\"}},\"renderers\":[{\"type\":\"object\",\"name\":\"GraphRenderer\",\"id\":\"p1751\",\"attributes\":{\"layout_provider\":{\"type\":\"object\",\"name\":\"StaticLayoutProvider\",\"id\":\"p1782\",\"attributes\":{\"graph_layout\":{\"type\":\"map\",\"entries\":[[0,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"TnmYleoq3z9kR3oUT1Xsvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[1,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"bD+QPdN7pL8laAmBHV3lvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[2,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"PljvmPs9wT/LW30zFtHAvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[3,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"bNijfx4Bqj8EVmlb98Dvvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[4,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"CFsEg+j06j/vd9IUpmP7vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[5,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"nLTpJXd89z/eFn45cLv3vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[6,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"oUOv9CEn9T+SNuxaSAv6vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[7,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"pv1yH+Pu1z8RWZeODM/jvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[8,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"isBgddzfz78I73o6RVvFPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[9,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"ldVSyKj54z9Pan+3nlvsPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[10,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"Ya5Z08wX8j9L2UQ16zv+vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[11,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"3zn3TD6y9j+Spsxc3P7qvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[12,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"S60zyTIqk7/AxXeSQPL9vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[13,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"16xhFbQcvr/EzbVkXYzTvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[14,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"DOAIhUQy8b/VwZw1Sib4Pw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[15,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"lMV028bF8r8LWDp/sHjmPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[16,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"UtzfdZNg/z+PLKg1SF//vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[17,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"yazGtIU+1L8I+15npW35vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[18,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"J1iWMiDe97+om4GT+o33Pw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[19,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"ZtInMSF/4L8OQMunSNrkvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[20,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"cYMNqmCE+b/fHlftoTzrPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[21,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"WJCQZ8ZQzD8Pun0m1zT4vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[22,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"CbCxrDPd879YzKKOvf/xPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[23,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"ackM2X2u07/aXTwpFQr1Pw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[24,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"Z6tqEogw4z/LvQBBzSb5Pw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[25,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"9jwrfx4YyT+gV0rt2Y32Pw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[26,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"rMdzc52E5r8AAAAAAAAAQA==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[27,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"jS58p/HUtj/9TTQikbnwPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[28,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"o6//T8hx2T8fFTeOxhvgPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[29,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"I20i+TdO5b+ZdeCFmEn5Pw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[30,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"VBl3rYjz5b8HQ5l7mpHGPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[31,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"DhtFCdGZtj/e2Qi2y9fnPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[32,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"jsXROGua5r/8LW4Vwf/rPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[33,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"cQ4al20W4L/NQlQSW9/pPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}]]}}},\"node_renderer\":{\"type\":\"object\",\"name\":\"GlyphRenderer\",\"id\":\"p1765\",\"attributes\":{\"data_source\":{\"type\":\"object\",\"name\":\"ColumnDataSource\",\"id\":\"p1762\",\"attributes\":{\"selected\":{\"type\":\"object\",\"name\":\"Selection\",\"id\":\"p1763\",\"attributes\":{\"indices\":[],\"line_indices\":[]}},\"selection_policy\":{\"type\":\"object\",\"name\":\"UnionRenderers\",\"id\":\"p1764\"},\"data\":{\"type\":\"map\",\"entries\":[[\"club\",[\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Officer\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Officer\",\"Officer\",\"Mr. Hi\",\"Mr. Hi\",\"Officer\",\"Mr. Hi\",\"Officer\",\"Mr. Hi\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\"]],[\"index\",[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33]]]}}},\"view\":{\"type\":\"object\",\"name\":\"CDSView\",\"id\":\"p1766\",\"attributes\":{\"filter\":{\"type\":\"object\",\"name\":\"AllIndices\",\"id\":\"p1767\"}}},\"glyph\":{\"type\":\"object\",\"name\":\"Circle\",\"id\":\"p1761\"}}},\"edge_renderer\":{\"type\":\"object\",\"name\":\"GlyphRenderer\",\"id\":\"p1757\",\"attributes\":{\"data_source\":{\"type\":\"object\",\"name\":\"ColumnDataSource\",\"id\":\"p1754\",\"attributes\":{\"selected\":{\"type\":\"object\",\"name\":\"Selection\",\"id\":\"p1755\",\"attributes\":{\"indices\":[],\"line_indices\":[]}},\"selection_policy\":{\"type\":\"object\",\"name\":\"UnionRenderers\",\"id\":\"p1756\"},\"data\":{\"type\":\"map\",\"entries\":[[\"weight\",[4,5,3,3,3,3,2,2,2,3,1,3,2,2,2,2,6,3,4,5,1,2,2,2,3,4,5,1,3,2,2,2,3,3,3,2,3,5,3,3,3,3,3,4,2,3,3,2,3,4,1,2,1,3,1,2,3,5,4,3,5,4,2,3,2,7,4,2,4,2,2,4,2,3,3,4,4,5]],[\"start\",[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,4,4,5,5,5,6,8,8,8,9,13,14,14,15,15,18,18,19,20,20,22,22,23,23,23,23,23,24,24,24,25,26,26,27,28,28,29,29,30,30,31,31,32]],[\"end\",[1,2,3,4,5,6,7,8,10,11,12,13,17,19,21,31,2,3,7,13,17,19,21,30,3,7,8,9,13,27,28,32,7,12,13,6,10,6,10,16,16,30,32,33,33,33,32,33,32,33,32,33,33,32,33,32,33,25,27,29,32,33,25,27,31,31,29,33,33,31,33,32,33,32,33,32,33,33]]]}}},\"view\":{\"type\":\"object\",\"name\":\"CDSView\",\"id\":\"p1758\",\"attributes\":{\"filter\":{\"type\":\"object\",\"name\":\"AllIndices\",\"id\":\"p1759\"}}},\"glyph\":{\"type\":\"object\",\"name\":\"MultiLine\",\"id\":\"p1753\"}}},\"selection_policy\":{\"type\":\"object\",\"name\":\"NodesOnly\",\"id\":\"p1760\"},\"inspection_policy\":{\"type\":\"object\",\"name\":\"NodesOnly\",\"id\":\"p1752\"}}}],\"toolbar\":{\"type\":\"object\",\"name\":\"Toolbar\",\"id\":\"p1726\"},\"toolbar_location\":null,\"left\":[{\"type\":\"object\",\"name\":\"LinearAxis\",\"id\":\"p1743\",\"attributes\":{\"ticker\":{\"type\":\"object\",\"name\":\"BasicTicker\",\"id\":\"p1746\",\"attributes\":{\"mantissas\":[1,2,5]}},\"formatter\":{\"type\":\"object\",\"name\":\"BasicTickFormatter\",\"id\":\"p1745\"},\"major_label_policy\":{\"type\":\"object\",\"name\":\"AllLabels\",\"id\":\"p1744\"}}}],\"below\":[{\"type\":\"object\",\"name\":\"LinearAxis\",\"id\":\"p1736\",\"attributes\":{\"ticker\":{\"type\":\"object\",\"name\":\"BasicTicker\",\"id\":\"p1739\",\"attributes\":{\"mantissas\":[1,2,5]}},\"formatter\":{\"type\":\"object\",\"name\":\"BasicTickFormatter\",\"id\":\"p1738\"},\"major_label_policy\":{\"type\":\"object\",\"name\":\"AllLabels\",\"id\":\"p1737\"}}}],\"center\":[{\"type\":\"object\",\"name\":\"Grid\",\"id\":\"p1742\",\"attributes\":{\"axis\":{\"id\":\"p1736\"}}},{\"type\":\"object\",\"name\":\"Grid\",\"id\":\"p1749\",\"attributes\":{\"dimension\":1,\"axis\":{\"id\":\"p1743\"}}}]}}],\"callbacks\":{\"type\":\"map\"}}};\n", " const render_items = [{\"docid\":\"2d0d92d1-4ce7-4b2e-b67c-d27881a1c538\",\"roots\":{\"p1718\":\"ecf60698-6f47-441f-942b-2e0ef4573e83\"},\"root_ids\":[\"p1718\"]}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\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": "p1718" } }, "output_type": "display_data" } ], "source": [ "import networkx as nx\n", "\n", "from bokeh.io import show\n", "from bokeh.plotting import figure, from_networkx\n", "\n", "\n", "G = nx.karate_club_graph()\n", "\n", "p = figure(\n", " title=\"Networkx Integration Demonstration\",\n", " x_range=(-1.1, 1.1),\n", " y_range=(-1.1, 1.1),\n", " tools=\"\",\n", " toolbar_location=None,\n", ")\n", "\n", "graph = from_networkx(G, nx.spring_layout, scale=2, center=(0, 0))\n", "p.renderers.append(graph)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interaktionen\n", "\n", "Ihr könnt das Auswahl- oder Überprüfungsverhalten von Diagrammen konfigurieren, indem ihr die `selection_policy`- und `inspection_policy`-Attribute des `GraphRenderer` festlegt. Diese Attribute akzeptieren eine spezielle `GraphHitTestPolicy`-Instanz.\n", "\n", "So kann beispielsweise `selection_policy=NodesAndLinkedEdges()` bewirken, dass mit einem ausgewählten Knoten auch die zugehörigen Kanten ausgewählt werden. In ähnlicher Weise werden durch `inspection_policy=EdgesAndLinkedNodes()` der Start- und Endknoten einer Kante auch beim Bewegen der Maus über eine Kante mit dem HoverTool überprüft.\n", "\n", "Mit den `selection_glyph`-, `nonselection_glyph`- und `hover_glyph`-Attributen der Kanten- und Knotenrenderer können die Diagramminteraktionen auch dynamische visuelle Elemente hinzuzufügen. \n", "\n", "Hier ist ein Beispiel mit solchen Knoten- und Kanteninteraktionen:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " const docs_json = {\"3d3b51b0-8868-4919-a4c5-0a2d0e6edbc3\":{\"version\":\"3.1.1\",\"title\":\"Bokeh Application\",\"defs\":[],\"roots\":[{\"type\":\"object\",\"name\":\"Plot\",\"id\":\"p2104\",\"attributes\":{\"width\":400,\"height\":400,\"x_range\":{\"type\":\"object\",\"name\":\"Range1d\",\"id\":\"p2102\",\"attributes\":{\"start\":-1.1,\"end\":1.1}},\"y_range\":{\"type\":\"object\",\"name\":\"Range1d\",\"id\":\"p2103\",\"attributes\":{\"start\":-1.1,\"end\":1.1}},\"x_scale\":{\"type\":\"object\",\"name\":\"LinearScale\",\"id\":\"p2107\"},\"y_scale\":{\"type\":\"object\",\"name\":\"LinearScale\",\"id\":\"p2108\"},\"title\":{\"type\":\"object\",\"name\":\"Title\",\"id\":\"p2110\",\"attributes\":{\"text\":\"Graph Interaction Demonstration\"}},\"renderers\":[{\"type\":\"object\",\"name\":\"GraphRenderer\",\"id\":\"p2119\",\"attributes\":{\"layout_provider\":{\"type\":\"object\",\"name\":\"StaticLayoutProvider\",\"id\":\"p2150\",\"attributes\":{\"graph_layout\":{\"type\":\"map\",\"entries\":[[0,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAAAAA8D+1tLS07JUVPg==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[1,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAAIR07z+XvbKiGoXHPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[2,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAANDW7T/LXllhlR7XPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[3,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAgPo06z9mr6xAiNjgPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[4,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAAPal5z9mr6wg6o7lPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[5,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAgMhI4z9mr6xgW4npPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[6,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAIPqG3D9mr6yALaXsPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[7,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAwLGD0T9mr6ygRsfuPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[8,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAYOaetz9mr6zgDd3vPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[9,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAA4OWet79mr6zgDd3vPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[10,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAgLGD0b9mr6ygRsfuPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[11,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAA4PmG3L9mr6yALaXsPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[12,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAgMhI479mr6xgW4npPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[13,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAA4PWl579mr6xg6o7lPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[14,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAgPo0679mr6xAiNjgPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[15,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAA4M/W7b/LXlnhlR7XPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[16,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAAIR077+XvbLCGoXHPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[17,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAAAAA8L8tLS0NTiF3vg==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[18,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAAIR0779pQk09GoXHvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[19,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAANDW7b81oaaelR7Xvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[20,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAoPo067+aUFMfiNjgvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[21,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAAPal57+aUFMf6o7lvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[22,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAoMhI47+aUFM/W4npvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[23,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAQPqG3L+aUFN/LaXsvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[24,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAYLGD0b+aUFOfRsfuvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[25,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAA4OKet7+aUFP/Dd3vvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[26,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAYOOetz+aUFP/Dd3vvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[27,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAYLGD0T+aUFOfRsfuvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[28,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAQPqG3D+aUFN/LaXsvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[29,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAoMhI4z+aUFM/W4npvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[30,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAoPWl5z+aUFN/6o7lvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[31,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAYPo06z+aUFN/iNjgvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[32,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAANDW7T81oaZ+lR7Xvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[33,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAAIR07z9pQk39GYXHvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}]]}}},\"node_renderer\":{\"type\":\"object\",\"name\":\"GlyphRenderer\",\"id\":\"p2133\",\"attributes\":{\"data_source\":{\"type\":\"object\",\"name\":\"ColumnDataSource\",\"id\":\"p2130\",\"attributes\":{\"selected\":{\"type\":\"object\",\"name\":\"Selection\",\"id\":\"p2131\",\"attributes\":{\"indices\":[],\"line_indices\":[]}},\"selection_policy\":{\"type\":\"object\",\"name\":\"UnionRenderers\",\"id\":\"p2132\"},\"data\":{\"type\":\"map\",\"entries\":[[\"club\",[\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Officer\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Officer\",\"Officer\",\"Mr. Hi\",\"Mr. Hi\",\"Officer\",\"Mr. Hi\",\"Officer\",\"Mr. Hi\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\"]],[\"index\",[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33]]]}}},\"view\":{\"type\":\"object\",\"name\":\"CDSView\",\"id\":\"p2134\",\"attributes\":{\"filter\":{\"type\":\"object\",\"name\":\"AllIndices\",\"id\":\"p2135\"}}},\"glyph\":{\"type\":\"object\",\"name\":\"Circle\",\"id\":\"p2151\",\"attributes\":{\"size\":{\"type\":\"value\",\"value\":15},\"fill_color\":{\"type\":\"value\",\"value\":\"#2b83ba\"}}},\"selection_glyph\":{\"type\":\"object\",\"name\":\"Circle\",\"id\":\"p2159\",\"attributes\":{\"size\":{\"type\":\"value\",\"value\":15},\"fill_color\":{\"type\":\"value\",\"value\":\"#fdae61\"}}},\"hover_glyph\":{\"type\":\"object\",\"name\":\"Circle\",\"id\":\"p2167\",\"attributes\":{\"size\":{\"type\":\"value\",\"value\":15},\"fill_color\":{\"type\":\"value\",\"value\":\"#abdda4\"}}}}},\"edge_renderer\":{\"type\":\"object\",\"name\":\"GlyphRenderer\",\"id\":\"p2125\",\"attributes\":{\"data_source\":{\"type\":\"object\",\"name\":\"ColumnDataSource\",\"id\":\"p2122\",\"attributes\":{\"selected\":{\"type\":\"object\",\"name\":\"Selection\",\"id\":\"p2123\",\"attributes\":{\"indices\":[],\"line_indices\":[]}},\"selection_policy\":{\"type\":\"object\",\"name\":\"UnionRenderers\",\"id\":\"p2124\"},\"data\":{\"type\":\"map\",\"entries\":[[\"weight\",[4,5,3,3,3,3,2,2,2,3,1,3,2,2,2,2,6,3,4,5,1,2,2,2,3,4,5,1,3,2,2,2,3,3,3,2,3,5,3,3,3,3,3,4,2,3,3,2,3,4,1,2,1,3,1,2,3,5,4,3,5,4,2,3,2,7,4,2,4,2,2,4,2,3,3,4,4,5]],[\"start\",[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,4,4,5,5,5,6,8,8,8,9,13,14,14,15,15,18,18,19,20,20,22,22,23,23,23,23,23,24,24,24,25,26,26,27,28,28,29,29,30,30,31,31,32]],[\"end\",[1,2,3,4,5,6,7,8,10,11,12,13,17,19,21,31,2,3,7,13,17,19,21,30,3,7,8,9,13,27,28,32,7,12,13,6,10,6,10,16,16,30,32,33,33,33,32,33,32,33,32,33,33,32,33,32,33,25,27,29,32,33,25,27,31,31,29,33,33,31,33,32,33,32,33,32,33,33]]]}}},\"view\":{\"type\":\"object\",\"name\":\"CDSView\",\"id\":\"p2126\",\"attributes\":{\"filter\":{\"type\":\"object\",\"name\":\"AllIndices\",\"id\":\"p2127\"}}},\"glyph\":{\"type\":\"object\",\"name\":\"MultiLine\",\"id\":\"p2175\",\"attributes\":{\"line_color\":{\"type\":\"value\",\"value\":\"#CCCCCC\"},\"line_alpha\":{\"type\":\"value\",\"value\":0.8},\"line_width\":{\"type\":\"value\",\"value\":5}}},\"selection_glyph\":{\"type\":\"object\",\"name\":\"MultiLine\",\"id\":\"p2183\",\"attributes\":{\"line_color\":{\"type\":\"value\",\"value\":\"#fdae61\"},\"line_width\":{\"type\":\"value\",\"value\":5}}},\"hover_glyph\":{\"type\":\"object\",\"name\":\"MultiLine\",\"id\":\"p2191\",\"attributes\":{\"line_color\":{\"type\":\"value\",\"value\":\"#abdda4\"},\"line_width\":{\"type\":\"value\",\"value\":5}}}}},\"selection_policy\":{\"type\":\"object\",\"name\":\"NodesAndLinkedEdges\",\"id\":\"p2199\"},\"inspection_policy\":{\"type\":\"object\",\"name\":\"EdgesAndLinkedNodes\",\"id\":\"p2201\"}}}],\"toolbar\":{\"type\":\"object\",\"name\":\"Toolbar\",\"id\":\"p2109\",\"attributes\":{\"tools\":[{\"type\":\"object\",\"name\":\"HoverTool\",\"id\":\"p2112\",\"attributes\":{\"renderers\":\"auto\",\"tooltips\":null}},{\"type\":\"object\",\"name\":\"TapTool\",\"id\":\"p2113\",\"attributes\":{\"renderers\":\"auto\"}},{\"type\":\"object\",\"name\":\"BoxSelectTool\",\"id\":\"p2114\",\"attributes\":{\"renderers\":\"auto\",\"overlay\":{\"type\":\"object\",\"name\":\"BoxAnnotation\",\"id\":\"p2115\",\"attributes\":{\"syncable\":false,\"level\":\"overlay\",\"visible\":false,\"editable\":true,\"line_color\":\"black\",\"line_alpha\":1.0,\"line_width\":2,\"line_dash\":[4,4],\"fill_color\":\"lightgrey\",\"fill_alpha\":0.5}}}}]}}}}],\"callbacks\":{\"type\":\"map\"}}};\n", " const render_items = [{\"docid\":\"3d3b51b0-8868-4919-a4c5-0a2d0e6edbc3\",\"roots\":{\"p2104\":\"a1e73631-368d-4976-806f-cbcfc1dd6c70\"},\"root_ids\":[\"p2104\"]}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\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": "p2104" } }, "output_type": "display_data" } ], "source": [ "import networkx as nx\n", "\n", "from bokeh.io import show\n", "from bokeh.models import (\n", " BoxSelectTool,\n", " Circle,\n", " HoverTool,\n", " MultiLine,\n", " Plot,\n", " Range1d,\n", " TapTool,\n", ")\n", "from bokeh.models.graphs import EdgesAndLinkedNodes, NodesAndLinkedEdges\n", "from bokeh.palettes import Spectral4\n", "from bokeh.plotting import from_networkx\n", "\n", "\n", "G = nx.karate_club_graph()\n", "\n", "p = Plot(\n", " width=400,\n", " height=400,\n", " x_range=Range1d(-1.1, 1.1),\n", " y_range=Range1d(-1.1, 1.1),\n", ")\n", "p.title.text = \"Graph Interaction Demonstration\"\n", "\n", "p.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool())\n", "\n", "graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0, 0))\n", "\n", "graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])\n", "graph_renderer.node_renderer.selection_glyph = Circle(\n", " size=15, fill_color=Spectral4[2]\n", ")\n", "graph_renderer.node_renderer.hover_glyph = Circle(\n", " size=15, fill_color=Spectral4[1]\n", ")\n", "\n", "graph_renderer.edge_renderer.glyph = MultiLine(\n", " line_color=\"#CCCCCC\", line_alpha=0.8, line_width=5\n", ")\n", "graph_renderer.edge_renderer.selection_glyph = MultiLine(\n", " line_color=Spectral4[2], line_width=5\n", ")\n", "graph_renderer.edge_renderer.hover_glyph = MultiLine(\n", " line_color=Spectral4[1], line_width=5\n", ")\n", "\n", "graph_renderer.selection_policy = NodesAndLinkedEdges()\n", "graph_renderer.inspection_policy = EdgesAndLinkedNodes()\n", "\n", "p.renderers.append(graph_renderer)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Knoten- und Kantenattribute\n", "\n", "In `from_networkx` werden die Knoten- und Kantenattribute von NetworkX für den `node_renderer`oder `edge_renderer` umgewandelt.\n", "\n", "Das Dataset `karate_club_graph` hat beispielsweise ein Knotenattribut mit dem Namen `club`. Mit `from_networkx` ist es möglich, diese Informationen zu verschieben. In ähnlicher Weise können auch weitere Knoten- und Kantenattribute für Farbinformationen verwendet werden:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " const docs_json = {\"bb78c39e-40ee-4a52-82b7-72a84f2664fa\":{\"version\":\"3.1.1\",\"title\":\"Bokeh Application\",\"defs\":[],\"roots\":[{\"type\":\"object\",\"name\":\"Plot\",\"id\":\"p2539\",\"attributes\":{\"width\":400,\"height\":400,\"x_range\":{\"type\":\"object\",\"name\":\"Range1d\",\"id\":\"p2537\",\"attributes\":{\"start\":-1.1,\"end\":1.1}},\"y_range\":{\"type\":\"object\",\"name\":\"Range1d\",\"id\":\"p2538\",\"attributes\":{\"start\":-1.1,\"end\":1.1}},\"x_scale\":{\"type\":\"object\",\"name\":\"LinearScale\",\"id\":\"p2542\"},\"y_scale\":{\"type\":\"object\",\"name\":\"LinearScale\",\"id\":\"p2543\"},\"title\":{\"type\":\"object\",\"name\":\"Title\",\"id\":\"p2545\",\"attributes\":{\"text\":\"Graph Interaction Demonstration\"}},\"renderers\":[{\"type\":\"object\",\"name\":\"GraphRenderer\",\"id\":\"p2554\",\"attributes\":{\"layout_provider\":{\"type\":\"object\",\"name\":\"StaticLayoutProvider\",\"id\":\"p2585\",\"attributes\":{\"graph_layout\":{\"type\":\"map\",\"entries\":[[0,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"dIc1GbOv1T98tjL+OBrQvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[1,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"8s1ds8qAzz+6d5vtqiGsvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[2,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"UJ2R+2QdsD+NKG8gbGmLvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[3,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"DRMhm7rRxz+N/Gl5wcrTvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[4,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"EiolMmyU4j87h4hrWfDevw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[5,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"JxieyXls5z9HpgYP5Tngvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[6,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"UDjwN/Nr6D/xUKSlOtXZvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[7,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"VYCMK6+Bzz8O+PNd4rzFvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[8,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"DgiZflJmsr+GHegqbduzPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[9,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"ygH4UZbDkz9Mr6yRcyvgPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[10,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"CJ06vW+V4z+1HVO7zAPkvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[11,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"nJoPdZnqyT+6Ap9OjSXkvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[12,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"gthOq2wF1D8o8ghwK7njvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[13,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"YJ7qnmyjqD8XSTIRD6C7vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[14,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"CWT7keQO5b8MdJUXdX7QPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[15,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"GsusYgyp0r9kPzK+MIngPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[16,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"AAAAAAAA8D/DS/RJjEDivw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[17,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"fQxApp0m5j/DrCrw7lSevw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[18,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"qunyI0VR6L9bRWRgrX7YPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[19,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"3MTIjXMq0z/VY+tLiMDBPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[20,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"94fLe4It2b9QvRvjn37lPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[21,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"O2iGVFk74j/92juj1ee5vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[22,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"NWm2DHeg3b/HAgTQWFHhPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[23,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"SUdd5Aw24L+LLwSl2SnDPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[24,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"fIkJ/74V4r9FmigBlVHOvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[25,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"tis28W9w4L/lgdhe1jqxvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[26,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"+wCmZ+t+5b+22V4T7+LhPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[27,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"0mIdIke22L+sKHeh5uaOvw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[28,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"3aGJvF03nL9gQjyeMubQPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[29,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"mxkJpXIw4r/kBVo8MsjYPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[30,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"mSMfk+q4u79JO+5waVvKPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[31,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"LA9gdBb30L/sipn2P7N5vw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[32,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"BmlfzIxA17+WwST3wVzSPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}],[33,{\"type\":\"ndarray\",\"array\":{\"type\":\"bytes\",\"data\":\"vvBtvL0X07+UdpwTGE7QPw==\"},\"shape\":[2],\"dtype\":\"float64\",\"order\":\"little\"}]]}}},\"node_renderer\":{\"type\":\"object\",\"name\":\"GlyphRenderer\",\"id\":\"p2568\",\"attributes\":{\"data_source\":{\"type\":\"object\",\"name\":\"ColumnDataSource\",\"id\":\"p2565\",\"attributes\":{\"selected\":{\"type\":\"object\",\"name\":\"Selection\",\"id\":\"p2566\",\"attributes\":{\"indices\":[],\"line_indices\":[]}},\"selection_policy\":{\"type\":\"object\",\"name\":\"UnionRenderers\",\"id\":\"p2567\"},\"data\":{\"type\":\"map\",\"entries\":[[\"club\",[\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Officer\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Mr. Hi\",\"Officer\",\"Officer\",\"Mr. Hi\",\"Mr. Hi\",\"Officer\",\"Mr. Hi\",\"Officer\",\"Mr. Hi\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\",\"Officer\"]],[\"index\",[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33]]]}}},\"view\":{\"type\":\"object\",\"name\":\"CDSView\",\"id\":\"p2569\",\"attributes\":{\"filter\":{\"type\":\"object\",\"name\":\"AllIndices\",\"id\":\"p2570\"}}},\"glyph\":{\"type\":\"object\",\"name\":\"Circle\",\"id\":\"p2586\",\"attributes\":{\"size\":{\"type\":\"value\",\"value\":15},\"fill_color\":{\"type\":\"value\",\"value\":\"#2b83ba\"}}}}},\"edge_renderer\":{\"type\":\"object\",\"name\":\"GlyphRenderer\",\"id\":\"p2560\",\"attributes\":{\"data_source\":{\"type\":\"object\",\"name\":\"ColumnDataSource\",\"id\":\"p2557\",\"attributes\":{\"selected\":{\"type\":\"object\",\"name\":\"Selection\",\"id\":\"p2558\",\"attributes\":{\"indices\":[],\"line_indices\":[]}},\"selection_policy\":{\"type\":\"object\",\"name\":\"UnionRenderers\",\"id\":\"p2559\"},\"data\":{\"type\":\"map\",\"entries\":[[\"weight\",[4,5,3,3,3,3,2,2,2,3,1,3,2,2,2,2,6,3,4,5,1,2,2,2,3,4,5,1,3,2,2,2,3,3,3,2,3,5,3,3,3,3,3,4,2,3,3,2,3,4,1,2,1,3,1,2,3,5,4,3,5,4,2,3,2,7,4,2,4,2,2,4,2,3,3,4,4,5]],[\"edge_color\",[\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"red\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"red\",\"black\",\"black\",\"black\",\"red\",\"black\",\"red\",\"red\",\"red\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"red\",\"red\",\"red\",\"black\",\"red\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"red\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\",\"black\"]],[\"start\",[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,4,4,5,5,5,6,8,8,8,9,13,14,14,15,15,18,18,19,20,20,22,22,23,23,23,23,23,24,24,24,25,26,26,27,28,28,29,29,30,30,31,31,32]],[\"end\",[1,2,3,4,5,6,7,8,10,11,12,13,17,19,21,31,2,3,7,13,17,19,21,30,3,7,8,9,13,27,28,32,7,12,13,6,10,6,10,16,16,30,32,33,33,33,32,33,32,33,32,33,33,32,33,32,33,25,27,29,32,33,25,27,31,31,29,33,33,31,33,32,33,32,33,32,33,33]]]}}},\"view\":{\"type\":\"object\",\"name\":\"CDSView\",\"id\":\"p2561\",\"attributes\":{\"filter\":{\"type\":\"object\",\"name\":\"AllIndices\",\"id\":\"p2562\"}}},\"glyph\":{\"type\":\"object\",\"name\":\"MultiLine\",\"id\":\"p2594\",\"attributes\":{\"line_color\":{\"type\":\"field\",\"field\":\"edge_color\"},\"line_alpha\":{\"type\":\"value\",\"value\":0.8}}}}},\"selection_policy\":{\"type\":\"object\",\"name\":\"NodesOnly\",\"id\":\"p2563\"},\"inspection_policy\":{\"type\":\"object\",\"name\":\"NodesOnly\",\"id\":\"p2555\"}}}],\"toolbar\":{\"type\":\"object\",\"name\":\"Toolbar\",\"id\":\"p2544\",\"attributes\":{\"tools\":[{\"type\":\"object\",\"name\":\"HoverTool\",\"id\":\"p2547\",\"attributes\":{\"renderers\":\"auto\",\"tooltips\":[[\"index\",\"@index\"],[\"club\",\"@club\"]]}},{\"type\":\"object\",\"name\":\"BoxZoomTool\",\"id\":\"p2548\",\"attributes\":{\"overlay\":{\"type\":\"object\",\"name\":\"BoxAnnotation\",\"id\":\"p2549\",\"attributes\":{\"syncable\":false,\"level\":\"overlay\",\"visible\":false,\"left_units\":\"canvas\",\"right_units\":\"canvas\",\"bottom_units\":\"canvas\",\"top_units\":\"canvas\",\"line_color\":\"black\",\"line_alpha\":1.0,\"line_width\":2,\"line_dash\":[4,4],\"fill_color\":\"lightgrey\",\"fill_alpha\":0.5}}}},{\"type\":\"object\",\"name\":\"ResetTool\",\"id\":\"p2550\"}]}}}}],\"callbacks\":{\"type\":\"map\"}}};\n", " const render_items = [{\"docid\":\"bb78c39e-40ee-4a52-82b7-72a84f2664fa\",\"roots\":{\"p2539\":\"d3abf750-fcc4-412f-9c74-894f9916a8de\"},\"root_ids\":[\"p2539\"]}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\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": "p2539" } }, "output_type": "display_data" } ], "source": [ "import networkx as nx\n", "\n", "from bokeh.io import show\n", "from bokeh.models import (\n", " BoxZoomTool,\n", " Circle,\n", " HoverTool,\n", " MultiLine,\n", " Plot,\n", " Range1d,\n", " ResetTool,\n", ")\n", "from bokeh.palettes import Spectral4\n", "from bokeh.plotting import from_networkx\n", "\n", "\n", "# Prepare Data\n", "G = nx.karate_club_graph()\n", "\n", "SAME_CLUB_COLOR, DIFFERENT_CLUB_COLOR = \"black\", \"red\"\n", "edge_attrs = {}\n", "\n", "for start_node, end_node, _ in G.edges(data=True):\n", " edge_color = (\n", " SAME_CLUB_COLOR\n", " if G.nodes[start_node][\"club\"] == G.nodes[end_node][\"club\"]\n", " else DIFFERENT_CLUB_COLOR\n", " )\n", " edge_attrs[(start_node, end_node)] = edge_color\n", "\n", "nx.set_edge_attributes(G, edge_attrs, \"edge_color\")\n", "\n", "# Show with Bokeh\n", "p = Plot(\n", " width=400,\n", " height=400,\n", " x_range=Range1d(-1.1, 1.1),\n", " y_range=Range1d(-1.1, 1.1),\n", ")\n", "p.title.text = \"Graph Interaction Demonstration\"\n", "\n", "node_hover_tool = HoverTool(tooltips=[(\"index\", \"@index\"), (\"club\", \"@club\")])\n", "p.add_tools(node_hover_tool, BoxZoomTool(), ResetTool())\n", "\n", "graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0))\n", "\n", "graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])\n", "graph_renderer.edge_renderer.glyph = MultiLine(\n", " line_color=\"edge_color\", line_alpha=0.8, line_width=1\n", ")\n", "p.renderers.append(graph_renderer)\n", "\n", "show(p)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.11 Kernel", "language": "python", "name": "python311" }, "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.11.4" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }