{"id":1073043,"date":"2025-01-08T11:21:42","date_gmt":"2025-01-08T03:21:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073043.html"},"modified":"2025-01-08T11:21:45","modified_gmt":"2025-01-08T03:21:45","slug":"python3%e9%a5%bc%e5%9b%be%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e6%95%b0%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073043.html","title":{"rendered":"python3\u997c\u56fe\u5982\u4f55\u8f93\u51fa\u6570\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103107\/9d19b403-64d3-4a42-aee4-337cebe6938b.webp\" alt=\"python3\u997c\u56fe\u5982\u4f55\u8f93\u51fa\u6570\u503c\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python3\u7ed8\u5236\u997c\u56fe\u5e76\u8f93\u51fa\u6570\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3001\u8bbe\u7f6eautopct\u53c2\u6570\u3001\u5b9a\u4e49\u767e\u5206\u6bd4\u683c\u5f0f\u3001\u5c06\u6570\u503c\u663e\u793a\u5728\u56fe\u4e2d<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python3\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u997c\u56fe\u975e\u5e38\u5e38\u89c1\uff0c\u5e76\u4e14\u80fd\u591f\u65b9\u4fbf\u5730\u8f93\u51fa\u6570\u503c\u3002\u901a\u8fc7\u8bbe\u7f6eautopct\u53c2\u6570\uff0c\u53ef\u4ee5\u5c06\u767e\u5206\u6bd4\u663e\u793a\u5728\u56fe\u4e2d\uff0c\u4ece\u800c\u4f7f\u5f97\u56fe\u8868\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Matplotlib\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\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><h3>\u4e8c\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u997c\u56fe\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\u4e2d\u7684pyplot\u6a21\u5757\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><h3>\u4e09\u3001\u51c6\u5907\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u51c6\u5907\u597d\u9700\u8981\u7ed8\u5236\u7684\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">labels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed8\u5236\u997c\u56fe\u5e76\u663e\u793a\u6570\u503c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528plt.pie()\u51fd\u6570\u7ed8\u5236\u997c\u56fe\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6eautopct\u53c2\u6570\u6765\u663e\u793a\u6570\u503c\u3002autopct\u53c2\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6216\u8005\u4e00\u4e2a\u51fd\u6570\u6765\u5b9a\u4e49\u6570\u503c\u7684\u663e\u793a\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;)<\/p>\n<p>plt.axis(&#39;equal&#39;)  # Equal aspect ratio ensures that pie is drawn as a circle.<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8be6\u7ec6\u89e3\u91caautopct\u53c2\u6570<\/h3>\n<\/p>\n<p><p>autopct\u53c2\u6570\u7684\u503c\u53ef\u4ee5\u662f\u4e00\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982&#39;%1.1f%%&#39;\uff0c\u8868\u793a\u663e\u793a1\u4f4d\u5c0f\u6570\u7684\u767e\u5206\u6bd4\u3002\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u81ea\u5b9a\u4e49\u663e\u793a\u65b9\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_autopct(values):<\/p>\n<p>    def my_autopct(pct):<\/p>\n<p>        total = sum(values)<\/p>\n<p>        val = int(round(pct*total\/100.0))<\/p>\n<p>        return &#39;{p:.2f}%  ({v:d})&#39;.format(p=pct, v=val)<\/p>\n<p>    return my_autopct<\/p>\n<p>plt.pie(sizes, labels=labels, autopct=make_autopct(sizes))<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u4e0d\u4ec5\u663e\u793a\u767e\u5206\u6bd4\uff0c\u8fd8\u663e\u793a\u4e86\u5b9e\u9645\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8fdb\u4e00\u6b65\u5b9a\u5236\u997c\u56fe<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u663e\u793a\u6570\u503c\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5b9a\u5236\u997c\u56fe\uff0c\u4f8b\u5982\u8bbe\u7f6e\u989c\u8272\u3001\u9634\u5f71\u6548\u679c\u548c\u8d77\u59cb\u89d2\u5ea6\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">colors = [&#39;gold&#39;, &#39;yellowgreen&#39;, &#39;lightcoral&#39;, &#39;lightskyblue&#39;]<\/p>\n<p>explode = (0.1, 0, 0, 0)  # explode 1st slice<\/p>\n<p>plt.pie(sizes, explode=explode, labels=labels, colors=colors,<\/p>\n<p>        autopct=&#39;%1.1f%%&#39;, shadow=True, startangle=140)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python3\u548cMatplotlib\u5e93\u7ed8\u5236\u5e26\u6709\u6570\u503c\u7684\u997c\u56fe\u3002\u8bbe\u7f6eautopct\u53c2\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6570\u503c\u663e\u793a\u5728\u56fe\u4e2d\uff0c\u4f7f\u5f97\u6570\u636e\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8fdb\u4e00\u6b65\u5b9a\u5236\u997c\u56fe\u7684\u5916\u89c2\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u6570\u636e\u53ef\u89c6\u5316\u4e2d\u66f4\u597d\u5730\u4f20\u8fbe\u4fe1\u606f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python3\u4e2d\u521b\u5efa\u997c\u56fe\u5e76\u663e\u793a\u6570\u503c\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u521b\u5efa\u997c\u56fe\u3002\u901a\u8fc7\u4f7f\u7528<code>autopct<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u997c\u56fe\u4e0a\u76f4\u63a5\u663e\u793a\u6bcf\u4e2a\u6247\u533a\u7684\u6570\u503c\u6216\u767e\u5206\u6bd4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>autopct=&#39;%1.1f%%&#39;<\/code>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u663e\u793a\u6bcf\u4e2a\u90e8\u5206\u7684\u767e\u5206\u6bd4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nsizes = [15, 30, 45, 10]\nlabels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]\nplt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;)\nplt.axis(&#39;equal&#39;)  # \u4f7f\u997c\u56fe\u4e3a\u5706\u5f62\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728\u997c\u56fe\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49\u6570\u503c\u7684\u663e\u793a\u683c\u5f0f\uff1f<\/strong><br \/>\u901a\u8fc7\u5728<code>autopct<\/code>\u53c2\u6570\u4e2d\u81ea\u5b9a\u4e49\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u6539\u53d8\u663e\u793a\u7684\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>autopct=&#39;%.0f&#39;<\/code>\u6765\u53ea\u663e\u793a\u6574\u6570\u503c\uff0c\u6216\u4f7f\u7528<code>autopct=&#39;%1.2f%%&#39;<\/code>\u6765\u663e\u793a\u4e24\u4f4d\u5c0f\u6570\u7684\u767e\u5206\u6bd4\u3002\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u7075\u6d3b\u8c03\u6574\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u997c\u56fe\u7684\u6247\u533a\u989c\u8272\u66f4\u5177\u5438\u5f15\u529b\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>colors<\/code>\u53c2\u6570\u4e3a\u997c\u56fe\u6307\u5b9a\u81ea\u5b9a\u4e49\u989c\u8272\u3002\u4f20\u5165\u4e00\u4e2a\u989c\u8272\u5217\u8868\uff0c\u4f8b\u5982\uff1a<code>colors=[&#39;gold&#39;, &#39;yellowgreen&#39;, &#39;lightcoral&#39;, &#39;lightskyblue&#39;]<\/code>\u3002\u8fd9\u6837\u80fd\u4f7f\u997c\u56fe\u5728\u89c6\u89c9\u4e0a\u66f4\u5177\u5438\u5f15\u529b\uff0c\u4fbf\u4e8e\u89c2\u4f17\u533a\u5206\u4e0d\u540c\u7684\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python3\u7ed8\u5236\u997c\u56fe\u5e76\u8f93\u51fa\u6570\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3001\u8bbe\u7f6eautopct\u53c2\u6570\u3001\u5b9a\u4e49\u767e\u5206\u6bd4\u683c\u5f0f\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1073051,"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\/1073043"}],"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=1073043"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073043\/revisions"}],"predecessor-version":[{"id":1073054,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073043\/revisions\/1073054"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073051"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}