{"id":930834,"date":"2024-12-26T17:24:49","date_gmt":"2024-12-26T09:24:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/930834.html"},"modified":"2024-12-26T17:24:51","modified_gmt":"2024-12-26T09:24:51","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e6%b5%b7%e6%b4%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/930834.html","title":{"rendered":"python\u5982\u4f55\u753b\u6d77\u6d0b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065404\/18dc9161-b7eb-41ab-99d7-6b0e2263b726.webp\" alt=\"python\u5982\u4f55\u753b\u6d77\u6d0b\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u753b\u6d77\u6d0b\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u5229\u7528Matplotlib\u5e93\u7ed8\u5236\u6d77\u6d0b\u56fe\u5f62\u3001\u4f7f\u7528Basemap\u8fdb\u884c\u5730\u7406\u53ef\u89c6\u5316\u3001\u6216\u501f\u52a9Cartopy\u8fdb\u884c\u5730\u7406\u7a7a\u95f4\u6570\u636e\u7684\u7ed8\u56fe\u3002\u5176\u4e2d\uff0cMatplotlib\u7ed3\u5408Basemap\u6216Cartopy\u662f\u8f83\u4e3a\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u4e0eCartopy\u7ed8\u5236\u6d77\u6d0b\u56fe\u50cf\u3002<\/strong><\/p>\n<\/p>\n<p><p>Cartopy\u662f\u4e00\u4e2a\u4e13\u6ce8\u4e8e\u5730\u7406\u7a7a\u95f4\u6570\u636e\u53ef\u89c6\u5316\u7684Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5de5\u5177\uff0c\u7528\u4e8e\u521b\u5efa\u57fa\u4e8e\u5730\u56fe\u6295\u5f71\u7684\u56fe\u5f62\u3002\u7ed3\u5408Matplotlib\uff0cCartopy\u80fd\u591f\u9ad8\u6548\u5730\u8fdb\u884c\u5730\u7406\u7a7a\u95f4\u6570\u636e\u7684\u7ed8\u56fe\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u8bb2\u89e3\u5982\u4f55\u5229\u7528\u8fd9\u4e24\u4e2a\u5e93\u7ed8\u5236\u6d77\u6d0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u76f8\u5173\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u5fc5\u8981\u7684Python\u5e93\u3002\u4e3b\u8981\u5305\u62ecMatplotlib\u548cCartopy\u3002\u6b64\u5916\uff0c\u7531\u4e8eCartopy\u4f9d\u8d56\u4e8ePROJ\u548cGEOS\u5e93\uff0c\u56e0\u6b64\u5728\u5b89\u88c5Cartopy\u4e4b\u524d\uff0c\u786e\u4fdd\u8fd9\u4e9b\u5e93\u5df2\u7ecf\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p>pip install cartopy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import cartopy.crs as ccrs<\/p>\n<p>import cartopy.feature as cfeature<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u5730\u56fe\u57fa\u7840<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u7ed8\u5236\u6d77\u6d0b\uff0c\u9700\u8981\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5730\u56fe\u57fa\u7840\uff0c\u8fd9\u5305\u62ec\u9009\u62e9\u5730\u56fe\u6295\u5f71\u548c\u8bbe\u7f6e\u5730\u56fe\u7684\u8303\u56f4\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5730\u56fe\u6295\u5f71<\/strong><\/p>\n<\/p>\n<p><p>\u5730\u56fe\u6295\u5f71\u662f\u5c06\u5730\u7403\u8868\u9762\u8f6c\u5316\u4e3a\u5e73\u9762\u56fe\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u5728Cartopy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6295\u5f71\uff0c\u4f8b\u5982Mercator\u3001PlateCarree\u3001Robinson\u7b49\u3002\u5bf9\u4e8e\u6d77\u6d0b\u56fe\u50cf\uff0cMercator\u6295\u5f71\u662f\u5e38\u7528\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig = plt.figure(figsize=(10, 5))<\/p>\n<p>ax = plt.axes(projection=ccrs.Mercator())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u5730\u56fe\u8303\u56f4<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u7ecf\u7eac\u5ea6\u8303\u56f4\u6765\u9650\u5b9a\u5730\u56fe\u7684\u663e\u793a\u533a\u57df\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u8bbe\u7f6e\u5730\u56fe\u663e\u793a\u5168\u7403\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_global()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u6dfb\u52a0\u6d77\u6d0b\u548c\u9646\u5730\u7279\u5f81<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u7ed8\u5236\u6d77\u6d0b\uff0c\u9700\u5728\u5730\u56fe\u4e0a\u6dfb\u52a0\u6d77\u6d0b\u548c\u9646\u5730\u7684\u7279\u5f81\u3002Cartopy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5730\u7406\u7279\u5f81\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u5730\u56fe\u4e0a\u53e0\u52a0\u4e0d\u540c\u7684\u5730\u7406\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0\u6d77\u6d0b\u548c\u9646\u5730<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Cartopy\u7684<code>NaturalEarthFeature<\/code>\u6765\u6dfb\u52a0\u6d77\u6d0b\u548c\u9646\u5730\u7279\u5f81\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.add_feature(cfeature.OCEAN, zorder=1, edgecolor=&#39;k&#39;)<\/p>\n<p>ax.add_feature(cfeature.LAND, zorder=0, edgecolor=&#39;k&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u6d77\u5cb8\u7ebf<\/strong><\/p>\n<\/p>\n<p><p>\u6d77\u5cb8\u7ebf\u662f\u6d77\u6d0b\u5730\u56fe\u4e2d\u4e00\u4e2a\u91cd\u8981\u7684\u5143\u7d20\uff0c\u901a\u8fc7\u6dfb\u52a0\u6d77\u5cb8\u7ebf\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5b9a\u4e49\u6d77\u6d0b\u548c\u9646\u5730\u4e4b\u95f4\u7684\u754c\u9650\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.coastlines(resolution=&#39;110m&#39;, color=&#39;black&#39;, linewidth=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u7ed8\u5236\u6d77\u6d0b\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5730\u56fe\u57fa\u7840\u4e0a\u53e0\u52a0\u6d77\u6d0b\u6570\u636e\u662f\u7ed8\u5236\u6d77\u6d0b\u56fe\u7684\u6838\u5fc3\u6b65\u9aa4\u3002\u53ef\u4ee5\u4f7f\u7528Cartopy\u548cMatplotlib\u7ed8\u5236\u591a\u79cd\u6d77\u6d0b\u6570\u636e\uff0c\u5982\u6d77\u6d0b\u6e29\u5ea6\u3001\u76d0\u5ea6\u3001\u6d0b\u6d41\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ed8\u5236\u6d77\u6d0b\u6e29\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u6d77\u6d0b\u8868\u9762\u6e29\u5ea6\uff08SST\uff09\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7Matplotlib\u7684<code>contourf<\/code>\u51fd\u6570\u5728\u5730\u56fe\u4e0a\u7ed8\u5236\u6e29\u5ea6\u5206\u5e03\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>lons = np.linspace(-180, 180, 360)<\/p>\n<p>lats = np.linspace(-90, 90, 180)<\/p>\n<p>lons, lats = np.meshgrid(lons, lats)<\/p>\n<p>sst = np.sin(3 * np.deg2rad(lats)) + np.cos(2 * np.deg2rad(lons))<\/p>\n<h2><strong>\u7ed8\u5236\u6d77\u6d0b\u6e29\u5ea6<\/strong><\/h2>\n<p>contour = ax.contourf(lons, lats, sst, 60, transform=ccrs.PlateCarree(), cmap=&#39;coolwarm&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u989c\u8272\u6761<\/strong><\/p>\n<\/p>\n<p><p>\u989c\u8272\u6761\u7528\u4e8e\u6307\u793a\u56fe\u4e2d\u989c\u8272\u4e0e\u6570\u636e\u503c\u4e4b\u95f4\u7684\u5bf9\u5e94\u5173\u7cfb\uff0c\u662f\u6570\u636e\u53ef\u89c6\u5316\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cbar = plt.colorbar(contour, orientation=&#39;horizontal&#39;, pad=0.05, aspect=50)<\/p>\n<p>cbar.set_label(&#39;Sea Surface Temperature (\u00b0C)&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6807\u6ce8\u548c\u7f8e\u5316<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u56fe\u5f62\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\uff0c\u53ef\u4ee5\u5bf9\u56fe\u5f62\u8fdb\u884c\u6807\u6ce8\u548c\u7f8e\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0\u7ecf\u7eac\u5ea6\u7f51\u683c<\/strong><\/p>\n<\/p>\n<p><p>\u7ecf\u7eac\u5ea6\u7f51\u683c\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u5b9a\u4f4d\u5730\u56fe\u4e0a\u7684\u7279\u5f81\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6807\u9898\u548c\u6ce8\u91ca<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u56fe\u5f62\u6dfb\u52a0\u6807\u9898\u548c\u5fc5\u8981\u7684\u6ce8\u91ca\uff0c\u80fd\u591f\u4f7f\u8bfb\u8005\u66f4\u5bb9\u6613\u7406\u89e3\u56fe\u5f62\u7684\u5185\u5bb9\u548c\u6570\u636e\u6765\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.title(&#39;Global Sea Surface Temperature&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u4fdd\u5b58\u548c\u5c55\u793a\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u7ed8\u56fe\u540e\uff0c\u53ef\u4ee5\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\u6216\u76f4\u63a5\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.savefig(&#39;ocean_plot.png&#39;, dpi=300, bbox_inches=&#39;tight&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u548cCartopy\u7ed8\u5236\u51fa\u5177\u6709\u6d77\u6d0b\u7279\u5f81\u7684\u5730\u56fe\uff0c\u5e76\u5728\u5730\u56fe\u4e0a\u53e0\u52a0\u6d77\u6d0b\u6570\u636e\uff0c\u5982\u6e29\u5ea6\u3001\u76d0\u5ea6\u3001\u6d0b\u6d41\u7b49\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u7ed8\u5236\u7b80\u5355\u7684\u6d77\u6d0b\u56fe\u50cf\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5730\u7406\u7a7a\u95f4\u6570\u636e\u53ef\u89c6\u5316\u3002\u968f\u7740\u60a8\u5bf9Cartopy\u548cMatplotlib\u7684\u8fdb\u4e00\u6b65\u4e86\u89e3\uff0c\u53ef\u4ee5\u521b\u5efa\u51fa\u66f4\u590d\u6742\u3001\u66f4\u7cbe\u7f8e\u7684\u6d77\u6d0b\u56fe\u50cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u6d77\u6d0b\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7ed8\u5236\u6d77\u6d0b\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\uff0c\u5982Matplotlib\u548cNumPy\uff0c\u521b\u5efa\u4e00\u4e2a\u753b\u5e03\uff0c\u5b9a\u4e49\u6d77\u6d0b\u7684\u989c\u8272\u548c\u6ce2\u6d6a\u5f62\u72b6\uff0c\u5e76\u901a\u8fc7\u7ed8\u5236\u51fd\u6570\u5c06\u5176\u5448\u73b0\u51fa\u6765\u3002\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684plot\u51fd\u6570\u6765\u7ed8\u5236\u6ce2\u6d6a\uff0c\u901a\u8fc7\u8c03\u6574\u6ce2\u6d6a\u7684\u53c2\u6570\u6765\u6a21\u62df\u4e0d\u540c\u7684\u6d77\u6d0b\u6548\u679c\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u6765\u7ed8\u5236\u6d77\u6d0b\u56fe\u50cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecMatplotlib\u3001NumPy\u548cPillow\u3002Matplotlib\u9002\u5408\u7ed8\u52362D\u56fe\u5f62\uff0cNumPy\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u6570\u636e\u6570\u7ec4\uff0c\u800cPillow\u5219\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u751f\u6210\u66f4\u590d\u6742\u7684\u56fe\u50cf\u6548\u679c\u3002\u7ed3\u5408\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u51fa\u4e30\u5bcc\u591a\u5f69\u7684\u6d77\u6d0b\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u6dfb\u52a0\u52a8\u6001\u6548\u679c\u4f7f\u6d77\u6d0b\u770b\u8d77\u6765\u66f4\u903c\u771f\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f7f\u6d77\u6d0b\u770b\u8d77\u6765\u66f4\u52a0\u751f\u52a8\uff0c\u53ef\u4ee5\u5f15\u5165\u52a8\u753b\u6548\u679c\u3002\u4f7f\u7528Matplotlib\u7684FuncAnimation\u529f\u80fd\uff0c\u53ef\u4ee5\u521b\u5efa\u6ce2\u6d6a\u52a8\u6001\u53d8\u5316\u7684\u6548\u679c\u3002\u901a\u8fc7\u4e0d\u65ad\u66f4\u65b0\u6ce2\u6d6a\u7684\u53c2\u6570\uff0c\u6a21\u62df\u6d77\u6c34\u7684\u8d77\u4f0f\uff0c\u53ef\u4ee5\u6709\u6548\u589e\u5f3a\u89c6\u89c9\u4f53\u9a8c\u3002\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u5e93\uff0c\u5982Pygame\uff0c\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u52a8\u753b\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u753b\u6d77\u6d0b\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u5229\u7528Matplotlib\u5e93\u7ed8\u5236\u6d77\u6d0b\u56fe\u5f62\u3001\u4f7f\u7528Basemap\u8fdb\u884c\u5730 [&hellip;]","protected":false},"author":3,"featured_media":930837,"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\/930834"}],"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=930834"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/930834\/revisions"}],"predecessor-version":[{"id":930839,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/930834\/revisions\/930839"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/930837"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=930834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=930834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=930834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}