{"id":177181,"date":"2024-05-08T19:25:41","date_gmt":"2024-05-08T11:25:41","guid":{"rendered":""},"modified":"2024-05-08T19:25:46","modified_gmt":"2024-05-08T11:25:46","slug":"%e7%94%a8python-matplotlib-%e6%80%8e%e4%b9%88%e7%94%bb%e9%a3%8e%e5%90%91%e7%8e%ab%e7%91%b0%e5%9b%be-%ef%bc%8c%e8%83%bd%e7%bb%99%e5%87%ba%e7%a8%8b%e5%ba%8f%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/177181.html","title":{"rendered":"\u7528Python matplotlib \u600e\u4e48\u753b\u98ce\u5411\u73ab\u7470\u56fe \uff0c\u80fd\u7ed9\u51fa\u7a0b\u5e8f\u7684"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27061109\/3a54b6ab-e5b2-46a9-8b4c-59f1a9eb4b0c.webp\" alt=\"\u7528Python matplotlib \u600e\u4e48\u753b\u98ce\u5411\u73ab\u7470\u56fe \uff0c\u80fd\u7ed9\u51fa\u7a0b\u5e8f\u7684\" \/><\/p>\n<p><p>\u7528Python\u7684matplotlib\u5e93\u753b\u98ce\u5411\u73ab\u7470\u56fe\uff0c\u9996\u8981\u6b65\u9aa4\u5305\u62ec\u51c6\u5907\u6570\u636e\u3001\u521b\u5efa\u6781\u5750\u6807\u56fe\u8868\u3001\u7ed8\u5236\u98ce\u5411\u73ab\u7470\u56fe\u3001\u4ee5\u53ca\u6700\u540e\u7684\u4e2a\u6027\u5316\u8bbe\u5b9a\u3002\u98ce\u5411\u73ab\u7470\u56fe\u662f\u7528\u6765\u8868\u793a\u98ce\u901f\u548c\u98ce\u5411\u6570\u636e\u7684\u56fe\u5f62\u5316\u65b9\u6cd5\uff0c\u5bf9\u4e8e\u6c14\u8c61\u5b66\u3001\u5efa\u7b51\u5b66\u3001\u73af\u5883\u79d1\u5b66\u7b49\u9886\u57df\u5177\u6709\u91cd\u8981\u610f\u4e49\u3002\u5728Python\u4e2d\uff0cmatplotlib\u5e93\u56e0\u5176\u5f3a\u5927\u7684\u5b9a\u5236\u529f\u80fd\u548c\u5e7f\u6cdb\u7684\u4f7f\u7528\u793e\u533a\u800c\u6210\u4e3a\u751f\u6210\u98ce\u5411\u73ab\u7470\u56fe\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u521b\u5efa\u6781\u5750\u6807\u56fe\u8868\u5e76\u7ed8\u5236\u98ce\u5411\u73ab\u7470\u56fe\u3002\u5c06\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u51c6\u5907\u98ce\u901f\u548c\u98ce\u5411\u7684\u6570\u636e\u3001\u4f7f\u7528matplotlib\u7684\u6781\u5750\u6807\u7cfb\u7edf\u7ed8\u56fe\u3001\u5bf9\u56fe\u8868\u8fdb\u884c\u4e2a\u6027\u5316\u8bbe\u7f6e\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u6570\u636e\u5c55\u793a\u9700\u8981\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u6570\u636e\u51c6\u5907<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u56fe\u524d\uff0c\u9700\u8981\u51c6\u5907\u6216\u751f\u6210\u4ee3\u8868\u98ce\u901f\u548c\u98ce\u5411\u7684\u6570\u636e\u3002\u8fd9\u4e9b\u6570\u636e\u901a\u5e38\u6765\u6e90\u4e8e\u6c14\u8c61\u7ad9\u7684\u5b9e\u6d4b\u6570\u636e\uff0c\u5176\u4e2d\u98ce\u5411\u5e38\u4ee5\u89d2\u5ea6\u8868\u793a\uff0c\u98ce\u901f\u5219\u53ef\u80fd\u4ee5\u7c73\/\u79d2\u3001\u5343\u7c73\/\u5c0f\u65f6\u7b49\u5355\u4f4d\u663e\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u636e<\/strong><\/h2>\n<p>wind_directions = np.random.randint(0, 360, 500) # \u968f\u673a\u751f\u6210500\u4e2a\u98ce\u5411\u6570\u636e\uff080-359\u5ea6\uff09<\/p>\n<p>wind_speeds = np.random.rand(500) * 15  # \u968f\u673a\u751f\u6210500\u4e2a\u98ce\u901f\u6570\u636e\uff080-15m\/s\uff09<\/p>\n<h2><strong>\u5c06\u6570\u636e\u7f16\u7ec4\u4ee5\u4fbf\u4e8e\u7ed8\u56fe<\/strong><\/h2>\n<p>bins = np.arange(0, 360+1, 45)  # \u5212\u5206\u98ce\u5411\u4e3a8\u4e2a\u533a\u57df<\/p>\n<p>wind_speed_bins = [0, 5, 10, 15]  # \u5212\u5206\u98ce\u901f\u7ea7\u522b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u521b\u5efa\u6781\u5750\u6807\u56fe\u8868<\/h2>\n<\/p>\n<p><p>\u5728matplotlib\u4e2d\uff0c\u901a\u8fc7\u914d\u7f6e<code>subplot<\/code>\u5e76\u4f7f\u7528<code>polar=True<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u521b\u5efa\u4e00\u4e2a\u6781\u5750\u6807\u7cfb\u3002\u5728\u8fd9\u4e2a\u5750\u6807\u7cfb\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed8\u5236\u4e0d\u540c\u98ce\u5411\u548c\u98ce\u901f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6781\u5750\u6807\u56fe\u8868<\/strong><\/h2>\n<p>fig, ax = plt.subplots(subplot_kw={&#039;polar&#039;: True})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u7ed8\u5236\u98ce\u5411\u73ab\u7470\u56fe<\/h2>\n<\/p>\n<p><p><strong>\u6839\u636e\u98ce\u901f\u548c\u98ce\u5411\u7684\u6570\u636e\u7ed8\u5236\u98ce\u5411\u73ab\u7470\u56fe\u7684\u5173\u952e\uff0c\u5728\u4e8e\u5408\u7406\u5229\u7528\u6781\u5750\u6807\u7684\u6027\u8d28\uff0c\u5c06\u5404\u4e2a\u98ce\u5411\u533a\u95f4\u7684\u98ce\u901f\u8fdb\u884c\u5206\u7c7b\u7edf\u8ba1\uff0c\u5e76\u901a\u8fc7\u6761\u5f62\u56fe\u8868\u793a\u51fa\u6765\u3002<\/strong> \u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528<code>np.histogram2d<\/code>\u51fd\u6570\u6765\u5bf9\u98ce\u901f\u548c\u98ce\u5411\u7684\u6570\u636e\u8fdb\u884c\u5206\u7c7b\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7edf\u8ba1\u6bcf\u4e2a\u98ce\u5411\u533a\u95f4\u5185\u7684\u98ce\u901f\u5206\u5e03<\/p>\n<p>wind_distribution, _, _ = np.histogram2d(wind_directions, wind_speeds, bins=[bins, wind_speed_bins])<\/p>\n<h2><strong>\u5c06\u98ce\u5411\u533a\u95f4\u5206\u914d\u5230\u6781\u5750\u6807\u7684\u89d2\u5ea6<\/strong><\/h2>\n<p>angles = np.radians(np.arange(0, 360, 45))<\/p>\n<h2><strong>\u7ed8\u5236\u98ce\u5411\u73ab\u7470\u56fe<\/strong><\/h2>\n<p>for i, (wind_count, angle) in enumerate(zip(wind_distribution.T, angles)):<\/p>\n<p>    ax.bar(angle, wind_count, width=np.radians(45), bottom=0.2, color=plt.cm.viridis(i \/ len(wind_distribution.T)))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4e2a\u6027\u5316\u8bbe\u5b9a<\/h2>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5bf9\u98ce\u5411\u73ab\u7470\u56fe\u8fdb\u884c\u4e2a\u6027\u5316\u8bbe\u7f6e\u4ee5\u8fbe\u5230\u66f4\u597d\u7684\u89c6\u89c9\u6548\u679c\u3002\u8fd9\u5305\u62ec\u8bbe\u7f6e\u56fe\u8868\u7684\u6807\u9898\u3001\u8c03\u6574\u56fe\u4f8b\u3001\u4f18\u5316\u6807\u7b7e\u7b49\u3002\u8fd9\u4e9b\u4e2a\u6027\u5316\u8bbe\u7f6e\u53ef\u4ee5\u8ba9\u56fe\u8868\u66f4\u52a0\u6e05\u6670\u6613\u8bfb\uff0c\u540c\u65f6\u6ee1\u8db3\u7279\u5b9a\u7684\u5c55\u793a\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u56fe\u8868\u6807\u9898\u548c\u6807\u7b7e<\/p>\n<p>ax.set_title(&quot;\u98ce\u5411\u73ab\u7470\u56fe\u793a\u4f8b&quot;, va=&#039;bottom&#039;, loc=&#039;left&#039;)<\/p>\n<p>ax.set_theta_zero_location(&#039;N&#039;)  # \u8bbe\u7f6e0\u5ea6\u65b9\u5411\u4e3a\u6b63\u5317\u65b9\u5411<\/p>\n<p>ax.set_theta_direction(-1)  # \u8bbe\u7f6e\u89d2\u5ea6\u6309\u7167\u987a\u65f6\u9488\u65b9\u5411\u589e\u52a0<\/p>\n<h2><strong>\u8bbe\u7f6e\u6781\u8f74\u7684\u663e\u793a\u8303\u56f4\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_thetagrids(angles * 180 \/ np.pi, labels=[&#039;\u5317&#039;, &#039;\u4e1c\u5317&#039;, &#039;\u4e1c&#039;, &#039;\u4e1c\u5357&#039;, &#039;\u5357&#039;, &#039;\u897f\u5357&#039;, &#039;\u897f&#039;, &#039;\u897f\u5317&#039;])<\/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\u5728Python\u4e2d\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u51fa\u8868\u73b0\u98ce\u901f\u548c\u98ce\u5411\u6570\u636e\u7684\u98ce\u5411\u73ab\u7470\u56fe\u3002\u8be5\u8fc7\u7a0b\u6db5\u76d6\u4e86\u4ece\u6570\u636e\u51c6\u5907\u5230\u56fe\u8868\u4e2a\u6027\u5316\u8bbe\u7f6e\u7684\u5b8c\u6574\u6d41\u7a0b\uff0c\u4f7f\u5f97\u6700\u7ec8\u751f\u6210\u7684\u98ce\u5411\u73ab\u7470\u56fe\u65e2\u51c6\u786e\u53c8\u7f8e\u89c2\uff0c\u80fd\u591f\u6e05\u6670\u5730\u5c55\u73b0\u98ce\u7684\u884c\u4e3a\u7279\u5f81\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684matplotlib\u5e93\u753b\u51fa\u98ce\u5411\u73ab\u7470\u56fe\uff1f<\/strong><\/p>\n<p>\u98ce\u5411\u73ab\u7470\u56fe\u662f\u7528\u6765\u5c55\u793a\u98ce\u5411\u5206\u5e03\u60c5\u51b5\u7684\u4e00\u79cd\u56fe\u8868\u3002\u4f7f\u7528Python\u7684matplotlib\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u7ed8\u5236\u51fa\u98ce\u5411\u73ab\u7470\u56fe\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\n# \u521b\u5efa\u6570\u636e\ndirections = np.arange(0, 360, 30)  # \u98ce\u5411\u6570\u636e\uff0c\u4ee530\u5ea6\u4e3a\u95f4\u9694\nfrequencies = np.random.randint(1, 10, size=len(directions))  # \u9891\u7387\u6570\u636e\uff0c\u968f\u673a\u751f\u6210\n\n# \u8bbe\u7f6e\u6781\u5750\u6807\u8f74\nax = plt.subplot(1, 1, 1, polar=True)\n\n# \u753b\u98ce\u5411\u73ab\u7470\u56fe\nbars = ax.bar(\n    np.deg2rad(directions), frequencies, width=np.deg2rad(30), bottom=0.0\n)\n\n# \u8c03\u6574\u6781\u5750\u6807\u8f74\u7684\u65b9\u5411\nax.set_theta_zero_location(&quot;N&quot;)\n\n# \u6dfb\u52a0\u523b\u5ea6\u548c\u6807\u7b7e\nax.set_xticks(np.deg2rad(np.arange(0, 360, 30)))  # \u523b\u5ea6\u4f4d\u7f6e\nax.set_xticklabels(\n    [&quot;N&quot;, &quot;NNE&quot;, &quot;NE&quot;, &quot;ENE&quot;, &quot;E&quot;, &quot;ESE&quot;, &quot;SE&quot;, &quot;SSE&quot;, &quot;S&quot;, &quot;SSW&quot;, &quot;SW&quot;, &quot;WSW&quot;, &quot;W&quot;, &quot;WNW&quot;, &quot;NW&quot;, &quot;NNW&quot;]\n)  # \u523b\u5ea6\u6807\u7b7e\n\n# \u6dfb\u52a0\u6807\u9898\nplt.title(&quot;Wind Direction Rose Diagram&quot;)\n\n# \u663e\u793a\u56fe\u8868\nplt.show()\n<\/code><\/pre>\n<p><strong>2. \u5982\u4f55\u89e3\u8bfb\u98ce\u5411\u73ab\u7470\u56fe\u4e0a\u7684\u6570\u636e\uff1f<\/strong><\/p>\n<p>\u98ce\u5411\u73ab\u7470\u56fe\u7528\u6765\u5c55\u793a\u4e0d\u540c\u98ce\u5411\u7684\u9891\u7387\u5206\u5e03\u60c5\u51b5\uff0c\u5e38\u7528\u6765\u5206\u6790\u98ce\u7684\u53d8\u5316\u89c4\u5f8b\u3002\u56fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u6247\u5f62\u533a\u57df\u8868\u793a\u4e00\u4e2a\u98ce\u5411\uff0c\u6247\u5f62\u7684\u5706\u5f27\u957f\u5ea6\u8868\u793a\u8be5\u98ce\u5411\u7684\u9891\u7387\u3002\u9891\u7387\u8d8a\u9ad8\uff0c\u5f27\u7ebf\u8d8a\u957f\u3002\u901a\u8fc7\u89c2\u5bdf\u4e0d\u540c\u6247\u5f62\u533a\u57df\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u4e86\u89e3\u4e0d\u540c\u98ce\u5411\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<p><strong>3. \u98ce\u5411\u73ab\u7470\u56fe\u6709\u54ea\u4e9b\u5e94\u7528\u573a\u666f\uff1f<\/strong><\/p>\n<p>\u98ce\u5411\u73ab\u7470\u56fe\u4e3b\u8981\u7528\u6765\u5206\u6790\u98ce\u5411\u7684\u5206\u5e03\u60c5\u51b5\uff0c\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u5305\u62ec\u6c14\u8c61\u5b66\u3001\u5730\u7406\u5b66\u3001\u98ce\u80fd\u8bc4\u4f30\u7b49\u9886\u57df\u3002\u4f8b\u5982\uff0c\u5728\u6c14\u8c61\u5b66\u4e2d\uff0c\u53ef\u4ee5\u7528\u98ce\u5411\u73ab\u7470\u56fe\u5206\u6790\u67d0\u5730\u98ce\u5411\u7684\u5b63\u8282\u6027\u3001\u65f6\u6bb5\u6027\u53d8\u5316\u89c4\u5f8b\uff1b\u5728\u98ce\u80fd\u8bc4\u4f30\u4e2d\uff0c\u53ef\u4ee5\u7528\u98ce\u5411\u73ab\u7470\u56fe\u5206\u6790\u98ce\u80fd\u8d44\u6e90\u7684\u5206\u5e03\u60c5\u51b5\uff0c\u786e\u5b9a\u9002\u5408\u5efa\u8bbe\u98ce\u529b\u53d1\u7535\u573a\u7684\u4f4d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u7684matplotlib\u5e93\u753b\u98ce\u5411\u73ab\u7470\u56fe\uff0c\u9996\u8981\u6b65\u9aa4\u5305\u62ec\u51c6\u5907\u6570\u636e\u3001\u521b\u5efa\u6781\u5750\u6807\u56fe\u8868\u3001\u7ed8\u5236\u98ce\u5411\u73ab\u7470\u56fe\u3001\u4ee5\u53ca [&hellip;]","protected":false},"author":3,"featured_media":177190,"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\/177181"}],"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=177181"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177181\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/177190"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=177181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=177181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=177181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}