{"id":1060894,"date":"2024-12-31T15:38:03","date_gmt":"2024-12-31T07:38:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1060894.html"},"modified":"2024-12-31T15:38:05","modified_gmt":"2024-12-31T07:38:05","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e6%9f%b1%e7%8a%b6%e5%9b%be%e4%b8%8a%e5%b8%a6%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1060894.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u67f1\u72b6\u56fe\u4e0a\u5e26\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/cb8be896-fe43-4366-8acf-777095b7efea.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8ba9\u67f1\u72b6\u56fe\u4e0a\u5e26\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u5728\u67f1\u72b6\u56fe\u4e0a\u663e\u793a\u6570\u5b57\u3002\u901a\u8fc7\u8c03\u7528<code>bar_label<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u6dfb\u52a0\u6807\u7b7e\uff0c\u4ee5\u663e\u793a\u5bf9\u5e94\u7684\u6570\u503c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u8be6\u7ec6\u5c55\u793a\u5982\u4f55\u5728\u67f1\u72b6\u56fe\u4e0a\u5e26\u6570\u5b57\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\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\u811a\u672c\u4e2d\u5bfc\u5165Matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u521b\u5efa\u57fa\u7840\u6570\u636e\u548c\u67f1\u72b6\u56fe<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e9b\u6570\u636e\u6765\u521b\u5efa\u67f1\u72b6\u56fe\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e<\/p>\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, 13]<\/p>\n<h2><strong>\u521b\u5efa\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>bars = ax.bar(categories, values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684<code>bar_label<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u6dfb\u52a0\u6570\u503c\u6807\u7b7e\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u6dfb\u52a0\u6570\u503c\u6807\u7b7e<\/p>\n<p>ax.bar_label(bars)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u663e\u793a\u5bf9\u5e94\u7684\u6570\u503c\uff0c\u589e\u5f3a\u6570\u636e\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u89e3\u91ca\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><h2>\u56db\u3001\u8fdb\u4e00\u6b65\u5b9a\u5236\u56fe\u8868<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u56fe\u8868\u66f4\u52a0\u7f8e\u89c2\u548c\u4e13\u4e1a\uff0c\u53ef\u4ee5\u5bf9\u56fe\u8868\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5b9a\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5b9a\u5236\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><h2>1\u3001\u8bbe\u7f6e\u8f74\u6807\u7b7e\u548c\u6807\u9898<\/h2>\n<\/p>\n<p><p>\u6dfb\u52a0\u8f74\u6807\u7b7e\u548c\u6807\u9898\u53ef\u4ee5\u5e2e\u52a9\u89c2\u4f17\u66f4\u597d\u5730\u7406\u89e3\u56fe\u8868\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_xlabel(&#39;Categories&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Values&#39;)<\/p>\n<p>ax.set_title(&#39;Bar Chart with Labels&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u8c03\u6574\u6807\u7b7e\u4f4d\u7f6e\u548c\u683c\u5f0f<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574<code>padding<\/code>\u53c2\u6570\u6765\u6539\u53d8\u6807\u7b7e\u4e0e\u67f1\u5b50\u7684\u8ddd\u79bb\uff0c\u4ee5\u53ca\u4f7f\u7528<code>fmt<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u6807\u7b7e\u7684\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.bar_label(bars, padding=3, fmt=&#39;%.2f&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4fee\u6539\u6807\u7b7e\u989c\u8272\u548c\u5b57\u4f53<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>label_type<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u6807\u7b7e\u7684\u4f4d\u7f6e\uff08&#39;edge&#39; \u6216 &#39;center&#39;\uff09\uff0c\u5e76\u901a\u8fc7<code>color<\/code>\u548c<code>fontsize<\/code>\u53c2\u6570\u6765\u5b9a\u5236\u6807\u7b7e\u7684\u5916\u89c2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.bar_label(bars, label_type=&#39;edge&#39;, color=&#39;blue&#39;, fontsize=8)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57\u5e76\u8fdb\u884c\u5b9a\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\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, 13]<\/p>\n<h2><strong>\u521b\u5efa\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>bars = ax.bar(categories, values)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8f74\u6807\u7b7e\u548c\u6807\u9898<\/strong><\/h2>\n<p>ax.set_xlabel(&#39;Categories&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Values&#39;)<\/p>\n<p>ax.set_title(&#39;Bar Chart with Labels&#39;)<\/p>\n<h2><strong>\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u6dfb\u52a0\u6570\u503c\u6807\u7b7e<\/strong><\/h2>\n<p>ax.bar_label(bars, padding=3, fmt=&#39;%.2f&#39;, color=&#39;blue&#39;, fontsize=8)<\/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\u4e0a\u8ff0\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57\u6807\u7b7e\uff0c\u5e76\u5bf9\u56fe\u8868\u8fdb\u884c\u5b9a\u5236\uff0c\u4ee5\u63d0\u9ad8\u5176\u53ef\u8bfb\u6027\u548c\u89c6\u89c9\u6548\u679c\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7684\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u7684Matplotlib\u5e93\u7ed8\u5236\u67f1\u72b6\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>text()<\/code>\u51fd\u6570\u5728\u6bcf\u4e2a\u67f1\u5b50\u7684\u9876\u90e8\u6dfb\u52a0\u6570\u5b57\u3002\u5177\u4f53\u64cd\u4f5c\u662f\u5728\u7ed8\u5236\u67f1\u72b6\u56fe\u540e\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u67f1\u5b50\u7684\u9ad8\u5ea6\uff0c\u5e76\u5728\u9002\u5f53\u7684\u4f4d\u7f6e\u8c03\u7528<code>text()<\/code>\u65b9\u6cd5\u6765\u663e\u793a\u76f8\u5e94\u7684\u503c\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u5e26\u6570\u5b57\u7684\u67f1\u72b6\u56fe\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\u5916\uff0cSeaborn\u548cPlotly\u4e5f\u662f\u5f88\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u3002Seaborn\u57fa\u4e8eMatplotlib\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u63a5\u53e3\u3002Plotly\u5219\u5141\u8bb8\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u60a8\u53ef\u4ee5\u5728\u56fe\u8868\u4e2d\u8f7b\u677e\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\uff0c\u589e\u5f3a\u53ef\u89c6\u5316\u6548\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u67f1\u72b6\u56fe\u4e2d\u6570\u5b57\u7684\u5b57\u4f53\u548c\u989c\u8272\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>text()<\/code>\u65b9\u6cd5\u7684<code>fontsize<\/code>\u548c<code>color<\/code>\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u6570\u5b57\u7684\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e<code>fontsize=12<\/code>\u548c<code>color=&#39;red&#39;<\/code>\u6765\u6539\u53d8\u6570\u5b57\u7684\u663e\u793a\u6548\u679c\u3002\u8fd9\u79cd\u81ea\u5b9a\u4e49\u53ef\u4ee5\u8ba9\u56fe\u8868\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u5728\u67f1\u72b6\u56fe\u4e0a\u663e\u793a\u6570\u5b57\u3002\u901a\u8fc7\u8c03\u7528bar_label\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef [&hellip;]","protected":false},"author":3,"featured_media":1060911,"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\/1060894"}],"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=1060894"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060894\/revisions"}],"predecessor-version":[{"id":1060914,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060894\/revisions\/1060914"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1060911"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1060894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1060894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1060894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}