{"id":1138606,"date":"2025-01-08T22:01:39","date_gmt":"2025-01-08T14:01:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1138606.html"},"modified":"2025-01-08T22:01:41","modified_gmt":"2025-01-08T14:01:41","slug":"python%e5%a6%82%e4%bd%95%e5%85%ad%e8%be%b9%e5%bd%a2%e7%9a%84%e6%9e%81%e5%9d%90%e6%a0%87%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1138606.html","title":{"rendered":"python\u5982\u4f55\u516d\u8fb9\u5f62\u7684\u6781\u5750\u6807\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102151\/78b2be23-8b31-4174-8713-01f8da5fd43b.webp\" alt=\"python\u5982\u4f55\u516d\u8fb9\u5f62\u7684\u6781\u5750\u6807\u56fe\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u521b\u5efa\u516d\u8fb9\u5f62\u7684\u6781\u5750\u6807\u56fe<\/strong>\uff0c\u6709\u51e0\u4e2a\u5173\u952e\u7684\u6b65\u9aa4\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u8bbe\u7f6e\u6781\u5750\u6807\u7cfb\u3001\u8ba1\u7b97\u516d\u8fb9\u5f62\u9876\u70b9\u7684\u6781\u5750\u6807\u3001\u7ed8\u5236\u56fe\u5f62\u3002<strong>\u501f\u52a9matplotlib\u5e93\u8bbe\u7f6e\u6781\u5750\u6807\u7cfb\u3001\u8ba1\u7b97\u516d\u8fb9\u5f62\u9876\u70b9\u7684\u6781\u5750\u6807\u3001\u7ed8\u5236\u56fe\u5f62<\/strong>\u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4f60\u7684Python\u811a\u672c\u6216Jupyter Notebook\u4e2d\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/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><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u6781\u5750\u6807\u7cfb<\/h3>\n<\/p>\n<p><p>\u8981\u7ed8\u5236\u6781\u5750\u6807\u56fe\uff0c\u9700\u8981\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u4e3a\u6781\u5750\u6807\u7cfb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, polar=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8ba1\u7b97\u516d\u8fb9\u5f62\u9876\u70b9\u7684\u6781\u5750\u6807<\/h3>\n<\/p>\n<p><p>\u516d\u8fb9\u5f62\u6709\u516d\u4e2a\u9876\u70b9\uff0c\u6bcf\u4e2a\u9876\u70b9\u7684\u89d2\u5ea6\u95f4\u9694\u4e3a60\u5ea6\uff082\u03c0\/6\uff09\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u6765\u8ba1\u7b97\u8fd9\u4e9b\u9876\u70b9\u7684\u6781\u5750\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">angles = np.linspace(0, 2 * np.pi, 7)  # 7\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u95ed\u5408\u516d\u8fb9\u5f62<\/p>\n<p>radii = np.ones(7)  # \u534a\u5f84\u4e3a1\u7684\u516d\u8fb9\u5f62<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed8\u5236\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528plot\u51fd\u6570\u7ed8\u5236\u516d\u8fb9\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.plot(angles, radii)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u5c55\u793a\u516d\u8fb9\u5f62\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u6837\u5f0f\u548c\u6807\u6ce8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_yticklabels([])  # \u9690\u85cf\u5f84\u5411\u523b\u5ea6<\/p>\n<p>ax.set_xticks(angles[:-1])  # \u8bbe\u7f6e\u89d2\u5ea6\u523b\u5ea6<\/p>\n<p>ax.grid(True)  # \u663e\u793a\u7f51\u683c\u7ebf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u548c\u6781\u5750\u6807\u8f74<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, polar=True)<\/p>\n<h2><strong>\u8ba1\u7b97\u516d\u8fb9\u5f62\u9876\u70b9\u7684\u6781\u5750\u6807<\/strong><\/h2>\n<p>angles = np.linspace(0, 2 * np.pi, 7)  # 7\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u95ed\u5408\u516d\u8fb9\u5f62<\/p>\n<p>radii = np.ones(7)  # \u534a\u5f84\u4e3a1\u7684\u516d\u8fb9\u5f62<\/p>\n<h2><strong>\u7ed8\u5236\u516d\u8fb9\u5f62<\/strong><\/h2>\n<p>ax.plot(angles, radii)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6837\u5f0f\u548c\u6807\u6ce8<\/strong><\/h2>\n<p>ax.set_yticklabels([])  # \u9690\u85cf\u5f84\u5411\u523b\u5ea6<\/p>\n<p>ax.set_xticks(angles[:-1])  # \u8bbe\u7f6e\u89d2\u5ea6\u523b\u5ea6<\/p>\n<p>ax.grid(True)  # \u663e\u793a\u7f51\u683c\u7ebf<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6269\u5c55\u4e0e\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u516d\u8fb9\u5f62\u7684\u6781\u5750\u6807\u56fe\u53ef\u4ee5\u5e94\u7528\u4e8e\u591a\u4e2a\u9886\u57df\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u53ef\u89c6\u5316\u9886\u57df\uff0c\u516d\u8fb9\u5f62\u7684\u6781\u5750\u6807\u56fe\u53ef\u4ee5\u7528\u4e8e\u5c55\u793a\u5468\u671f\u6027\u6570\u636e\uff0c\u4f8b\u5982\u98ce\u901f\u548c\u98ce\u5411\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\uff08GIS\uff09<\/h4>\n<\/p>\n<p><p>\u5728GIS\u9886\u57df\uff0c\u516d\u8fb9\u5f62\u7f51\u683c\u5e38\u7528\u4e8e\u7a7a\u95f4\u6570\u636e\u7684\u79bb\u6563\u5316\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6e38\u620f\u5f00\u53d1<\/h4>\n<\/p>\n<p><p>\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c\u516d\u8fb9\u5f62\u7f51\u683c\u7528\u4e8e\u5730\u56fe\u751f\u6210\u548c\u8def\u5f84\u89c4\u5212\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u516d\u8fb9\u5f62\u7f51\u683c\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u9ad8\u7ea7\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7ed8\u5236\u591a\u4e2a\u516d\u8fb9\u5f62\u4ee5\u5f62\u6210\u4e00\u4e2a\u516d\u8fb9\u5f62\u7f51\u683c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u516d\u8fb9\u5f62\u7f51\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u548c\u6781\u5750\u6807\u8f74<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, polar=True)<\/p>\n<h2><strong>\u51fd\u6570\uff1a\u7ed8\u5236\u5355\u4e2a\u516d\u8fb9\u5f62<\/strong><\/h2>\n<p>def plot_hexagon(center_angle, center_radius, size=1):<\/p>\n<p>    angles = np.linspace(0, 2 * np.pi, 7)<\/p>\n<p>    radii = size * np.ones(7)<\/p>\n<p>    ax.plot(center_angle + angles, center_radius + radii)<\/p>\n<h2><strong>\u7ed8\u5236\u591a\u4e2a\u516d\u8fb9\u5f62\u5f62\u6210\u7f51\u683c<\/strong><\/h2>\n<p>hex_size = 1<\/p>\n<p>for i in range(6):<\/p>\n<p>    angle = i * np.pi \/ 3<\/p>\n<p>    plot_hexagon(angle, 0, hex_size)<\/p>\n<p>    for j in range(1, 4):<\/p>\n<p>        plot_hexagon(angle, j * hex_size * np.sqrt(3)\/2, hex_size)<\/p>\n<p>        plot_hexagon(angle + np.pi \/ 6, j * hex_size * np.sqrt(3)\/2, hex_size)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6837\u5f0f\u548c\u6807\u6ce8<\/strong><\/h2>\n<p>ax.set_yticklabels([])<\/p>\n<p>ax.set_xticks(np.linspace(0, 2 * np.pi, 12, endpoint=False))<\/p>\n<p>ax.grid(True)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u516d\u8fb9\u5f62\u7684\u6781\u5750\u6807\u56fe\u5728Python\u4e2d\u975e\u5e38\u7b80\u5355\uff0c\u901a\u8fc7\u4f7f\u7528matplotlib\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4ece\u5b89\u88c5\u5e93\u3001\u8bbe\u7f6e\u6781\u5750\u6807\u7cfb\u3001\u8ba1\u7b97\u516d\u8fb9\u5f62\u9876\u70b9\u5230\u7ed8\u5236\u56fe\u5f62\u7684\u6574\u4e2a\u8fc7\u7a0b\uff0c\u5e76\u63d0\u4f9b\u4e86\u5b9e\u9645\u4ee3\u7801\u793a\u4f8b\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u6211\u4eec\u4e0d\u4ec5\u4e86\u89e3\u4e86\u5982\u4f55\u7ed8\u5236\u516d\u8fb9\u5f62\u7684\u6781\u5750\u6807\u56fe\uff0c\u8fd8\u5b66\u4f1a\u4e86\u5982\u4f55\u5e94\u7528\u8fd9\u4e9b\u56fe\u5f62\u5230\u6570\u636e\u53ef\u89c6\u5316\u3001GIS\u548c\u6e38\u620f\u5f00\u53d1\u7b49\u9886\u57df\u3002\u65e0\u8bba\u4f60\u662f\u6570\u636e\u79d1\u5b66\u5bb6\u3001\u5730\u7406\u4fe1\u606f\u7cfb\u7edf\u5de5\u7a0b\u5e08\u8fd8\u662f\u6e38\u620f\u5f00\u53d1\u8005\uff0c\u8fd9\u4e9b\u77e5\u8bc6\u90fd\u5c06\u662f\u4f60\u5de5\u5177\u7bb1\u4e2d\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><p>\u8bb0\u4f4f\uff0c\u7ed8\u56fe\u548c\u6570\u636e\u53ef\u89c6\u5316\u662f\u4e00\u4e2a\u4e0d\u65ad\u5b66\u4e60\u548c\u6539\u8fdb\u7684\u8fc7\u7a0b\u3002\u5e0c\u671b\u4f60\u80fd\u901a\u8fc7\u672c\u6587\u83b7\u5f97\u7075\u611f\uff0c\u521b\u5efa\u51fa\u66f4\u591a\u6709\u8da3\u548c\u5b9e\u7528\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u516d\u8fb9\u5f62\u7684\u6781\u5750\u6807\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u516d\u8fb9\u5f62\u7684\u6781\u5750\u6807\u56fe\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\u3002\u63a5\u7740\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u6781\u5750\u6807\u8f6c\u6362\u4e3a\u7b1b\u5361\u5c14\u5750\u6807\u6765\u7ed8\u5236\u516d\u8fb9\u5f62\u3002\u4f7f\u7528<code>plt.polar()<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u6781\u5750\u6807\u7cfb\u7edf\u4e2d\u7ed8\u5236\u56fe\u5f62\u3002<\/p>\n<p><strong>\u5728\u6781\u5750\u6807\u56fe\u4e2d\uff0c\u5982\u4f55\u8bbe\u7f6e\u516d\u8fb9\u5f62\u7684\u8fb9\u957f\u548c\u6570\u91cf\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u6781\u5750\u6807\u56fe\u7684\u89d2\u5ea6\u548c\u534a\u5f84\u6765\u8bbe\u7f6e\u516d\u8fb9\u5f62\u7684\u8fb9\u957f\u548c\u6570\u91cf\u3002\u4f7f\u7528NumPy\u751f\u6210\u516d\u8fb9\u5f62\u7684\u89d2\u5ea6\u548c\u76f8\u5e94\u7684\u534a\u5f84\u503c\uff0c\u7136\u540e\u901a\u8fc7Matplotlib\u7684\u7ed8\u56fe\u51fd\u6570\u5c06\u5176\u7ed8\u5236\u51fa\u6765\u3002\u786e\u4fdd\u5728\u7ed8\u56fe\u65f6\u5c06\u8fb9\u957f\u4e0e\u89d2\u5ea6\u76f8\u7ed3\u5408\uff0c\u4ee5\u4fbf\u51c6\u786e\u5f62\u6210\u516d\u8fb9\u5f62\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u516d\u8fb9\u5f62\u6781\u5750\u6807\u56fe\u7684\u989c\u8272\u548c\u6837\u5f0f\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>color<\/code>\u548c<code>linestyle<\/code>\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u6781\u5750\u6807\u56fe\u7684\u989c\u8272\u548c\u6837\u5f0f\u3002\u4f7f\u7528<code>plt.fill()<\/code>\u53ef\u4ee5\u586b\u5145\u516d\u8fb9\u5f62\u7684\u5185\u90e8\u989c\u8272\uff0c\u800c<code>plt.plot()<\/code>\u5219\u7528\u4e8e\u8c03\u6574\u8fb9\u754c\u7ebf\u7684\u6837\u5f0f\u3002\u901a\u8fc7\u8fd9\u4e9b\u53c2\u6570\u7684\u7ec4\u5408\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u51fa\u72ec\u7279\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efa\u516d\u8fb9\u5f62\u7684\u6781\u5750\u6807\u56fe\uff0c\u6709\u51e0\u4e2a\u5173\u952e\u7684\u6b65\u9aa4\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u8bbe\u7f6e\u6781\u5750\u6807\u7cfb\u3001\u8ba1\u7b97\u516d\u8fb9\u5f62\u9876 [&hellip;]","protected":false},"author":3,"featured_media":1138612,"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\/1138606"}],"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=1138606"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138606\/revisions"}],"predecessor-version":[{"id":1138615,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138606\/revisions\/1138615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1138612"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1138606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1138606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1138606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}