{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "

Bokeh 教程

\n", "
\n", "\n", "

09. 地理图

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(root) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " var force = true;\n", "\n", " if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n", " root._bokeh_onload_callbacks = [];\n", " root._bokeh_is_loading = undefined;\n", " }\n", "\n", " var JS_MIME_TYPE = 'application/javascript';\n", " var HTML_MIME_TYPE = 'text/html';\n", " var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n", " var CLASS_NAME = 'output_bokeh rendered_html';\n", "\n", " /**\n", " * Render data to the DOM node\n", " */\n", " function render(props, node) {\n", " var script = document.createElement(\"script\");\n", " node.appendChild(script);\n", " }\n", "\n", " /**\n", " * Handle when an output is cleared or removed\n", " */\n", " function handleClearOutput(event, handle) {\n", " var cell = handle.cell;\n", "\n", " var id = cell.output_area._bokeh_element_id;\n", " var server_id = cell.output_area._bokeh_server_id;\n", " // Clean up Bokeh references\n", " if (id !== undefined) {\n", " Bokeh.index[id].model.document.clear();\n", " delete Bokeh.index[id];\n", " }\n", "\n", " if (server_id !== undefined) {\n", " // Clean up Bokeh references\n", " var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n", " cell.notebook.kernel.execute(cmd, {\n", " iopub: {\n", " output: function(msg) {\n", " var element_id = msg.content.text.trim();\n", " Bokeh.index[element_id].model.document.clear();\n", " delete Bokeh.index[element_id];\n", " }\n", " }\n", " });\n", " // Destroy server and session\n", " var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n", " cell.notebook.kernel.execute(cmd);\n", " }\n", " }\n", "\n", " /**\n", " * Handle when a new output is added\n", " */\n", " function handleAddOutput(event, handle) {\n", " var output_area = handle.output_area;\n", " var output = handle.output;\n", "\n", " // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n", " if ((output.output_type != \"display_data\") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n", " return\n", " }\n", "\n", " var toinsert = output_area.element.find(`.${CLASS_NAME.split(' ')[0]}`);\n", "\n", " if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n", " toinsert[0].firstChild.textContent = output.data[JS_MIME_TYPE];\n", " // store reference to embed id on output_area\n", " output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n", " }\n", " if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n", " var bk_div = document.createElement(\"div\");\n", " bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n", " var script_attrs = bk_div.children[0].attributes;\n", " for (var i = 0; i < script_attrs.length; i++) {\n", " toinsert[0].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n", " }\n", " // store reference to server id on output_area\n", " output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n", " }\n", " }\n", "\n", " function register_renderer(events, OutputArea) {\n", "\n", " function append_mime(data, metadata, element) {\n", " // create a DOM node to render to\n", " var toinsert = this.create_output_subarea(\n", " metadata,\n", " CLASS_NAME,\n", " EXEC_MIME_TYPE\n", " );\n", " this.keyboard_manager.register_events(toinsert);\n", " // Render to node\n", " var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n", " render(props, toinsert[0]);\n", " element.append(toinsert);\n", " return toinsert\n", " }\n", "\n", " /* Handle when an output is cleared or removed */\n", " events.on('clear_output.CodeCell', handleClearOutput);\n", " events.on('delete.Cell', handleClearOutput);\n", "\n", " /* Handle when a new output is added */\n", " events.on('output_added.OutputArea', handleAddOutput);\n", "\n", " /**\n", " * Register the mime type and append_mime function with output_area\n", " */\n", " OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n", " /* Is output safe? */\n", " safe: true,\n", " /* Index of renderer in `output_area.display_order` */\n", " index: 0\n", " });\n", " }\n", "\n", " // register the mime type if in Jupyter Notebook environment and previously unregistered\n", " if (root.Jupyter !== undefined) {\n", " var events = require('base/js/events');\n", " var OutputArea = require('notebook/js/outputarea').OutputArea;\n", "\n", " if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n", " register_renderer(events, OutputArea);\n", " }\n", " }\n", "\n", " \n", " if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n", " root._bokeh_timeout = Date.now() + 5000;\n", " root._bokeh_failed_load = false;\n", " }\n", "\n", " var NB_LOAD_WARNING = {'data': {'text/html':\n", " \"
\\n\"+\n", " \"

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

\\n\"+\n", " \"\\n\"+\n", " \"\\n\"+\n", " \"from bokeh.resources import INLINE\\n\"+\n", " \"output_notebook(resources=INLINE)\\n\"+\n", " \"\\n\"+\n", " \"
\"}};\n", "\n", " function display_loaded() {\n", " var el = document.getElementById(\"9e2439b0-95f4-4bcb-ba1e-1f03c1b5a300\");\n", " if (el != null) {\n", " el.textContent = \"BokehJS is loading...\";\n", " }\n", " if (root.Bokeh !== undefined) {\n", " if (el != null) {\n", " el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n", " }\n", " } else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(display_loaded, 100)\n", " }\n", " }\n", "\n", "\n", " function run_callbacks() {\n", " try {\n", " root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n", " }\n", " finally {\n", " delete root._bokeh_onload_callbacks\n", " }\n", " console.info(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(js_urls, callback) {\n", " root._bokeh_onload_callbacks.push(callback);\n", " if (root._bokeh_is_loading > 0) {\n", " console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " root._bokeh_is_loading = js_urls.length;\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " var s = document.createElement('script');\n", " s.src = url;\n", " s.async = false;\n", " s.onreadystatechange = s.onload = function() {\n", " root._bokeh_is_loading--;\n", " if (root._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: all BokehJS libraries loaded\");\n", " run_callbacks()\n", " }\n", " };\n", " s.onerror = function() {\n", " console.warn(\"failed to load library \" + url);\n", " };\n", " console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", " }\n", " };var element = document.getElementById(\"9e2439b0-95f4-4bcb-ba1e-1f03c1b5a300\");\n", " if (element == null) {\n", " console.log(\"Bokeh: ERROR: autoload.js configured with elementid '9e2439b0-95f4-4bcb-ba1e-1f03c1b5a300' but no matching script tag was found. \")\n", " return false;\n", " }\n", "\n", " var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.12.10.min.js\"];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " \n", " },\n", " function(Bokeh) {\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " \n", " if ((root.Bokeh !== undefined) || (force === true)) {\n", " for (var i = 0; i < inline_js.length; i++) {\n", " inline_js[i].call(root, root.Bokeh);\n", " }if (force === true) {\n", " display_loaded();\n", " }} else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(run_inline_js, 100);\n", " } else if (!root._bokeh_failed_load) {\n", " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", " root._bokeh_failed_load = true;\n", " } else if (force !== true) {\n", " var cell = $(document.getElementById(\"9e2439b0-95f4-4bcb-ba1e-1f03c1b5a300\")).parents('.cell').data().cell;\n", " cell.output_area.append_execute_result(NB_LOAD_WARNING)\n", " }\n", "\n", " }\n", "\n", " if (root._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(js_urls, function() {\n", " console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(window));" ], "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n function now() {\n return new Date();\n }\n\n var force = true;\n\n if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\n \n\n \n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_failed_load = false;\n }\n\n var NB_LOAD_WARNING = {'data': {'text/html':\n \"
\\n\"+\n \"

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

