{"id":1091488,"date":"2025-01-08T14:12:10","date_gmt":"2025-01-08T06:12:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091488.html"},"modified":"2025-01-08T14:12:12","modified_gmt":"2025-01-08T06:12:12","slug":"python%e5%9c%b0%e5%9b%be%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e4%b8%a4%e4%b8%aa%e5%9f%8e%e5%b8%82-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091488.html","title":{"rendered":"python\u5730\u56fe\u5982\u4f55\u663e\u793a\u4e24\u4e2a\u57ce\u5e02"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203643\/d8fc6db5-fab3-4ec2-9129-b64982635586.webp\" alt=\"python\u5730\u56fe\u5982\u4f55\u663e\u793a\u4e24\u4e2a\u57ce\u5e02\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u663e\u793a\u4e24\u4e2a\u57ce\u5e02\u7684\u5730\u56fe<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u663e\u793a\u4e24\u4e2a\u57ce\u5e02\u7684\u5730\u56fe\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u5de5\u5177\u5305\u62ecFolium\u3001Matplotlib\u548cBasemap\u7b49\u3002<strong>\u4f7f\u7528Folium\u3001\u4f7f\u7528Matplotlib\u548cBasemap\u3001\u4f7f\u7528Geopy\u8fdb\u884c\u5730\u7406\u7f16\u7801<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Folium\u5e93\u6765\u5b9e\u73b0\u8be5\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86Folium\u548cGeopy\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install folium geopy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import folium<\/p>\n<p>from geopy.geocoders import Nominatim<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Folium<\/h3>\n<\/p>\n<p><p>Folium\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u57fa\u4e8eLeaflet.js\u7684\u4ea4\u4e92\u5f0f\u5730\u56fe\u3002\u6211\u4eec\u5c06\u4f7f\u7528Folium\u6765\u663e\u793a\u4e24\u4e2a\u57ce\u5e02\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u5730\u56fe\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5730\u56fe\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5730\u56fe\u5bf9\u8c61\uff0c\u521d\u59cb\u4e2d\u5fc3\u70b9\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u4efb\u610f\u4f4d\u7f6e\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u4e3a\u5168\u7403\u4e2d\u5fc3<\/p>\n<p>map = folium.Map(location=[20, 0], zoom_start=2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u57ce\u5e02\u5750\u6807<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Geopy\u5e93\u83b7\u53d6\u57ce\u5e02\u7684\u5730\u7406\u5750\u6807\u3002Geopy\u662f\u4e00\u4e2aPython\u5e93\uff0c\u7528\u4e8e\u5bf9\u5730\u7406\u4f4d\u7f6e\u8fdb\u884c\u5730\u7406\u7f16\u7801\uff08\u4ece\u5730\u5740\u83b7\u53d6\u5750\u6807\uff09\u548c\u53cd\u5411\u5730\u7406\u7f16\u7801\uff08\u4ece\u5750\u6807\u83b7\u53d6\u5730\u5740\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">geolocator = Nominatim(user_agent=&quot;geoapiExercises&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u57ce\u5e02\u5750\u6807<\/strong><\/h2>\n<p>city1 = geolocator.geocode(&quot;New York, USA&quot;)<\/p>\n<p>city2 = geolocator.geocode(&quot;London, UK&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u5750\u6807\u4fe1\u606f<\/strong><\/h2>\n<p>location1 = [city1.latitude, city1.longitude]<\/p>\n<p>location2 = [city2.latitude, city2.longitude]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5728\u5730\u56fe\u4e0a\u6dfb\u52a0\u57ce\u5e02\u6807\u8bb0<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Folium\u5728\u5730\u56fe\u4e0a\u6dfb\u52a0\u57ce\u5e02\u6807\u8bb0\u548c\u5f39\u51fa\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u57ce\u5e02\u6807\u8bb0<\/p>\n<p>folium.Marker(location1, popup=&quot;New York, USA&quot;).add_to(map)<\/p>\n<p>folium.Marker(location2, popup=&quot;London, UK&quot;).add_to(map)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u663e\u793a\u5730\u56fe<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u663e\u793a\u5730\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u5730\u56fe\u5230HTML\u6587\u4ef6<\/p>\n<p>map.save(&quot;map.html&quot;)<\/p>\n<h2><strong>\u5728Jupyter Notebook\u4e2d\u663e\u793a\u5730\u56fe<\/strong><\/h2>\n<p>map<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Matplotlib\u548cBasemap<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Folium\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u548cBasemap\u6765\u7ed8\u5236\u5730\u56fe\u3002Basemap\u662fMatplotlib\u7684\u4e00\u4e2a\u9644\u52a0\u5de5\u5177\u5305\uff0c\u7528\u4e8e\u7ed8\u5236\u5730\u7406\u5730\u56fe\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u548cBasemap\u663e\u793a\u4e24\u4e2a\u57ce\u5e02\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from mpl_toolkits.basemap import Basemap<\/p>\n<h2><strong>\u521b\u5efa\u5730\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots(figsize=(10, 8))<\/p>\n<p>m = Basemap(projection=&#39;merc&#39;, llcrnrlat=-60, urcrnrlat=90,<\/p>\n<p>            llcrnrlon=-180, urcrnrlon=180, resolution=&#39;c&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u5730\u56fe<\/strong><\/h2>\n<p>m.drawcoastlines()<\/p>\n<p>m.drawcountries()<\/p>\n<p>m.drawmapboundary(fill_color=&#39;aqua&#39;)<\/p>\n<p>m.fillcontinents(color=&#39;lightgreen&#39;, lake_color=&#39;aqua&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u57ce\u5e02\u5750\u6807<\/strong><\/h2>\n<p>city1 = (40.7128, -74.0060)  # New York, USA<\/p>\n<p>city2 = (51.5074, -0.1278)   # London, UK<\/p>\n<h2><strong>\u5728\u5730\u56fe\u4e0a\u6dfb\u52a0\u57ce\u5e02\u6807\u8bb0<\/strong><\/h2>\n<p>x1, y1 = m(city1[1], city1[0])<\/p>\n<p>x2, y2 = m(city2[1], city2[0])<\/p>\n<p>m.plot(x1, y1, &#39;bo&#39;, markersize=12)<\/p>\n<p>m.plot(x2, y2, &#39;ro&#39;, markersize=12)<\/p>\n<p>plt.text(x1, y1, &#39; New York, USA&#39;, fontsize=12, ha=&#39;left&#39;, va=&#39;bottom&#39;, color=&#39;b&#39;)<\/p>\n<p>plt.text(x2, y2, &#39; London, UK&#39;, fontsize=12, ha=&#39;left&#39;, va=&#39;bottom&#39;, color=&#39;r&#39;)<\/p>\n<p>plt.title(&quot;Map Showing Two Cities: New York and London&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Geopy\u8fdb\u884c\u5730\u7406\u7f16\u7801<\/h3>\n<\/p>\n<p><p>Geopy\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5c06\u57ce\u5e02\u540d\u79f0\u8f6c\u6362\u4e3a\u5730\u7406\u5750\u6807\uff0c\u8fd9\u662f\u5b9e\u73b0\u5730\u56fe\u6807\u8bb0\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Geopy\u8fdb\u884c\u5730\u7406\u7f16\u7801\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from geopy.geocoders import Nominatim<\/p>\n<h2><strong>\u521b\u5efa\u5730\u7406\u7f16\u7801\u5668\u5bf9\u8c61<\/strong><\/h2>\n<p>geolocator = Nominatim(user_agent=&quot;geoapiExercises&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u57ce\u5e02\u5750\u6807<\/strong><\/h2>\n<p>location1 = geolocator.geocode(&quot;New York, USA&quot;)<\/p>\n<p>location2 = geolocator.geocode(&quot;London, UK&quot;)<\/p>\n<p>print((location1.latitude, location1.longitude))<\/p>\n<p>print((location2.latitude, location2.longitude))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u663e\u793a\u4e24\u4e2a\u57ce\u5e02\u5730\u56fe\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Folium\u3001Matplotlib\u548cBasemap\u5e93\u4ee5\u53caGeopy\u8fdb\u884c\u5730\u7406\u7f16\u7801\u3002<strong>Folium\u5e93\u9002\u5408\u521b\u5efa\u4ea4\u4e92\u5f0f\u5730\u56fe<\/strong>\uff0c<strong>Matplotlib\u548cBasemap\u9002\u5408\u9759\u6001\u5730\u56fe\u7ed8\u5236<\/strong>\uff0c\u800c<strong>Geopy\u5e93\u7528\u4e8e\u83b7\u53d6\u5730\u7406\u5750\u6807<\/strong>\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u663e\u793a\u591a\u4e2a\u57ce\u5e02\u7684\u5730\u56fe\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u5c55\u793a\u57ce\u5e02\u4e4b\u95f4\u7684\u5730\u7406\u4f4d\u7f6e\u5173\u7cfb\uff0c\u5e76\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u4ee5\u6ee1\u8db3\u66f4\u591a\u7684\u5730\u7406\u53ef\u89c6\u5316\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5730\u56fe\u4e0a\u6807\u8bb0\u4e24\u4e2a\u57ce\u5e02\u7684\u4f4d\u7f6e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5730\u7406\u6570\u636e\u53ef\u89c6\u5316\u5e93\u5982Folium\u6216Matplotlib\u6765\u6807\u8bb0\u57ce\u5e02\u3002\u4ee5Folium\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u57ce\u5e02\u7684\u7ecf\u7eac\u5ea6\u521b\u5efa\u6807\u8bb0\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5730\u56fe\u4e0a\u3002\u60a8\u53ea\u9700\u5b89\u88c5Folium\u5e93\uff0c\u521b\u5efa\u4e00\u4e2aMap\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528Marker\u65b9\u6cd5\u6dfb\u52a0\u57ce\u5e02\u6807\u8bb0\uff0c\u6700\u540e\u5c06\u5730\u56fe\u4fdd\u5b58\u4e3aHTML\u6587\u4ef6\u4ee5\u8fdb\u884c\u67e5\u770b\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u5730\u56fe\u663e\u793a\u57ce\u5e02\u4e4b\u95f4\u7684\u8ddd\u79bb\uff1f<\/strong><br \/>\u8981\u663e\u793a\u4e24\u4e2a\u57ce\u5e02\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u53ef\u4ee5\u4f7f\u7528Geopy\u5e93\u6765\u8ba1\u7b97\u4e24\u8005\u7684\u5730\u7406\u5750\u6807\uff0c\u7136\u540e\u5229\u7528Haversine\u516c\u5f0f\u8fdb\u884c\u8ddd\u79bb\u8ba1\u7b97\u3002\u5c06\u8ba1\u7b97\u7ed3\u679c\u5c55\u793a\u5728\u5730\u56fe\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7ed3\u5408Basemap\u8fdb\u884c\u53ef\u89c6\u5316\uff0c\u6216\u8005\u5728Folium\u5730\u56fe\u4e0a\u663e\u793a\u76f8\u5e94\u7684\u6807\u8bb0\u548c\u7ebf\u6761\uff0c\u76f4\u89c2\u5448\u73b0\u57ce\u5e02\u95f4\u7684\u8ddd\u79bb\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u5728Python\u5730\u56fe\u4e0a\u6dfb\u52a0\u57ce\u5e02\u7684\u76f8\u5173\u4fe1\u606f\u5417\uff1f<\/strong><br \/>\u7edd\u5bf9\u53ef\u4ee5\u3002\u60a8\u53ef\u4ee5\u5728Folium\u6216Matplotlib\u4e2d\u4e3a\u6bcf\u4e2a\u57ce\u5e02\u7684\u6807\u8bb0\u6dfb\u52a0\u5f39\u51fa\u7a97\u53e3\uff0c\u663e\u793a\u76f8\u5173\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Popup\u65b9\u6cd5\uff0c\u5728\u70b9\u51fb\u57ce\u5e02\u6807\u8bb0\u65f6\u5f39\u51fa\u57ce\u5e02\u7684\u540d\u79f0\u3001\u4eba\u53e3\u3001\u8457\u540d\u666f\u70b9\u7b49\u4fe1\u606f\u3002\u8fd9\u6837\u7684\u529f\u80fd\u4e0d\u4ec5\u4e30\u5bcc\u4e86\u5730\u56fe\u7684\u4ea4\u4e92\u6027\uff0c\u4e5f\u4f7f\u5f97\u4fe1\u606f\u5c55\u793a\u66f4\u52a0\u751f\u52a8\u548c\u6709\u8da3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u663e\u793a\u4e24\u4e2a\u57ce\u5e02\u7684\u5730\u56fe \u5728Python\u4e2d\u663e\u793a\u4e24\u4e2a\u57ce\u5e02\u7684\u5730\u56fe\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u5de5\u5177\u5305\u62ecF [&hellip;]","protected":false},"author":3,"featured_media":1091490,"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\/1091488"}],"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=1091488"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091488\/revisions"}],"predecessor-version":[{"id":1091492,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091488\/revisions\/1091492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1091490"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1091488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1091488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1091488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}