{"id":1026343,"date":"2024-12-31T10:41:47","date_gmt":"2024-12-31T02:41:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026343.html"},"modified":"2024-12-31T10:41:49","modified_gmt":"2024-12-31T02:41:49","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e6%80%a7%e5%88%ab%e6%af%94%e4%be%8b%e9%a5%bc%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1026343.html","title":{"rendered":"python\u5982\u4f55\u753b\u6027\u522b\u6bd4\u4f8b\u997c\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/00e8a74d-a1f8-4b8b-9495-5fcb0b9821b7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u753b\u6027\u522b\u6bd4\u4f8b\u997c\u56fe\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u753b\u6027\u522b\u6bd4\u4f8b\u997c\u56fe<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u753b\u51fa\u6027\u522b\u6bd4\u4f8b\u997c\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002<strong>\u5bfc\u5165\u6570\u636e\u3001\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u997c\u56fe\u3001\u6dfb\u52a0\u6807\u7b7e\u548c\u767e\u5206\u6bd4\u3001\u8bbe\u7f6e\u56fe\u5f62\u6807\u9898<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u5305\u542b\u6027\u522b\u4fe1\u606f\u7684\u6570\u636e\u3002\u6570\u636e\u53ef\u4ee5\u6765\u81eaCSV\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216\u5176\u4ed6\u6570\u636e\u6e90\u3002\u4ee5\u4e0b\u793a\u4f8b\u4f7f\u7528Pandas\u5e93\u4eceCSV\u6587\u4ef6\u4e2d\u5bfc\u5165\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u524d\u51e0\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u997c\u56fe<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Matplotlib\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u997c\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8ba1\u7b97\u5404\u6027\u522b\u7684\u6570\u91cf<\/strong><\/h2>\n<p>gender_counts = data[&#39;Gender&#39;].value_counts()<\/p>\n<h2><strong>\u5b9a\u4e49\u997c\u56fe\u7684\u6807\u7b7e\u548c\u6570\u636e<\/strong><\/h2>\n<p>labels = gender_counts.index<\/p>\n<p>sizes = gender_counts.values<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>plt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;, startangle=140)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.axis(&#39;equal&#39;)  # \u4fdd\u6301\u997c\u56fe\u4e3a\u5706\u5f62<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u6807\u7b7e\u548c\u767e\u5206\u6bd4<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u997c\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>autopct<\/code>\u53c2\u6570\u6dfb\u52a0\u767e\u5206\u6bd4\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;, startangle=140)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>autopct<\/code>\u53c2\u6570\u63a7\u5236\u767e\u5206\u6bd4\u7684\u663e\u793a\u683c\u5f0f\u3002<code>%1.1f%%<\/code>\u8868\u793a\u663e\u793a\u4e00\u4e2a\u5c0f\u6570\u70b9\u540e\u7684\u767e\u5206\u6bd4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8bbe\u7f6e\u56fe\u5f62\u6807\u9898<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u56fe\u5f62\u66f4\u5177\u63cf\u8ff0\u6027\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u6807\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.title(&#39;Gender Distribution&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u4ece\u6570\u636e\u5bfc\u5165\u5230\u7ed8\u5236\u5e76\u663e\u793a\u6027\u522b\u6bd4\u4f8b\u997c\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u5404\u6027\u522b\u7684\u6570\u91cf<\/strong><\/h2>\n<p>gender_counts = data[&#39;Gender&#39;].value_counts()<\/p>\n<h2><strong>\u5b9a\u4e49\u997c\u56fe\u7684\u6807\u7b7e\u548c\u6570\u636e<\/strong><\/h2>\n<p>labels = gender_counts.index<\/p>\n<p>sizes = gender_counts.values<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>plt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;, startangle=140)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>plt.title(&#39;Gender Distribution&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.axis(&#39;equal&#39;)  # \u4fdd\u6301\u997c\u56fe\u4e3a\u5706\u5f62<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f18\u5316\u997c\u56fe<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u997c\u56fe\u66f4\u52a0\u7f8e\u89c2\u548c\u4e13\u4e1a\uff0c\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u4f18\u5316\uff0c\u6bd4\u5982\u6dfb\u52a0\u9634\u5f71\u3001\u7a81\u51fa\u67d0\u4e2a\u90e8\u5206\u6216\u8bbe\u7f6e\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6dfb\u52a0\u9634\u5f71<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>shadow<\/code>\u53c2\u6570\u6dfb\u52a0\u9634\u5f71\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;, startangle=140, shadow=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7a81\u51fa\u67d0\u4e2a\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>explode<\/code>\u53c2\u6570\u7a81\u51fa\u67d0\u4e2a\u90e8\u5206\uff0c\u4f8b\u5982\u7a81\u51fa\u5973\u6027\u6bd4\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">explode = (0.1, 0)  # \u7a81\u51fa\u663e\u793a\u7b2c\u4e00\u90e8\u5206\uff08\u5973\u6027\uff09<\/p>\n<p>plt.pie(sizes, explode=explode, labels=labels, autopct=&#39;%1.1f%%&#39;, startangle=140)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8bbe\u7f6e\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>colors<\/code>\u53c2\u6570\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u989c\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">colors = [&#39;#ff9999&#39;,&#39;#66b3ff&#39;]<\/p>\n<p>plt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;, startangle=140, colors=colors)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5305\u542b\u4e0a\u8ff0\u6240\u6709\u4f18\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u5404\u6027\u522b\u7684\u6570\u91cf<\/strong><\/h2>\n<p>gender_counts = data[&#39;Gender&#39;].value_counts()<\/p>\n<h2><strong>\u5b9a\u4e49\u997c\u56fe\u7684\u6807\u7b7e\u548c\u6570\u636e<\/strong><\/h2>\n<p>labels = gender_counts.index<\/p>\n<p>sizes = gender_counts.values<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u989c\u8272<\/strong><\/h2>\n<p>colors = [&#39;#ff9999&#39;,&#39;#66b3ff&#39;]<\/p>\n<h2><strong>\u7a81\u51fa\u663e\u793a\u5973\u6027\u90e8\u5206<\/strong><\/h2>\n<p>explode = (0.1, 0) <\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>plt.pie(sizes, explode=explode, labels=labels, autopct=&#39;%1.1f%%&#39;, startangle=140, colors=colors, shadow=True)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>plt.title(&#39;Gender Distribution&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.axis(&#39;equal&#39;)  # \u4fdd\u6301\u997c\u56fe\u4e3a\u5706\u5f62<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7ed8\u5236\u4e00\u4e2a\u4e13\u4e1a\u7684\u6027\u522b\u6bd4\u4f8b\u997c\u56fe\u3002<strong>\u5bfc\u5165\u6570\u636e\u3001\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u997c\u56fe\u3001\u6dfb\u52a0\u6807\u7b7e\u548c\u767e\u5206\u6bd4\u3001\u8bbe\u7f6e\u56fe\u5f62\u6807\u9898\u3001\u4f18\u5316\u997c\u56fe<\/strong>\uff0c\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u4ee5\u8ba9\u60a8\u521b\u5efa\u51fa\u66f4\u7f8e\u89c2\u548c\u4fe1\u606f\u4e30\u5bcc\u7684\u56fe\u8868\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u4e0e\u6211\u4eec\u8054\u7cfb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u6027\u522b\u6bd4\u4f8b\u997c\u56fe\uff1f<\/strong><br \/>\u8981\u7ed8\u5236\u6027\u522b\u6bd4\u4f8b\u997c\u56fe\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684Matplotlib\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Matplotlib\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install matplotlib<\/code>\u6765\u5b8c\u6210\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u51c6\u5907\u6027\u522b\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528<code>plt.pie()<\/code>\u51fd\u6570\u7ed8\u5236\u997c\u56fe\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u6027\u522b\u6570\u636e\nlabels = [&#39;\u7537\u6027&#39;, &#39;\u5973\u6027&#39;]\nsizes = [60, 40]  # \u5047\u8bbe60%\u662f\u7537\u6027\uff0c40%\u662f\u5973\u6027\ncolors = [&#39;#ff9999&#39;,&#39;#66b3ff&#39;]  # \u81ea\u5b9a\u4e49\u989c\u8272\nexplode = (0.1, 0)  # \u7a81\u51fa\u663e\u793a\u7537\u6027\u90e8\u5206\n\nplt.pie(sizes, explode=explode, labels=labels, colors=colors,\n        autopct=&#39;%1.1f%%&#39;, shadow=True, startangle=90)\nplt.axis(&#39;equal&#39;)  # \u4f7f\u997c\u56fe\u4e3a\u5706\u5f62\nplt.title(&#39;\u6027\u522b\u6bd4\u4f8b\u997c\u56fe&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u997c\u56fe\u7684\u989c\u8272\u548c\u6807\u7b7e\u5982\u4f55\u81ea\u5b9a\u4e49\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u997c\u56fe\u65f6\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4f20\u5165\u989c\u8272\u5217\u8868\u548c\u6807\u7b7e\u5217\u8868\u6765\u5b9e\u73b0\u81ea\u5b9a\u4e49\u3002<code>colors<\/code>\u53c2\u6570\u63a5\u53d7\u4e00\u4e2a\u989c\u8272\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u6216\u5e38\u7528\u989c\u8272\u540d\u79f0\u3002<code>labels<\/code>\u53c2\u6570\u5219\u7528\u4e8e\u5b9a\u4e49\u6bcf\u4e2a\u6247\u533a\u7684\u540d\u79f0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u997c\u56fe\u4e2d\u6dfb\u52a0\u767e\u5206\u6bd4\u663e\u793a\uff1f<\/strong><br \/>\u5728<code>plt.pie()<\/code>\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>autopct<\/code>\u53c2\u6570\u6765\u663e\u793a\u767e\u5206\u6bd4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>autopct=&#39;%1.1f%%&#39;<\/code>\u5c06\u663e\u793a\u6bcf\u4e2a\u90e8\u5206\u7684\u767e\u5206\u6bd4\uff0c\u683c\u5f0f\u4e3a\u4e00\u4f4d\u5c0f\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u8ba9\u89c2\u4f17\u66f4\u6e05\u6670\u5730\u7406\u89e3\u6570\u636e\u6240\u4ee3\u8868\u7684\u6bd4\u4f8b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6570\u636e\u4e2d\u7684\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6027\u522b\u6bd4\u4f8b\u6570\u636e\u65f6\uff0c\u7f3a\u5931\u503c\u53ef\u80fd\u4f1a\u5f71\u54cd\u997c\u56fe\u7684\u51c6\u786e\u6027\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u6e05\u6d17\u6570\u636e\uff0c\u786e\u4fdd\u5728\u7ed8\u5236\u997c\u56fe\u524d\u7edf\u8ba1\u51fa\u6709\u6548\u7684\u6027\u522b\u6570\u636e\u3002\u4f7f\u7528<code>dropna()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u53bb\u9664\u7f3a\u5931\u503c\uff0c\u786e\u4fdd\u7ed8\u5236\u7684\u997c\u56fe\u53cd\u6620\u771f\u5b9e\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u753b\u6027\u522b\u6bd4\u4f8b\u997c\u56fe \u8981\u5728Python\u4e2d\u753b\u51fa\u6027\u522b\u6bd4\u4f8b\u997c\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u5bfc\u5165\u6570\u636e\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1026352,"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\/1026343"}],"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=1026343"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026343\/revisions"}],"predecessor-version":[{"id":1026358,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026343\/revisions\/1026358"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1026352"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1026343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1026343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1026343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}