{"id":970137,"date":"2024-12-27T05:23:23","date_gmt":"2024-12-26T21:23:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/970137.html"},"modified":"2024-12-27T05:23:25","modified_gmt":"2024-12-26T21:23:25","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e7%94%bb%e9%a5%bc%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/970137.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528python\u753b\u997c\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24184054\/bafd497d-059a-4f27-a644-c78648721de6.webp\" alt=\"\u5982\u4f55\u4f7f\u7528python\u753b\u997c\u56fe\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u753b\u997c\u56fe\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u5229\u7528Matplotlib\u5e93\u3001\u8bbe\u7f6e\u6570\u636e\u548c\u6807\u7b7e\u3001\u8c03\u6574\u56fe\u5f62\u5c5e\u6027\u3001\u6dfb\u52a0\u767e\u5206\u6bd4\u548c\u6807\u9898\u3001\u589e\u5f3a\u56fe\u5f62\u7f8e\u89c2\u6027\u3002<\/strong>\u5176\u4e2d\uff0cMatplotlib\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u521b\u5efa\u4e13\u4e1a\u7684\u997c\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u5e93\u7684\u5b89\u88c5\u4e0e\u5bfc\u5165<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u53ef\u7528\u4e8e\u521b\u5efa\u591a\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\u8fd8\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\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><p>\u8fd9\u4e2a\u547d\u4ee4\u5c06Matplotlib\u5e93\u4e2d\u7684pyplot\u6a21\u5757\u5bfc\u5165\u5230\u4f60\u7684\u811a\u672c\u4e2d\uff0c\u5e76\u4f7f\u7528<code>plt<\/code>\u4f5c\u4e3a\u8be5\u6a21\u5757\u7684\u522b\u540d\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u6570\u636e\u548c\u6807\u7b7e<\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u997c\u56fe\u65f6\uff0c\u9996\u5148\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u548c\u5bf9\u5e94\u7684\u6807\u7b7e\u3002\u6570\u636e\u901a\u5e38\u4ee5\u5217\u8868\u6216\u6570\u7ec4\u7684\u5f62\u5f0f\u5b58\u50a8\uff0c\u4ee3\u8868\u6bcf\u4e2a\u90e8\u5206\u7684\u5927\u5c0f\u3002\u6807\u7b7e\u5219\u7528\u4e8e\u6807\u8bc6\u6570\u636e\u7684\u4e0d\u540c\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<p>labels = [&#39;Apples&#39;, &#39;Bananas&#39;, &#39;Cherries&#39;, &#39;Dates&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>sizes<\/code>\u5217\u8868\u8868\u793a\u56db\u4e2a\u90e8\u5206\u7684\u5927\u5c0f\uff0c\u800c<code>labels<\/code>\u5217\u8868\u63d0\u4f9b\u4e86\u6bcf\u4e2a\u90e8\u5206\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7ed8\u5236\u57fa\u7840\u997c\u56fe<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u5e93\u7684<code>pie<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u997c\u56fe\u3002\u9700\u8981\u5c06\u6570\u636e\u548c\u6807\u7b7e\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.pie(sizes, labels=labels)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f1a\u751f\u6210\u4e00\u4e2a\u57fa\u672c\u7684\u997c\u56fe\uff0c\u663e\u793a\u4e0d\u540c\u90e8\u5206\u7684\u5927\u5c0f\u53ca\u5176\u5bf9\u5e94\u7684\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8c03\u6574\u56fe\u5f62\u5c5e\u6027<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u997c\u56fe\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\uff0c\u53ef\u4ee5\u8c03\u6574\u4e00\u4e9b\u56fe\u5f62\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u67d0\u4e9b\u90e8\u5206\u7a81\u51fa\u663e\u793a\u3001\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u3001\u8c03\u6574\u56fe\u4f8b\u4f4d\u7f6e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7a81\u51fa\u663e\u793a\u7b2c\u4e8c\u90e8\u5206<\/p>\n<p>explode = (0, 0.1, 0, 0)<\/p>\n<h2><strong>\u8bbe\u7f6e\u989c\u8272<\/strong><\/h2>\n<p>colors = [&#39;gold&#39;, &#39;yellowgreen&#39;, &#39;lightcoral&#39;, &#39;lightskyblue&#39;]<\/p>\n<p>plt.pie(sizes, explode=explode, labels=labels, colors=colors)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>explode<\/code>\u53c2\u6570\u7528\u4e8e\u7a81\u51fa\u663e\u793a\u7b2c\u4e8c\u90e8\u5206\uff0c\u800c<code>colors<\/code>\u53c2\u6570\u5219\u7528\u4e8e\u8bbe\u7f6e\u6bcf\u4e2a\u90e8\u5206\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6dfb\u52a0\u767e\u5206\u6bd4\u548c\u6807\u9898<\/p>\n<\/p>\n<p><p>\u5728\u997c\u56fe\u4e2d\u663e\u793a\u6bcf\u4e2a\u90e8\u5206\u6240\u5360\u7684\u767e\u5206\u6bd4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>autopct<\/code>\u53c2\u6570\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4e3a\u997c\u56fe\u6dfb\u52a0\u6807\u9898\u4ee5\u589e\u5f3a\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct=&#39;%1.1f%%&#39;)<\/p>\n<p>plt.title(&#39;Fruit Distribution&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>autopct=&#39;%1.1f%%&#39;<\/code>\u53c2\u6570\u4f1a\u5728\u6bcf\u4e2a\u90e8\u5206\u4e0a\u663e\u793a\u5176\u6240\u5360\u7684\u767e\u5206\u6bd4\uff0c<code>title<\/code>\u51fd\u6570\u7528\u4e8e\u4e3a\u56fe\u5f62\u6dfb\u52a0\u6807\u9898\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u589e\u5f3a\u56fe\u5f62\u7f8e\u89c2\u6027<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u63d0\u5347\u997c\u56fe\u7684\u7f8e\u89c2\u6027\uff0c\u53ef\u4ee5\u8c03\u6574\u8fb9\u7f18\u989c\u8272\u3001\u9634\u5f71\u6548\u679c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct=&#39;%1.1f%%&#39;, shadow=True, startangle=140)<\/p>\n<p>plt.title(&#39;Fruit Distribution&#39;)<\/p>\n<p>plt.axis(&#39;equal&#39;)  # \u786e\u4fdd\u997c\u56fe\u662f\u5706\u5f62<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>shadow=True<\/code>\u53c2\u6570\u4e3a\u997c\u56fe\u6dfb\u52a0\u4e86\u9634\u5f71\u6548\u679c\uff0c<code>startangle=140<\/code>\u53c2\u6570\u5219\u8c03\u6574\u4e86\u997c\u56fe\u7684\u8d77\u59cb\u89d2\u5ea6\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u3002<code>axis(&#39;equal&#39;)<\/code>\u786e\u4fdd\u997c\u56fe\u662f\u5706\u5f62\uff0c\u800c\u4e0d\u662f\u692d\u5706\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4fdd\u5b58\u56fe\u5f62\u5230\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5982\u679c\u4f60\u9700\u8981\u5c06\u751f\u6210\u7684\u997c\u56fe\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>savefig<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct=&#39;%1.1f%%&#39;, shadow=True, startangle=140)<\/p>\n<p>plt.title(&#39;Fruit Distribution&#39;)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.savefig(&#39;pie_chart.png&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>savefig(&#39;pie_chart.png&#39;)<\/code>\u4f1a\u5c06\u997c\u56fe\u4fdd\u5b58\u4e3a\u540d\u4e3a<code>pie_chart.png<\/code>\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5df2\u7ecf\u5b66\u4f1a\u4e86\u5982\u4f55\u4f7f\u7528Python\u548cMatplotlib\u5e93\u521b\u5efa\u4e13\u4e1a\u7684\u997c\u56fe\u3002\u65e0\u8bba\u662f\u7528\u4e8e\u6570\u636e\u5206\u6790\u3001\u62a5\u544a\u5c55\u793a\uff0c\u8fd8\u662f\u5b66\u672f\u7814\u7a76\uff0c\u8fd9\u4e9b\u6280\u5de7\u90fd\u80fd\u5e2e\u52a9\u4f60\u751f\u6210\u9ad8\u8d28\u91cf\u7684\u56fe\u5f62\u3002\u8bb0\u4f4f\uff0c\u56fe\u5f62\u7684\u7f8e\u89c2\u6027\u548c\u4fe1\u606f\u4f20\u8fbe\u7684\u51c6\u786e\u6027\u540c\u6837\u91cd\u8981\uff0c\u56e0\u6b64\u5728\u521b\u5efa\u56fe\u8868\u65f6\u5e94\u4ed4\u7ec6\u8bbe\u8ba1\u548c\u8c03\u6574\u6bcf\u4e2a\u7ec6\u8282\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u7ed8\u5236\u997c\u56fe\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u997c\u56fe\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fMatplotlib\u548cSeaborn\u3002Matplotlib\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5305\u62ec\u997c\u56fe\u3002Seaborn\u5219\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u63a5\u53e3\uff0c\u66f4\u52a0\u7f8e\u89c2\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002\u5982\u679c\u4f60\u662f\u521d\u5b66\u8005\uff0c\u53ef\u4ee5\u4eceMatplotlib\u5f00\u59cb\u3002<\/p>\n<p><strong>\u7ed8\u5236\u997c\u56fe\u65f6\u9700\u8981\u8003\u8651\u54ea\u4e9b\u6570\u636e\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u997c\u56fe\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684\u6570\u636e\u662f\u4ee5\u9002\u5f53\u7684\u683c\u5f0f\u7ec4\u7ec7\u7684\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u997c\u56fe\u9700\u8981\u4e00\u4e2a\u5305\u542b\u5206\u7c7b\u6807\u7b7e\u548c\u5bf9\u5e94\u503c\u7684\u5217\u8868\u6216\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u952e\u53ef\u4ee5\u662f\u5206\u7c7b\u540d\u79f0\uff0c\u503c\u662f\u6570\u91cf\u3002\u786e\u4fdd\u6570\u636e\u7684\u603b\u548c\u80fd\u591f\u53cd\u6620\u51fa\u5404\u90e8\u5206\u7684\u6bd4\u4f8b\uff0c\u8fd9\u6837\u997c\u56fe\u624d\u80fd\u51c6\u786e\u663e\u793a\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u997c\u56fe\u7684\u5916\u89c2\uff1f<\/strong><br \/>Python\u7684\u7ed8\u56fe\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u81ea\u5b9a\u4e49\u9009\u9879\u6765\u4f18\u5316\u997c\u56fe\u7684\u5916\u89c2\u3002\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u989c\u8272\u3001\u9634\u5f71\u3001\u6807\u7b7e\u5b57\u4f53\u5927\u5c0f\u3001\u56fe\u4f8b\u4f4d\u7f6e\u7b49\u3002\u901a\u8fc7\u8c03\u6574<code>explode<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u7a81\u51fa\u663e\u793a\u67d0\u4e00\u90e8\u5206\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>autopct<\/code>\u53c2\u6570\u6765\u663e\u793a\u6bcf\u4e2a\u90e8\u5206\u7684\u767e\u5206\u6bd4\uff0c\u589e\u5f3a\u56fe\u8868\u7684\u4fe1\u606f\u4f20\u9012\u6548\u679c\u3002\u5229\u7528\u8fd9\u4e9b\u9009\u9879\uff0c\u53ef\u4ee5\u521b\u5efa\u65e2\u7f8e\u89c2\u53c8\u5b9e\u7528\u7684\u997c\u56fe\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u753b\u997c\u56fe\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u5229\u7528Matplotlib\u5e93\u3001\u8bbe\u7f6e\u6570\u636e\u548c\u6807\u7b7e\u3001\u8c03\u6574\u56fe\u5f62\u5c5e\u6027\u3001\u6dfb\u52a0\u767e\u5206\u6bd4\u548c\u6807 [&hellip;]","protected":false},"author":3,"featured_media":970145,"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\/970137"}],"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=970137"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970137\/revisions"}],"predecessor-version":[{"id":970147,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970137\/revisions\/970147"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/970145"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=970137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=970137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=970137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}