{"id":1108448,"date":"2025-01-08T17:00:25","date_gmt":"2025-01-08T09:00:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108448.html"},"modified":"2025-01-08T17:00:27","modified_gmt":"2025-01-08T09:00:27","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e5%9c%b0%e5%9b%be%e4%b8%8a%e5%81%9a%e8%b7%af%e7%ba%bf%e5%9b%be-python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108448.html","title":{"rendered":"\u5982\u4f55\u5728\u5730\u56fe\u4e0a\u505a\u8def\u7ebf\u56fe python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072105\/87960491-3d7e-4ced-9f32-cf1997e65d8b.webp\" alt=\"\u5982\u4f55\u5728\u5730\u56fe\u4e0a\u505a\u8def\u7ebf\u56fe python\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5728\u5730\u56fe\u4e0a\u5236\u4f5c\u8def\u7ebf\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\uff0c\u5982Matplotlib\u3001Folium\u3001Geopandas\u7b49\u3002\u9996\u5148\uff0c\u6211\u4eec\u5c06\u76f4\u63a5\u56de\u7b54\u6b64\u95ee\u9898\uff1a\u4f7f\u7528Python\u5728\u5730\u56fe\u4e0a\u7ed8\u5236\u8def\u7ebf\u56fe\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Folium\u3001Geopandas\u3001Matplotlib\u7b49\u5e93\u5b9e\u73b0\uff0c\u901a\u8fc7\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u5730\u56fe\u4e0a\u6807\u8bb0\u8def\u5f84\u3001\u6dfb\u52a0\u6807\u8bb0\u3001\u7ed8\u5236\u591a\u8fb9\u5f62\u7b49\u3002\u8fd9\u91cc\u8be6\u7ec6\u4ecb\u7ecdFolium\u5e93\uff0c\u5b83\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5730\u56fe\u53ef\u89c6\u5316\u5e93\uff0c\u80fd\u591f\u4e0eLeaflet.js\u7ed3\u5408\u4f7f\u7528\uff0c\u521b\u5efa\u4ea4\u4e92\u5f0f\u5730\u56fe\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Folium\u5e93\u5728\u5730\u56fe\u4e0a\u7ed8\u5236\u8def\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/strong><\/h2>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5fc5\u8981\u7684\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install folium<\/p>\n<p>pip install geopandas<\/p>\n<p>pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5bfc\u5165\u8fd9\u4e9b\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import folium<\/p>\n<p>import geopandas as gpd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528Folium\u5e93\u521b\u5efa\u57fa\u672c\u5730\u56fe<\/strong><\/h2>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u5730\u56fe\u5bf9\u8c61\uff0c\u6307\u5b9a\u4e2d\u5fc3\u70b9\u548c\u7f29\u653e\u7ea7\u522b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5730\u56fe\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u4e2d\u5fc3\u70b9\u548c\u7f29\u653e\u7ea7\u522b<\/p>\n<p>map_center = [37.7749, -122.4194]  # \u4ee5\u65e7\u91d1\u5c71\u4e3a\u4f8b<\/p>\n<p>map_zoom_start = 13<\/p>\n<p>map = folium.Map(location=map_center, zoom_start=map_zoom_start)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u5728\u5730\u56fe\u4e0a\u6dfb\u52a0\u8def\u7ebf<\/strong><\/h2>\n<p><p>\u4e3a\u4e86\u5728\u5730\u56fe\u4e0a\u7ed8\u5236\u8def\u7ebf\uff0c\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u8def\u7ebf\u7684\u5750\u6807\u3002\u53ef\u4ee5\u662f\u7ecf\u7eac\u5ea6\u5217\u8868\u6216\u8005\u8bfb\u53d6GeoJSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u8def\u7ebf\u7684\u7ecf\u7eac\u5ea6\u5750\u6807<\/p>\n<p>route_coords = [<\/p>\n<p>    [37.7749, -122.4194],  # \u51fa\u53d1\u70b9<\/p>\n<p>    [37.7849, -122.4294],<\/p>\n<p>    [37.7949, -122.4394],<\/p>\n<p>    [37.8049, -122.4494]   # \u7ec8\u70b9<\/p>\n<p>]<\/p>\n<h2><strong>\u5728\u5730\u56fe\u4e0a\u6dfb\u52a0\u8def\u7ebf<\/strong><\/h2>\n<p>folium.PolyLine(route_coords, color=&quot;blue&quot;, weight=2.5, opacity=1).add_to(map)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5728\u8def\u7ebf\u56fe\u4e0a\u6dfb\u52a0\u6807\u8bb0<\/strong><\/h2>\n<p><p>\u53ef\u4ee5\u5728\u8def\u7ebf\u7684\u8d77\u70b9\u3001\u7ec8\u70b9\u6216\u8005\u91cd\u8981\u8282\u70b9\u6dfb\u52a0\u6807\u8bb0\uff0c\u4ee5\u4fbf\u66f4\u6e05\u6670\u5730\u663e\u793a\u8def\u7ebf\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u8d77\u70b9\u548c\u7ec8\u70b9\u6807\u8bb0<\/p>\n<p>folium.Marker(location=route_coords[0], popup=&quot;Start&quot;, icon=folium.Icon(color=&#39;green&#39;)).add_to(map)<\/p>\n<p>folium.Marker(location=route_coords[-1], popup=&quot;End&quot;, icon=folium.Icon(color=&#39;red&#39;)).add_to(map)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u4fdd\u5b58\u548c\u663e\u793a\u5730\u56fe<\/strong><\/h2>\n<p><p>\u5b8c\u6210\u8def\u7ebf\u56fe\u7684\u521b\u5efa\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5730\u56fe\u4fdd\u5b58\u4e3aHTML\u6587\u4ef6\uff0c\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u67e5\u770b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u5730\u56fe\u5230HTML\u6587\u4ef6<\/p>\n<p>map.save(&#39;route_map.html&#39;)<\/p>\n<h2><strong>\u663e\u793a\u5730\u56fe<\/strong><\/h2>\n<p>map<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528Geopandas\u548cMatplotlib\u8fdb\u884c\u9ad8\u7ea7\u53ef\u89c6\u5316<\/strong><\/h2>\n<p><p>\u9664\u4e86Folium\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528Geopandas\u548cMatplotlib\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u5730\u7406\u7a7a\u95f4\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6GeoJSON\u6570\u636e<\/p>\n<p>gdf = gpd.read_file(&#39;path_to_geojson_file.geojson&#39;)<\/p>\n<h2><strong>\u4f7f\u7528Geopandas\u7ed8\u5236\u5730\u56fe<\/strong><\/h2>\n<p>gdf.plot()<\/p>\n<h2><strong>\u6dfb\u52a0\u8def\u7ebf<\/strong><\/h2>\n<p>plt.plot([37.7749, 37.7849, 37.7949, 37.8049], [-122.4194, -122.4294, -122.4394, -122.4494], color=&#39;blue&#39;)<\/p>\n<h2><strong>\u663e\u793a\u5730\u56fe<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u5728\u5730\u56fe\u4e0a\u7ed8\u5236\u8def\u7ebf\u56fe\u7684\u8be6\u7ec6\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<strong>\u901a\u8fc7\u4f7f\u7528Folium\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u4ea4\u4e92\u5f0f\u5730\u56fe\uff0c\u6dfb\u52a0\u8def\u7ebf\u548c\u6807\u8bb0\u3002Geopandas\u548cMatplotlib\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5730\u7406\u7a7a\u95f4\u6570\u636e\u53ef\u89c6\u5316\u80fd\u529b\uff0c\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5730\u56fe\u7ed8\u5236\u9700\u6c42\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5de5\u5177\uff0c\u521b\u5efa\u4e30\u5bcc\u7684\u5730\u56fe\u53ef\u89c6\u5316\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u53c2\u8003\u8d44\u6599<\/strong><\/h2>\n<ol>\n<li>Folium\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/python-visualization.github.io\/folium\/\">https:\/\/python-visualization.github.io\/folium\/<\/a><\/li>\n<li>Geopandas\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/geopandas.org\/\">https:\/\/geopandas.org\/<\/a><\/li>\n<li>Matplotlib\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/matplotlib.org\/\">https:\/\/matplotlib.org\/<\/a><\/li>\n<\/ol>\n<p><p>\u8fd9\u4e9b\u53c2\u8003\u8d44\u6599\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684API\u4f7f\u7528\u8bf4\u660e\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u5e2e\u52a9\u66f4\u6df1\u5165\u5730\u4e86\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u8def\u7ebf\u56fe\u7684\u57fa\u7840\u5e93\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u8def\u7ebf\u56fe\u7684\u5e38\u7528\u5e93\u5305\u62ecMatplotlib\u3001Folium\u548cGeopandas\u3002Matplotlib\u9002\u5408\u8fdb\u884c\u57fa\u7840\u7684\u56fe\u5f62\u7ed8\u5236\uff0cFolium\u5219\u4e13\u6ce8\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u5730\u56fe\uff0cGeopandas\u9002\u5408\u5904\u7406\u5730\u7406\u6570\u636e\u5e76\u53ef\u4e0ePandas\u7ed3\u5408\u4f7f\u7528\u3002\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u7ed8\u56fe\u6548\u679c\u4e0e\u4ea4\u4e92\u6027\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u54ea\u4e9b\u6570\u636e\u6765\u7ed8\u5236\u8def\u7ebf\u56fe\uff1f<\/strong><br \/>\u7ed8\u5236\u8def\u7ebf\u56fe\u901a\u5e38\u9700\u8981\u8d77\u70b9\u548c\u7ec8\u70b9\u7684\u5730\u7406\u5750\u6807\uff08\u7ecf\u7eac\u5ea6\uff09\uff0c\u4ee5\u53ca\u53ef\u80fd\u7ecf\u8fc7\u7684\u4e2d\u95f4\u70b9\u7684\u5750\u6807\u3002\u53ef\u4ee5\u901a\u8fc7API\uff08\u5982Google Maps API\u6216OpenStreetMap\uff09\u83b7\u53d6\u8fd9\u4e9b\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528CSV\u6587\u4ef6\u7b49\u683c\u5f0f\u5b58\u50a8\u8def\u7ebf\u6570\u636e\u3002\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u5b8c\u6574\u6027\u662f\u7ed8\u5236\u6e05\u6670\u8def\u7ebf\u56fe\u7684\u5173\u952e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5730\u56fe\u4e0a\u6dfb\u52a0\u6807\u8bb0\u548c\u4fe1\u606f\u6846\uff1f<\/strong><br \/>\u5728Folium\u5e93\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Marker\u7c7b\u6765\u6dfb\u52a0\u6807\u8bb0\uff0c\u4f7f\u7528Popup\u7c7b\u6765\u521b\u5efa\u4fe1\u606f\u6846\u3002\u901a\u8fc7\u8bbe\u7f6e\u6807\u8bb0\u7684\u5750\u6807\u548c\u4fe1\u606f\u6846\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u5728\u5730\u56fe\u4e0a\u76f4\u89c2\u5730\u5c55\u793a\u76f8\u5173\u4fe1\u606f\u3002\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u53ef\u4ee5\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\uff0c\u4f7f\u5f97\u5730\u56fe\u4e0d\u4ec5\u4ec5\u662f\u8def\u7ebf\u7684\u5c55\u793a\uff0c\u8fd8\u80fd\u63d0\u4f9b\u66f4\u591a\u80cc\u666f\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5730\u56fe\u4e0a\u81ea\u5b9a\u4e49\u8def\u7ebf\u7684\u6837\u5f0f\uff1f<\/strong><br \/>\u901a\u8fc7Folium\u6216Matplotlib\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u8def\u7ebf\u7684\u989c\u8272\u3001\u5bbd\u5ea6\u548c\u6837\u5f0f\u3002Folium\u7684PolyLine\u5bf9\u8c61\u5141\u8bb8\u7528\u6237\u8bbe\u7f6e\u7ebf\u6761\u7684\u989c\u8272\u548c\u5bbd\u5ea6\uff0c\u800cMatplotlib\u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u4e30\u5bcc\u7684\u56fe\u5f62\u5b9a\u5236\u9009\u9879\uff0c\u5305\u62ec\u7ebf\u578b\u3001\u70b9\u6837\u5f0f\u7b49\u3002\u8fd9\u6837\u7684\u81ea\u5b9a\u4e49\u53ef\u4ee5\u4f7f\u8def\u7ebf\u56fe\u66f4\u52a0\u7f8e\u89c2\u548c\u7b26\u5408\u4e2a\u4eba\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5728\u5730\u56fe\u4e0a\u5236\u4f5c\u8def\u7ebf\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\uff0c\u5982Matplotlib\u3001Folium\u3001Geopandas\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1108454,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108448"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1108448"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108448\/revisions"}],"predecessor-version":[{"id":1108455,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108448\/revisions\/1108455"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108454"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}