{"id":1098727,"date":"2025-01-08T15:23:47","date_gmt":"2025-01-08T07:23:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098727.html"},"modified":"2025-01-08T15:23:51","modified_gmt":"2025-01-08T07:23:51","slug":"%e5%88%a0%e6%a0%bc%e5%8c%96%e5%9c%b0%e5%9b%be%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098727.html","title":{"rendered":"\u5220\u683c\u5316\u5730\u56fe\u5982\u4f55\u7528python\u753b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062804\/4c15370f-4c0e-46d8-abd1-26b81c3d25f8.webp\" alt=\"\u5220\u683c\u5316\u5730\u56fe\u5982\u4f55\u7528python\u753b\" \/><\/p>\n<p><p> \u5220\u683c\u5316\u5730\u56fe\u5728\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\uff08GIS\uff09\u548c\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u663e\u793a\u5730\u7406\u6570\u636e\u7684\u6a21\u5f0f\u548c\u8d8b\u52bf\u3002\u4f7f\u7528Python\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\u5982Matplotlib\u3001Geopandas\u548cNumpy\u6765\u5b9e\u73b0\u5220\u683c\u5316\u5730\u56fe\u7684\u7ed8\u5236\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u5220\u683c\u5316\u5730\u56fe\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p><strong>\u8981\u7ed8\u5236\u5220\u683c\u5316\u5730\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u79cd\u5e93\uff0c\u5982Matplotlib\u3001Geopandas\u3001Numpy\u3001Pandas\u7b49\u3002\u9996\u5148\uff0c\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff0c\u7136\u540e\u52a0\u8f7d\u6570\u636e\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u6700\u540e\u4f7f\u7528Matplotlib\u548cGeopandas\u8fdb\u884c\u53ef\u89c6\u5316\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5fc5\u8981\u7684Python\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy pandas geopandas matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u52a0\u8f7d\u548c\u5904\u7406\u6570\u636e<\/p>\n<\/p>\n<p><p>1\u3001\u52a0\u8f7d\u5730\u7406\u6570\u636e<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u5730\u7406\u6570\u636e\u3002\u5730\u7406\u6570\u636e\u901a\u5e38\u4ee5Shapefile\u683c\u5f0f\u5b58\u50a8\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Geopandas\u6765\u8bfb\u53d6Shapefile\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import geopandas as gpd<\/p>\n<h2><strong>\u8bfb\u53d6Shapefile\u6587\u4ef6<\/strong><\/h2>\n<p>shapefile_path = &#39;path_to_shapefile.shp&#39;<\/p>\n<p>gdf = gpd.read_file(shapefile_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u52a0\u8f7d\u5c5e\u6027\u6570\u636e<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u5305\u542b\u6211\u4eec\u611f\u5174\u8da3\u7684\u5c5e\u6027\u6570\u636e\u7684\u6587\u4ef6\u3002\u901a\u5e38\uff0c\u8fd9\u4e9b\u6570\u636e\u5b58\u50a8\u5728CSV\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>csv_path = &#39;path_to_data.csv&#39;<\/p>\n<p>df = pd.read_csv(csv_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u5408\u5e76\u5730\u7406\u6570\u636e\u548c\u5c5e\u6027\u6570\u636e<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u9700\u8981\u5c06\u5730\u7406\u6570\u636e\u548c\u5c5e\u6027\u6570\u636e\u5408\u5e76\u5728\u4e00\u8d77\u3002\u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u6839\u636e\u67d0\u4e2a\u5171\u540c\u7684\u5b57\u6bb5\uff08\u5982\u5730\u7406\u5355\u5143\u7684ID\uff09\u8fdb\u884c\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u5730\u7406\u5355\u5143\u7684ID\u5b57\u6bb5\u4e3a&#39;ID&#39;<\/p>\n<p>merged_gdf = gdf.merge(df, on=&#39;ID&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u521b\u5efa\u5220\u683c\u5316\u5730\u56fe<\/p>\n<\/p>\n<p><p>1\u3001\u5b9a\u4e49\u5220\u683c\u5316\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u751f\u6210\u5220\u683c\u5316\u5730\u56fe\u3002\u8fd9\u4e2a\u51fd\u6570\u5c06\u4f7f\u7528Matplotlib\u6765\u7ed8\u5236\u5730\u56fe\uff0c\u5e76\u4f7f\u7528Geopandas\u6765\u5904\u7406\u5730\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p>def create_grid_map(gdf, attribute, grid_size):<\/p>\n<p>    # \u83b7\u53d6\u5730\u7406\u6570\u636e\u7684\u8fb9\u754c<\/p>\n<p>    bounds = gdf.total_bounds<\/p>\n<p>    xmin, ymin, xmax, ymax = bounds<\/p>\n<p>    # \u521b\u5efa\u7f51\u683c<\/p>\n<p>    x_coords = np.arange(xmin, xmax, grid_size)<\/p>\n<p>    y_coords = np.arange(ymin, ymax, grid_size)<\/p>\n<p>    grid = np.meshgrid(x_coords, y_coords)<\/p>\n<p>    # \u521b\u5efa\u7a7a\u56fe<\/p>\n<p>    fig, ax = plt.subplots(figsize=(10, 10))<\/p>\n<p>    # \u7ed8\u5236\u5730\u7406\u6570\u636e\u7684\u8fb9\u754c<\/p>\n<p>    gdf.boundary.plot(ax=ax, linewidth=1, color=&#39;black&#39;)<\/p>\n<p>    # \u904d\u5386\u7f51\u683c\u5e76\u586b\u5145\u989c\u8272<\/p>\n<p>    for i in range(len(x_coords) - 1):<\/p>\n<p>        for j in range(len(y_coords) - 1):<\/p>\n<p>            xmin, xmax = x_coords[i], x_coords[i + 1]<\/p>\n<p>            ymin, ymax = y_coords[j], y_coords[j + 1]<\/p>\n<p>            # \u521b\u5efa\u4e00\u4e2a\u77e9\u5f62<\/p>\n<p>            rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, edgecolor=&#39;none&#39;)<\/p>\n<p>            # \u8ba1\u7b97\u7f51\u683c\u5185\u7684\u5e73\u5747\u5c5e\u6027\u503c<\/p>\n<p>            grid_gdf = gdf.cx[xmin:xmax, ymin:ymax]<\/p>\n<p>            if not grid_gdf.empty:<\/p>\n<p>                mean_value = grid_gdf[attribute].mean()<\/p>\n<p>                # \u6839\u636e\u5e73\u5747\u503c\u8bbe\u7f6e\u989c\u8272<\/p>\n<p>                color = plt.cm.viridis(mean_value \/ gdf[attribute].max())<\/p>\n<p>                rect.set_facecolor(color)<\/p>\n<p>                ax.add_patch(rect)<\/p>\n<p>    # \u663e\u793a\u989c\u8272\u6761<\/p>\n<p>    sm = plt.cm.ScalarMappable(cmap=&#39;viridis&#39;, norm=plt.Normalize(vmin=gdf[attribute].min(), vmax=gdf[attribute].max()))<\/p>\n<p>    sm._A = []<\/p>\n<p>    plt.colorbar(sm, ax=ax)<\/p>\n<p>    plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u8c03\u7528\u5220\u683c\u5316\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b9a\u4e49\u7684\u51fd\u6570\u6765\u521b\u5efa\u5220\u683c\u5316\u5730\u56fe\u3002\u6211\u4eec\u9700\u8981\u4f20\u9012\u5730\u7406\u6570\u636e\u3001\u611f\u5174\u8da3\u7684\u5c5e\u6027\u5b57\u6bb5\u548c\u7f51\u683c\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5220\u683c\u5316\u5730\u56fe<\/p>\n<p>attribute = &#39;population_density&#39;<\/p>\n<p>grid_size = 0.01<\/p>\n<p>create_grid_map(merged_gdf, attribute, grid_size)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u5220\u683c\u5316\u5730\u56fe\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u8c03\u6574\u4ee3\u7801\u4e2d\u7684\u53c2\u6570\u548c\u5b57\u6bb5\u540d\u79f0\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5730\u7406\u6570\u636e\u53ef\u89c6\u5316\uff0c\u5e76\u5206\u6790\u5176\u6a21\u5f0f\u548c\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u5220\u683c\u5316\u5730\u56fe\uff1f<\/strong><br \/>\u7ed8\u5236\u5220\u683c\u5316\u5730\u56fe\u901a\u5e38\u9700\u8981\u4f7f\u7528Python\u7684\u591a\u4e2a\u5e93\uff0c\u5982Matplotlib\u3001Pandas\u548cGeopandas\u7b49\u3002\u4f60\u53ef\u4ee5\u9996\u5148\u5c06\u5730\u7406\u6570\u636e\u8bfb\u5165Pandas DataFrame\uff0c\u7136\u540e\u5229\u7528Geopandas\u8fdb\u884c\u7a7a\u95f4\u6570\u636e\u5904\u7406\uff0c\u6700\u540e\u4f7f\u7528Matplotlib\u5b9e\u73b0\u53ef\u89c6\u5316\u3002\u786e\u4fdd\u51c6\u5907\u597d\u5730\u7406\u6570\u636e\uff0c\u5e76\u4e86\u89e3\u57fa\u672c\u7684\u5750\u6807\u7cfb\u7edf\u548c\u6295\u5f71\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u7cbe\u786e\u5730\u663e\u793a\u5730\u56fe\u3002<\/p>\n<p><strong>\u9700\u8981\u54ea\u4e9b\u5e93\u6765\u7ed8\u5236\u5220\u683c\u5316\u5730\u56fe\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u7ed8\u5236\u5220\u683c\u5316\u5730\u56fe\u65f6\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecGeopandas\u7528\u4e8e\u5904\u7406\u5730\u7406\u6570\u636e\uff0cMatplotlib\u548cSeaborn\u7528\u4e8e\u7ed8\u56fe\uff0cNumpy\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0cPandas\u7528\u4e8e\u6570\u636e\u5904\u7406\u3002\u6839\u636e\u9700\u6c42\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u5176\u4ed6\u5e93\uff0c\u4f8b\u5982Folium\u7528\u4e8e\u4ea4\u4e92\u5f0f\u5730\u56fe\u6216Plotly\u7528\u4e8e\u9ad8\u7ea7\u53ef\u89c6\u5316\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5730\u7406\u6570\u636e\u4ee5\u9002\u5408\u5220\u683c\u5316\u5730\u56fe\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u5220\u683c\u5316\u5730\u56fe\u4e4b\u524d\uff0c\u9700\u5bf9\u5730\u7406\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\u8fd9\u5305\u62ec\u6570\u636e\u6e05\u6d17\u3001\u5750\u6807\u8f6c\u6362\u3001\u4ee5\u53ca\u5c06\u6570\u636e\u5212\u5206\u4e3a\u9002\u5f53\u7684\u7f51\u683c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Geopandas\u4e2d\u7684<code>GeoDataFrame<\/code>\u6765\u52a0\u8f7d\u548c\u5904\u7406\u5730\u7406\u4fe1\u606f\uff0c\u5229\u7528\u7a7a\u95f4\u5206\u6790\u529f\u80fd\u6765\u521b\u5efa\u9002\u5408\u53ef\u89c6\u5316\u7684\u5220\u683c\u5316\u6570\u636e\u3002\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u548c\u6295\u5f71\u7cfb\u7edf\u6b63\u786e\uff0c\u4ee5\u4fbf\u6709\u6548\u5c55\u793a\u5730\u7406\u7279\u5f81\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5220\u683c\u5316\u5730\u56fe\u5728\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\uff08GIS\uff09\u548c\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u663e\u793a\u5730\u7406\u6570\u636e\u7684\u6a21\u5f0f\u548c\u8d8b\u52bf\u3002\u4f7f\u7528Python\uff0c\u4f60\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":1098736,"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\/1098727"}],"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=1098727"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098727\/revisions"}],"predecessor-version":[{"id":1098737,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098727\/revisions\/1098737"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098736"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}