{"id":1108087,"date":"2025-01-08T16:56:33","date_gmt":"2025-01-08T08:56:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108087.html"},"modified":"2025-01-08T16:56:52","modified_gmt":"2025-01-08T08:56:52","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e6%9f%b1%e7%8a%b6%e5%9b%be%e4%b8%8a%e5%8a%a0%e6%95%b0%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108087.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u67f1\u72b6\u56fe\u4e0a\u52a0\u6570\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071858\/2a536f94-ad67-4fba-ad8f-0976d50b0e13.webp\" alt=\"\u5982\u4f55\u7528python\u753b\u67f1\u72b6\u56fe\u4e0a\u52a0\u6570\u503c\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u753b\u67f1\u72b6\u56fe\u4e0a\u52a0\u6570\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u7528Python\u753b\u67f1\u72b6\u56fe\u5e76\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u6765\u5b9e\u73b0\u3002<strong>\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165\u6240\u9700\u5e93\u3001\u521b\u5efa\u6570\u636e\u3001\u7ed8\u5236\u67f1\u72b6\u56fe\u3001\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c\u7684\u8be6\u7ec6\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u67f1\u72b6\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165Python\u7684Matplotlib\u5e93\u3002Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u9759\u6001\u3001\u52a8\u753b\u548c\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u7684\u7efc\u5408\u5e93\u3002\u5b83\u662fPython\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\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><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u6570\u636e\u6765\u7ed8\u5236\u67f1\u72b6\u56fe\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u7ec4\u6570\u636e\uff0c\u8868\u793a\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>values = [23, 45, 56, 78, 89]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u67f1\u72b6\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684 <code>bar<\/code> \u51fd\u6570\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7ed8\u5236\u67f1\u72b6\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p>bars = ax.bar(categories, values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684 <code>text<\/code> \u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5728\u6307\u5b9a\u7684\u4f4d\u7f6e\u4e0a\u6dfb\u52a0\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for bar in bars:<\/p>\n<p>    yval = bar.get_height()<\/p>\n<p>    ax.text(bar.get_x() + bar.get_width() \/ 2, yval + 1, int(yval), ha=&#39;center&#39;, va=&#39;bottom&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u663e\u793a\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u6b65\u662f\u663e\u793a\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed8\u5236\u4e00\u4e2a\u67f1\u72b6\u56fe\u5e76\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u6dfb\u52a0\u6570\u503c\u3002\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>\u6570\u636e<\/strong><\/h2>\n<p>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>values = [23, 45, 56, 78, 89]<\/p>\n<h2><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>bars = ax.bar(categories, values)<\/p>\n<h2><strong>\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c<\/strong><\/h2>\n<p>for bar in bars:<\/p>\n<p>    yval = bar.get_height()<\/p>\n<p>    ax.text(bar.get_x() + bar.get_width() \/ 2, yval + 1, int(yval), ha=&#39;center&#39;, va=&#39;bottom&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6df1\u5165\u4e86\u89e3\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u56fe\u8868\u8fdb\u884c\u4e00\u4e9b\u4f18\u5316\u548c\u7f8e\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8c03\u6574\u989c\u8272\u548c\u6837\u5f0f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u989c\u8272\u548c\u6837\u5f0f\u4f7f\u67f1\u72b6\u56fe\u66f4\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">bars = ax.bar(categories, values, color=&#39;skyblue&#39;, edgecolor=&#39;black&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u4e3a\u56fe\u8868\u6dfb\u52a0\u6807\u9898\u548c\u8f74\u6807\u7b7e\uff0c\u4f7f\u56fe\u8868\u4fe1\u606f\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_title(&#39;Category Values&#39;)<\/p>\n<p>ax.set_xlabel(&#39;Categories&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Values&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8bbe\u7f6ey\u8f74\u7684\u8303\u56f4<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u9ed8\u8ba4\u7684y\u8f74\u8303\u56f4\u53ef\u80fd\u4e0d\u9002\u5408\uff0c\u53ef\u4ee5\u624b\u52a8\u8bbe\u7f6ey\u8f74\u7684\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_ylim(0, 100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u65cb\u8f6cx\u8f74\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u5982\u679cx\u8f74\u7684\u6807\u7b7e\u8fc7\u957f\uff0c\u53ef\u4ee5\u65cb\u8f6c\u6807\u7b7e\uff0c\u4ee5\u907f\u514d\u91cd\u53e0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.xticks(rotation=45)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5b8c\u6574\u7684\u4f18\u5316\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5305\u542b\u4e0a\u8ff0\u4f18\u5316\u7684\u5b8c\u6574\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>\u6570\u636e<\/strong><\/h2>\n<p>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>values = [23, 45, 56, 78, 89]<\/p>\n<h2><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>bars = ax.bar(categories, values, color=&#39;skyblue&#39;, edgecolor=&#39;black&#39;)<\/p>\n<h2><strong>\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c<\/strong><\/h2>\n<p>for bar in bars:<\/p>\n<p>    yval = bar.get_height()<\/p>\n<p>    ax.text(bar.get_x() + bar.get_width() \/ 2, yval + 1, int(yval), ha=&#39;center&#39;, va=&#39;bottom&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_title(&#39;Category Values&#39;)<\/p>\n<p>ax.set_xlabel(&#39;Categories&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Values&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6ey\u8f74\u7684\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_ylim(0, 100)<\/p>\n<h2><strong>\u65cb\u8f6cx\u8f74\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xticks(rotation=45)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7528Python\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u5728\u56fe\u8868\u4e0a\u6dfb\u52a0\u6570\u503c\u3002Matplotlib\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5bf9\u56fe\u8868\u8fdb\u884c\u4f18\u5316\u548c\u7f8e\u5316\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u5728Python\u4e2d\u7ed8\u5236\u67f1\u72b6\u56fe\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u7ed8\u5236\u5e26\u6709\u6570\u503c\u7684\u67f1\u72b6\u56fe\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u7ed8\u5236\u5e26\u6709\u6570\u503c\u7684\u67f1\u72b6\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u4f7f\u7528<code>plt.bar()<\/code>\u51fd\u6570\u7ed8\u5236\u67f1\u72b6\u56fe\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>plt.text()<\/code>\u5728\u6bcf\u4e2a\u67f1\u7684\u9876\u90e8\u6dfb\u52a0\u6570\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u793a\u4f8b\u6570\u636e\ncategories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;]\nvalues = [10, 15, 7]\n\n# \u7ed8\u5236\u67f1\u72b6\u56fe\nplt.bar(categories, values)\n\n# \u5728\u67f1\u4e0a\u6dfb\u52a0\u6570\u503c\nfor i, value in enumerate(values):\n    plt.text(i, value + 0.5, str(value), ha=&#39;center&#39;)\n\nplt.show()\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u7ed8\u5236\u67f1\u72b6\u56fe\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0c\u5176\u4ed6\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\u4e5f\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u67f1\u72b6\u56fe\u3002\u4f8b\u5982\uff0cSeaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7f8e\u89c2\u7684\u56fe\u5f62\u3002Plotly\u5219\u652f\u6301\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u9002\u5408\u7528\u4e8eWeb\u5e94\u7528\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<p><strong>\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c\u65f6\uff0c\u786e\u4fdd\u6570\u503c\u4e0e\u67f1\u5b50\u7684\u989c\u8272\u6216\u80cc\u666f\u6709\u8db3\u591f\u7684\u5bf9\u6bd4\u5ea6\uff0c\u4ee5\u4fdd\u8bc1\u53ef\u8bfb\u6027\u3002\u6b64\u5916\uff0c\u907f\u514d\u5c06\u6570\u503c\u653e\u5f97\u8fc7\u9ad8\uff0c\u4ee5\u514d\u5f71\u54cd\u6574\u4f53\u7f8e\u89c2\u3002\u9009\u62e9\u5408\u9002\u7684\u5b57\u4f53\u5927\u5c0f\u548c\u4f4d\u7f6e\u4e5f\u662f\u63d0\u5347\u89c6\u89c9\u6548\u679c\u7684\u91cd\u8981\u56e0\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u753b\u67f1\u72b6\u56fe\u4e0a\u52a0\u6570\u503c \u7528Python\u753b\u67f1\u72b6\u56fe\u5e76\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib [&hellip;]","protected":false},"author":3,"featured_media":1108117,"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\/1108087"}],"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=1108087"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108087\/revisions"}],"predecessor-version":[{"id":1108128,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108087\/revisions\/1108128"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108117"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}