\\n\"+\n \"\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"\\n\"+\n \"
\"}};\n\n function display_loaded() {\n var el = document.getElementById(\"9e2439b0-95f4-4bcb-ba1e-1f03c1b5a300\");\n if (el != null) {\n el.textContent = \"BokehJS is loading...\";\n }\n if (root.Bokeh !== undefined) {\n if (el != null) {\n el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n }\n } else if (Date.now() < root._bokeh_timeout) {\n setTimeout(display_loaded, 100)\n }\n }\n\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n }\n finally {\n delete root._bokeh_onload_callbacks\n }\n console.info(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(js_urls, callback) {\n root._bokeh_onload_callbacks.push(callback);\n if (root._bokeh_is_loading > 0) {\n console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n }\n if (js_urls == null || js_urls.length === 0) {\n run_callbacks();\n return null;\n }\n console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n root._bokeh_is_loading = js_urls.length;\n for (var i = 0; i < js_urls.length; i++) {\n var url = js_urls[i];\n var s = document.createElement('script');\n s.src = url;\n s.async = false;\n s.onreadystatechange = s.onload = function() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.log(\"Bokeh: all BokehJS libraries loaded\");\n run_callbacks()\n }\n };\n s.onerror = function() {\n console.warn(\"failed to load library \" + url);\n };\n console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.getElementsByTagName(\"head\")[0].appendChild(s);\n }\n };var element = document.getElementById(\"9e2439b0-95f4-4bcb-ba1e-1f03c1b5a300\");\n if (element == null) {\n console.log(\"Bokeh: ERROR: autoload.js configured with elementid '9e2439b0-95f4-4bcb-ba1e-1f03c1b5a300' but no matching script tag was found. \")\n return false;\n }\n\n var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.12.10.min.js\"];\n\n var inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n \n function(Bokeh) {\n \n },\n function(Bokeh) {\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n }\n ];\n\n function run_inline_js() {\n \n if ((root.Bokeh !== undefined) || (force === true)) {\n for (var i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }if (force === true) {\n display_loaded();\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n } else if (force !== true) {\n var cell = $(document.getElementById(\"9e2439b0-95f4-4bcb-ba1e-1f03c1b5a300\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\n }\n\n }\n\n if (root._bokeh_is_loading === 0) {\n console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n run_inline_js();\n } else {\n load_libs(js_urls, function() {\n console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n run_inline_js();\n });\n }\n}(window));" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from bokeh.io import output_notebook, show\n", "output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "将数据集与真实世界上下文联系起来非常有用。你可以像任何其他类型的数据一样绘制地理数据,例如 [Texas Unemployment example](https://bokeh.pydata.org/en/latest/docs/gallery/texas.html) 的例子。但Bokeh提供了一些在地理坐标绘图的专门机制:\n", "\n", "* [GMapPlot](https://bokeh.pydata.org/en/latest/docs/user_guide/geo.html#google-maps-support): Bokeh 基于 Google Maps 绘图\n", "\n", "* [TileSource](https://bokeh.pydata.org/en/latest/docs/user_guide/geo.html#tile-providers), 特别是wmtstilesource:允许数据被覆盖于任何地图平铺显示服务器和自定义服务器的数据上,包括:\n", "[Google Maps](http://maps.google.com), \n", "[Stamen](http://maps.stamen.com), \n", "[MapQuest](https://www.mapquest.com/), \n", "[OpenStreetMap](https://www.openstreetmap.org), \n", "[ESRI](http://www.esri.com), \n", "\n", "* [GeoJSONDataSource](https://bokeh.pydata.org/en/dev/docs/user_guide/geo.html#geojson-datasource): 可使 [GeoJSON](http://geojson.org/) 类型数据用于Bokeh绘图,就像 `ColumnDataSource` 类型。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## WMTS Tile Source\n", "\n", "WTMS是最常见的平铺地图数据Web标准,即地图由标准大小的图块拼接而成,如此则可以在一个给定的缩放级别构造整个地图。WTMS使用Web Mercator格式,从英国的格林尼治向北向西测量距离,这样容易计算但不会扭曲全球形状。\n", "\n", "首先,我们创建一个覆盖美国的空白Bokeh图,范围按米计算:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from bokeh.plotting import figure\n", "from bokeh.models.tiles import WMTSTileSource\n", "\n", "# web mercator coordinates\n", "USA = x_range,y_range = ((-13884029,-7453304), (2698291,6455972))\n", "\n", "p = figure(tools='pan, wheel_zoom', x_range=x_range, y_range=y_range)\n", "p.axis.visible = False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`bokeh.models.tiles` 中提供了几个WTMS地图源。在这里,我们将展示通过使用格式字符串接口,Bokeh如何根据所需的缩放,X和Y值从供应商处请求图片:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
TileRenderer(
id = 'da5efdfe-6ea5-490f-be34-b58d6d9fdf6e', …)
alpha = 1.0,
js_event_callbacks = {},
js_property_callbacks = {},
level = 'underlay',
name = None,
render_parents = True,
subscribed_events = [],
tags = [],
tile_source = WMTSTileSource(id='3db6beb5-5153-47ca-bd8a-2e11536b3506', ...),
visible = True,
x_range_name = 'default',
y_range_name = 'default')
\n", "\n" ], "text/plain": [ "TileRenderer(id='da5efdfe-6ea5-490f-be34-b58d6d9fdf6e', ...)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "url = 'http://a.basemaps.cartocdn.com/dark_all/{Z}/{X}/{Y}.png'\n", "attribution = \"Tiles by Carto, under CC BY 3.0. Data by OSM, under ODbL\"\n", "\n", "p.add_tile(WMTSTileSource(url=url, attribution=attribution))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果显示这个地图,那么你可以使用滚轮缩放和平移工具在任何缩放级别浏览地图,Bokeh会从服务器请求适当的图块并放在图中正确的位置:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " var docs_json = {\"873ad661-ce95-45e0-9b7c-abed097225a9\":{\"roots\":{\"references\":[{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"b3ddc447-d8e3-4f05-9b31-e955984a7e8a\",\"type\":\"BasicTicker\"}},\"id\":\"c39940f0-bb3a-482d-b601-8e876b4d6eca\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"3462adaf-986d-416b-bcae-ebdf2d50d116\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"tile_source\":{\"id\":\"3db6beb5-5153-47ca-bd8a-2e11536b3506\",\"type\":\"WMTSTileSource\"}},\"id\":\"da5efdfe-6ea5-490f-be34-b58d6d9fdf6e\",\"type\":\"TileRenderer\"},{\"attributes\":{},\"id\":\"8eb292a1-03e6-41bc-9a7c-6497dfbbd5f0\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"da339d7e-733d-4071-8ce6-1235517ff909\",\"type\":\"LinearScale\"},{\"attributes\":{\"below\":[{\"id\":\"ae69af44-7666-4180-8bdb-ddeded47788c\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"b3dc20f4-43bd-44eb-b917-b1643d8cb847\",\"type\":\"LinearAxis\"}],\"renderers\":[{\"id\":\"ae69af44-7666-4180-8bdb-ddeded47788c\",\"type\":\"LinearAxis\"},{\"id\":\"f0fdc383-3b0d-4ef7-8435-94173adce6ee\",\"type\":\"Grid\"},{\"id\":\"b3dc20f4-43bd-44eb-b917-b1643d8cb847\",\"type\":\"LinearAxis\"},{\"id\":\"c39940f0-bb3a-482d-b601-8e876b4d6eca\",\"type\":\"Grid\"},{\"id\":\"da5efdfe-6ea5-490f-be34-b58d6d9fdf6e\",\"type\":\"TileRenderer\"}],\"title\":{\"id\":\"29066b59-97fe-44f3-8e29-8a666d1a6598\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"370f281e-166e-41bc-b86f-f70414177146\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"0c0ea3c4-8bce-4d22-a833-5e447269401b\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"8eb292a1-03e6-41bc-9a7c-6497dfbbd5f0\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"d1c5882c-c286-48db-8270-30073e11441f\",\"type\":\"Range1d\"},\"y_scale\":{\"id\":\"da339d7e-733d-4071-8ce6-1235517ff909\",\"type\":\"LinearScale\"}},\"id\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"formatter\":{\"id\":\"3462adaf-986d-416b-bcae-ebdf2d50d116\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"b3ddc447-d8e3-4f05-9b31-e955984a7e8a\",\"type\":\"BasicTicker\"},\"visible\":false},\"id\":\"b3dc20f4-43bd-44eb-b917-b1643d8cb847\",\"type\":\"LinearAxis\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"98e9ba8c-623c-4c46-9050-9ca36891da0e\",\"type\":\"PanTool\"},{\"id\":\"5a41a513-622a-44a9-8fee-aca38a5d8964\",\"type\":\"WheelZoomTool\"}]},\"id\":\"370f281e-166e-41bc-b86f-f70414177146\",\"type\":\"Toolbar\"},{\"attributes\":{\"attribution\":\"Tiles by Carto, under CC BY 3.0. Data by OSM, under ODbL\",\"url\":\"http://a.basemaps.cartocdn.com/dark_all/{Z}/{X}/{Y}.png\"},\"id\":\"3db6beb5-5153-47ca-bd8a-2e11536b3506\",\"type\":\"WMTSTileSource\"},{\"attributes\":{\"plot\":null,\"text\":\"\"},\"id\":\"29066b59-97fe-44f3-8e29-8a666d1a6598\",\"type\":\"Title\"},{\"attributes\":{\"callback\":null,\"end\":6455972,\"start\":2698291},\"id\":\"d1c5882c-c286-48db-8270-30073e11441f\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"5a41a513-622a-44a9-8fee-aca38a5d8964\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"plot\":{\"id\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"46eeb80e-de80-4faa-b156-11ff00b53218\",\"type\":\"BasicTicker\"}},\"id\":\"f0fdc383-3b0d-4ef7-8435-94173adce6ee\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"dcf83eb1-528f-44f4-854e-863496e476aa\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"callback\":null,\"end\":-7453304,\"start\":-13884029},\"id\":\"0c0ea3c4-8bce-4d22-a833-5e447269401b\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"98e9ba8c-623c-4c46-9050-9ca36891da0e\",\"type\":\"PanTool\"},{\"attributes\":{\"formatter\":{\"id\":\"dcf83eb1-528f-44f4-854e-863496e476aa\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"46eeb80e-de80-4faa-b156-11ff00b53218\",\"type\":\"BasicTicker\"},\"visible\":false},\"id\":\"ae69af44-7666-4180-8bdb-ddeded47788c\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"b3ddc447-d8e3-4f05-9b31-e955984a7e8a\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"46eeb80e-de80-4faa-b156-11ff00b53218\",\"type\":\"BasicTicker\"}],\"root_ids\":[\"b2b0f481-5cb8-4a45-a117-60c391b918de\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.10\"}};\n", " var render_items = [{\"docid\":\"873ad661-ce95-45e0-9b7c-abed097225a9\",\"elementid\":\"ed8bdc03-2d19-4807-b8b8-deadd3db8994\",\"modelid\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\"}];\n", "\n", " root.Bokeh.embed.embed_items(docs_json, render_items);\n", " }\n", "\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " var attempts = 0;\n", " var timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " clearInterval(timer);\n", " }\n", " attempts++;\n", " if (attempts > 100) {\n", " console.log(\"Bokeh: ERROR: Unable to embed document because BokehJS library is missing\")\n", " clearInterval(timer);\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "b2b0f481-5cb8-4a45-a117-60c391b918de" } }, "output_type": "display_data" } ], "source": [ "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "这就是在图表中放入地图数据要做的全部!当然,您通常还希望显示其他数据,除非您只想使用服务器提供的地图。现在,只要您能得到Web Mercator格式的坐标,您可以把任何您平常使用Bokeh图表想表达的东西添加到地图上。例如:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
latlonnamexy
030.2672-97.7431Austin-1.088071e+073.537942e+06
140.7128-74.0059NYC-8.238299e+064.970072e+06
\n", "
" ], "text/plain": [ " lat lon name x y\n", "0 30.2672 -97.7431 Austin -1.088071e+07 3.537942e+06\n", "1 40.7128 -74.0059 NYC -8.238299e+06 4.970072e+06" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "def wgs84_to_web_mercator(df, lon=\"lon\", lat=\"lat\"):\n", " \"\"\"Converts decimal longitude/latitude to Web Mercator format\"\"\"\n", " k = 6378137\n", " df[\"x\"] = df[lon] * (k * np.pi/180.0)\n", " df[\"y\"] = np.log(np.tan((90 + df[lat]) * np.pi/360.0)) * k\n", " return df\n", "\n", "df = pd.DataFrame(dict(name=[\"Austin\", \"NYC\"], lon=[-97.7431,-74.0059], lat=[30.2672,40.7128]))\n", "wgs84_to_web_mercator(df)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " var docs_json = {\"750c563e-2223-4574-8a22-b075fadb2d78\":{\"roots\":{\"references\":[{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"b3ddc447-d8e3-4f05-9b31-e955984a7e8a\",\"type\":\"BasicTicker\"}},\"id\":\"c39940f0-bb3a-482d-b601-8e876b4d6eca\",\"type\":\"Grid\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\"],\"data\":{\"x\":{\"__ndarray__\":\"TJjbA9nAZMGf/KLGNm1fwQ==\",\"dtype\":\"float64\",\"shape\":[2]},\"y\":{\"__ndarray__\":\"bqXdLQv+SkFnqxDllfVSQQ==\",\"dtype\":\"float64\",\"shape\":[2]}}},\"id\":\"80229149-194b-4235-baa1-b1086ad8d184\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"3462adaf-986d-416b-bcae-ebdf2d50d116\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"tile_source\":{\"id\":\"3db6beb5-5153-47ca-bd8a-2e11536b3506\",\"type\":\"WMTSTileSource\"}},\"id\":\"da5efdfe-6ea5-490f-be34-b58d6d9fdf6e\",\"type\":\"TileRenderer\"},{\"attributes\":{\"fill_color\":{\"value\":\"orange\"},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":10},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"b235ba8f-0cfc-496f-b26d-6e657095d8b7\",\"type\":\"Circle\"},{\"attributes\":{\"data_source\":{\"id\":\"80229149-194b-4235-baa1-b1086ad8d184\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"b235ba8f-0cfc-496f-b26d-6e657095d8b7\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1755adb5-d45b-421b-9806-3e17feb62a89\",\"type\":\"Circle\"},\"selection_glyph\":null,\"view\":{\"id\":\"f7d9199a-9ecb-4752-ba56-f4e210c2e843\",\"type\":\"CDSView\"}},\"id\":\"041103c1-160c-4250-804c-94f97df648cc\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"8eb292a1-03e6-41bc-9a7c-6497dfbbd5f0\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"da339d7e-733d-4071-8ce6-1235517ff909\",\"type\":\"LinearScale\"},{\"attributes\":{\"below\":[{\"id\":\"ae69af44-7666-4180-8bdb-ddeded47788c\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"b3dc20f4-43bd-44eb-b917-b1643d8cb847\",\"type\":\"LinearAxis\"}],\"renderers\":[{\"id\":\"ae69af44-7666-4180-8bdb-ddeded47788c\",\"type\":\"LinearAxis\"},{\"id\":\"f0fdc383-3b0d-4ef7-8435-94173adce6ee\",\"type\":\"Grid\"},{\"id\":\"b3dc20f4-43bd-44eb-b917-b1643d8cb847\",\"type\":\"LinearAxis\"},{\"id\":\"c39940f0-bb3a-482d-b601-8e876b4d6eca\",\"type\":\"Grid\"},{\"id\":\"da5efdfe-6ea5-490f-be34-b58d6d9fdf6e\",\"type\":\"TileRenderer\"},{\"id\":\"041103c1-160c-4250-804c-94f97df648cc\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"29066b59-97fe-44f3-8e29-8a666d1a6598\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"370f281e-166e-41bc-b86f-f70414177146\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"0c0ea3c4-8bce-4d22-a833-5e447269401b\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"8eb292a1-03e6-41bc-9a7c-6497dfbbd5f0\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"d1c5882c-c286-48db-8270-30073e11441f\",\"type\":\"Range1d\"},\"y_scale\":{\"id\":\"da339d7e-733d-4071-8ce6-1235517ff909\",\"type\":\"LinearScale\"}},\"id\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"formatter\":{\"id\":\"3462adaf-986d-416b-bcae-ebdf2d50d116\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"b3ddc447-d8e3-4f05-9b31-e955984a7e8a\",\"type\":\"BasicTicker\"},\"visible\":false},\"id\":\"b3dc20f4-43bd-44eb-b917-b1643d8cb847\",\"type\":\"LinearAxis\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"98e9ba8c-623c-4c46-9050-9ca36891da0e\",\"type\":\"PanTool\"},{\"id\":\"5a41a513-622a-44a9-8fee-aca38a5d8964\",\"type\":\"WheelZoomTool\"}]},\"id\":\"370f281e-166e-41bc-b86f-f70414177146\",\"type\":\"Toolbar\"},{\"attributes\":{\"attribution\":\"Tiles by Carto, under CC BY 3.0. Data by OSM, under ODbL\",\"url\":\"http://a.basemaps.cartocdn.com/dark_all/{Z}/{X}/{Y}.png\"},\"id\":\"3db6beb5-5153-47ca-bd8a-2e11536b3506\",\"type\":\"WMTSTileSource\"},{\"attributes\":{\"plot\":null,\"text\":\"\"},\"id\":\"29066b59-97fe-44f3-8e29-8a666d1a6598\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"5a41a513-622a-44a9-8fee-aca38a5d8964\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"callback\":null,\"end\":6455972,\"start\":2698291},\"id\":\"d1c5882c-c286-48db-8270-30073e11441f\",\"type\":\"Range1d\"},{\"attributes\":{\"source\":{\"id\":\"80229149-194b-4235-baa1-b1086ad8d184\",\"type\":\"ColumnDataSource\"}},\"id\":\"f7d9199a-9ecb-4752-ba56-f4e210c2e843\",\"type\":\"CDSView\"},{\"attributes\":{\"plot\":{\"id\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"46eeb80e-de80-4faa-b156-11ff00b53218\",\"type\":\"BasicTicker\"}},\"id\":\"f0fdc383-3b0d-4ef7-8435-94173adce6ee\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"dcf83eb1-528f-44f4-854e-863496e476aa\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":10},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1755adb5-d45b-421b-9806-3e17feb62a89\",\"type\":\"Circle\"},{\"attributes\":{\"callback\":null,\"end\":-7453304,\"start\":-13884029},\"id\":\"0c0ea3c4-8bce-4d22-a833-5e447269401b\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"98e9ba8c-623c-4c46-9050-9ca36891da0e\",\"type\":\"PanTool\"},{\"attributes\":{\"formatter\":{\"id\":\"dcf83eb1-528f-44f4-854e-863496e476aa\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"46eeb80e-de80-4faa-b156-11ff00b53218\",\"type\":\"BasicTicker\"},\"visible\":false},\"id\":\"ae69af44-7666-4180-8bdb-ddeded47788c\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"b3ddc447-d8e3-4f05-9b31-e955984a7e8a\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"46eeb80e-de80-4faa-b156-11ff00b53218\",\"type\":\"BasicTicker\"}],\"root_ids\":[\"b2b0f481-5cb8-4a45-a117-60c391b918de\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.10\"}};\n", " var render_items = [{\"docid\":\"750c563e-2223-4574-8a22-b075fadb2d78\",\"elementid\":\"52432c3b-009c-4f6a-b915-d6dc2bd73667\",\"modelid\":\"b2b0f481-5cb8-4a45-a117-60c391b918de\"}];\n", "\n", " root.Bokeh.embed.embed_items(docs_json, render_items);\n", " }\n", "\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " var attempts = 0;\n", " var timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " clearInterval(timer);\n", " }\n", " attempts++;\n", " if (attempts > 100) {\n", " console.log(\"Bokeh: ERROR: Unable to embed document because BokehJS library is missing\")\n", " clearInterval(timer);\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "b2b0f481-5cb8-4a45-a117-60c391b918de" } }, "output_type": "display_data" } ], "source": [ "p.circle(x=df['x'], y=df['y'], fill_color='orange', size=10)\n", "show(p)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# EXERCISE: find some data in lat, lon (e.g. at http://data.gov), \n", "# import it into a dataframe or data source, and add it on the map above.\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## GeoJSON" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 1 }