{"id":1098457,"date":"2025-01-08T15:21:20","date_gmt":"2025-01-08T07:21:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098457.html"},"modified":"2025-01-08T15:21:23","modified_gmt":"2025-01-08T07:21:23","slug":"python%e7%bb%98%e5%88%b6%e9%a5%bc%e7%8a%b6%e5%9b%be%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e6%b1%89%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098457.html","title":{"rendered":"python\u7ed8\u5236\u997c\u72b6\u56fe\u5982\u4f55\u663e\u793a\u6c49\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062637\/6d4a40b6-ddb8-4a73-8c2a-78b9e45f706d.webp\" alt=\"python\u7ed8\u5236\u997c\u72b6\u56fe\u5982\u4f55\u663e\u793a\u6c49\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7ed8\u5236\u997c\u72b6\u56fe\u5e76\u663e\u793a\u6c49\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\u8bbe\u7f6e\u5b57\u4f53\u3001\u4f7f\u7528\u5408\u9002\u7684\u5e93\u3001\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u3002\u5173\u952e\u6b65\u9aa4\u662f\u8bbe\u7f6e\u5b57\u4f53\u4ee5\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u6240\u9700\u5e93<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5b89\u88c5\u4e86<code>matplotlib<\/code>\u5e93\uff0c\u56e0\u4e3a\u8fd9\u662f\u7ed8\u5236\u997c\u72b6\u56fe\u7684\u4e3b\u8981\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\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>\u4e8c\u3001\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u997c\u72b6\u56fe\u4e2d\u7684\u6807\u7b7e\u80fd\u591f\u6b63\u786e\u663e\u793a\u6c49\u5b57\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u5b57\u4f53\u3002<code>matplotlib<\/code>\u9ed8\u8ba4\u7684\u5b57\u4f53\u4e0d\u652f\u6301\u4e2d\u6587\u5b57\u7b26\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\uff0c\u4f8b\u5982SimHei\uff08\u9ed1\u4f53\uff09\u3002\u4ee5\u4e0b\u662f\u8bbe\u7f6e\u5b57\u4f53\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53\u4e3aSimHei\uff0c\u786e\u4fdd\u80fd\u663e\u793a\u4e2d\u6587<\/strong><\/h2>\n<p>matplotlib.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;]<\/p>\n<h2><strong>\u89e3\u51b3\u8d1f\u53f7\u663e\u793a\u95ee\u9898<\/strong><\/h2>\n<p>matplotlib.rcParams[&#39;axes.unicode_minus&#39;] = False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u7ed8\u5236\u997c\u72b6\u56fe<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u4e2d\u7684<code>pie<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u997c\u72b6\u56fe\uff0c\u5e76\u901a\u8fc7\u53c2\u6570\u8bbe\u7f6e\u6765\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>labels = [&#39;\u82f9\u679c&#39;, &#39;\u9999\u8549&#39;, &#39;\u6a58\u5b50&#39;, &#39;\u8461\u8404&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<p>colors = [&#39;gold&#39;, &#39;yellowgreen&#39;, &#39;lightcoral&#39;, &#39;lightskyblue&#39;]<\/p>\n<p>explode = (0.1, 0, 0, 0)  # \u4f7f\u82f9\u679c\u90a3\u90e8\u5206\u7a81\u51fa<\/p>\n<p>plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct=&#39;%1.1f%%&#39;, shadow=True, startangle=140)<\/p>\n<p>plt.axis(&#39;equal&#39;)  # \u4f7f\u997c\u56fe\u4e3a\u6b63\u5706<\/p>\n<p>plt.title(&#39;\u6c34\u679c\u5206\u5e03\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u7ed8\u5236\u4e00\u4e2a\u997c\u72b6\u56fe\uff0c\u5176\u4e2d\u5305\u542b\u4e2d\u6587\u6807\u7b7e\u3002\u6211\u4eec\u901a\u8fc7<code>labels<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e86\u6807\u7b7e\uff0c\u5e76\u901a\u8fc7<code>matplotlib.rcParams<\/code>\u8bbe\u7f6e\u4e86\u5b57\u4f53\uff0c\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u80fd\u591f\u6b63\u786e\u663e\u793a\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6df1\u5165\u7406\u89e3\u4e0e\u4f18\u5316<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b57\u4f53\u8def\u5f84\u8bbe\u7f6e<\/strong>\uff1a\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u5b57\u4f53\u7684\u5177\u4f53\u8def\u5f84\u6765\u786e\u4fdd\u80fd\u591f\u627e\u5230\u76f8\u5e94\u7684\u5b57\u4f53\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from matplotlib.font_manager import FontProperties<\/p>\n<p>font = FontProperties(fname=&#39;\/path\/to\/SimHei.ttf&#39;)<\/p>\n<p>plt.title(&#39;\u6c34\u679c\u5206\u5e03\u56fe&#39;, fontproperties=font)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u636e\u6765\u6e90\u4e0e\u52a8\u6001\u66f4\u65b0<\/strong>\uff1a\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u5f80\u5f80\u662f\u52a8\u6001\u7684\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u6216\u6570\u636e\u5e93\u6765\u83b7\u53d6\u6570\u636e\uff0c\u7136\u540e\u52a8\u6001\u751f\u6210\u997c\u72b6\u56fe\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u636e\u5b58\u50a8\u5728CSV\u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;fruit_data.csv&#39;)<\/p>\n<p>labels = data[&#39;Fruit&#39;]<\/p>\n<p>sizes = data[&#39;Quantity&#39;]<\/p>\n<p>plt.pie(sizes, labels=labels, 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<ol start=\"3\">\n<li><strong>\u7f8e\u89c2\u4e0e\u5b9a\u5236\u5316<\/strong>\uff1a\u4e3a\u4e86\u8ba9\u997c\u72b6\u56fe\u66f4\u52a0\u7f8e\u89c2\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u63d0\u4f9b\u7684\u5404\u79cd\u53c2\u6570\u8fdb\u884c\u5b9a\u5236\u3002\u4f8b\u5982\uff0c\u8c03\u6574\u989c\u8272\u3001\u9634\u5f71\u3001\u8d77\u59cb\u89d2\u5ea6\u7b49\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">plt.pie(sizes, labels=labels, colors=colors, autopct=&#39;%1.1f%%&#39;, shadow=True, startangle=140, pctdistance=0.85)<\/p>\n<p>plt.gca().set_aspect(&#39;equal&#39;)  # \u4fdd\u6301\u7eb5\u6a2a\u6bd4\u76f8\u540c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e2d\u6587\u4e71\u7801\u95ee\u9898<\/strong>\uff1a\u5982\u679c\u5728\u663e\u793a\u4e2d\u6587\u65f6\u51fa\u73b0\u4e71\u7801\uff0c\u9996\u5148\u786e\u4fdd\u4f60\u5df2\u7ecf\u6b63\u786e\u8bbe\u7f6e\u4e86\u5b57\u4f53\u8def\u5f84\uff0c\u53e6\u5916\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u5176\u4ed6\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8d1f\u53f7\u663e\u793a\u95ee\u9898<\/strong>\uff1a\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8d1f\u53f7\u53ef\u80fd\u4f1a\u663e\u793a\u4e3a\u65b9\u5757\uff0c\u8fd9\u662f\u56e0\u4e3a\u9ed8\u8ba4\u5b57\u4f53\u4e0d\u652f\u6301\u8d1f\u53f7\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u89e3\u51b3\uff1a<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">matplotlib.rcParams[&#39;axes.unicode_minus&#39;] = False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5b57\u4f53\u6587\u4ef6\u95ee\u9898<\/strong>\uff1a\u5982\u679c\u7cfb\u7edf\u4e2d\u6ca1\u6709\u5b89\u88c5SimHei\u5b57\u4f53\uff0c\u53ef\u4ee5\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002\u4e00\u822c\u6765\u8bf4\uff0cWindows\u7cfb\u7edf\u81ea\u5e26SimHei\u5b57\u4f53\uff0c\u800c\u5728Linux\u6216MacOS\u7cfb\u7edf\u4e2d\u9700\u8981\u624b\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5b9e\u8df5\u4e0e\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed8\u5236\u997c\u72b6\u56fe\u5e76\u663e\u793a\u6c49\u5b57\u7684\u9700\u6c42\u5e7f\u6cdb\u5b58\u5728\u4e8e\u6570\u636e\u5206\u6790\u548c\u62a5\u544a\u4e2d\u3002\u4f8b\u5982\uff0c\u5206\u6790\u5e02\u573a\u4efd\u989d\u3001\u7528\u6237\u5206\u5e03\u3001\u9500\u552e\u6bd4\u4f8b\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408\u591a\u4e2a\u56fe\u8868\u8fdb\u884c\u7efc\u5408\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>categories = [&#39;\u6c34\u679c&#39;, &#39;\u852c\u83dc&#39;, &#39;\u96f6\u98df&#39;]<\/p>\n<p>data = {<\/p>\n<p>    &#39;\u6c34\u679c&#39;: [20, 35, 30, 15],<\/p>\n<p>    &#39;\u852c\u83dc&#39;: [25, 32, 34, 9],<\/p>\n<p>    &#39;\u96f6\u98df&#39;: [30, 10, 20, 40]<\/p>\n<p>}<\/p>\n<p>labels = [&#39;\u82f9\u679c&#39;, &#39;\u9999\u8549&#39;, &#39;\u6a58\u5b50&#39;, &#39;\u8461\u8404&#39;]<\/p>\n<p>colors = [&#39;gold&#39;, &#39;yellowgreen&#39;, &#39;lightcoral&#39;, &#39;lightskyblue&#39;]<\/p>\n<p>explode = (0.1, 0, 0, 0)<\/p>\n<p>fig, axes = plt.subplots(1, 3, figsize=(18, 6))<\/p>\n<p>for ax, category in zip(axes, categories):<\/p>\n<p>    ax.pie(data[category], explode=explode, labels=labels, colors=colors, autopct=&#39;%1.1f%%&#39;, shadow=True, startangle=140)<\/p>\n<p>    ax.axis(&#39;equal&#39;)<\/p>\n<p>    ax.set_title(f&#39;{category}\u5206\u5e03\u56fe&#39;, fontproperties=font)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u6210\u529f\u7ed8\u5236\u997c\u72b6\u56fe\u5e76\u6b63\u786e\u663e\u793a\u6c49\u5b57\u3002\u65e0\u8bba\u662f\u5728\u6570\u636e\u5206\u6790\u8fd8\u662f\u5728\u62a5\u544a\u751f\u6210\u4e2d\uff0c\u8fd9\u90fd\u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u53ef\u89c6\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7ed8\u5236\u7684\u997c\u72b6\u56fe\u4e2d\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728\u997c\u72b6\u56fe\u4e2d\u6b63\u786e\u663e\u793a\u6c49\u5b57\uff0c\u9700\u8981\u786e\u4fdd\u4f7f\u7528\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u5e93\u3002\u5728\u4f7f\u7528Matplotlib\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;]<\/code>\u6765\u6307\u5b9a\u9ed1\u4f53\u4f5c\u4e3a\u5b57\u4f53\uff0c\u4ece\u800c\u786e\u4fdd\u6c49\u5b57\u80fd\u591f\u6b63\u786e\u663e\u793a\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5728Python\u4e2d\u7ed8\u5236\u997c\u72b6\u56fe\uff0c\u5e76\u652f\u6301\u6c49\u5b57\u663e\u793a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u6709Matplotlib\u548cSeaborn\u3002Matplotlib\u662f\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u53ef\u4ee5\u8f7b\u677e\u652f\u6301\u6c49\u5b57\u663e\u793a\u3002Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u63a5\u53e3\uff0c\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u76f8\u4f3c\u7684\u65b9\u5f0f\u6765\u89e3\u51b3\u6c49\u5b57\u663e\u793a\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728\u7ed8\u5236\u997c\u72b6\u56fe\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6c49\u5b57\u663e\u793a\u4e0d\u5168\u6216\u4e71\u7801\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u6c49\u5b57\u663e\u793a\u4e0d\u5168\u6216\u51fa\u73b0\u4e71\u7801\u901a\u5e38\u662f\u7531\u4e8e\u5b57\u4f53\u8bbe\u7f6e\u4e0d\u5f53\u3002\u786e\u4fdd\u5728\u811a\u672c\u4e2d\u6b63\u786e\u8bbe\u7f6e\u4e86\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\uff0c\u5e76\u4e14\u5728\u7ed8\u56fe\u524d\u52a0\u8f7d\u4e86\u8fd9\u4e9b\u8bbe\u7f6e\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u68c0\u67e5\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u662f\u5426\u5b89\u88c5\u4e86\u6240\u9700\u7684\u5b57\u4f53\u5e93\uff0c\u4ee5\u907f\u514d\u56e0\u7f3a\u5931\u5b57\u4f53\u5bfc\u81f4\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed8\u5236\u997c\u72b6\u56fe\u5e76\u663e\u793a\u6c49\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\u8bbe\u7f6e\u5b57\u4f53\u3001\u4f7f\u7528\u5408\u9002\u7684\u5e93\u3001\u6b63\u786e\u663e\u793a\u4e2d\u6587\u5b57\u7b26\u3002\u5173\u952e\u6b65\u9aa4\u662f\u8bbe\u7f6e\u5b57\u4f53\u4ee5\u652f [&hellip;]","protected":false},"author":3,"featured_media":1098460,"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\/1098457"}],"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=1098457"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098457\/revisions"}],"predecessor-version":[{"id":1098465,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098457\/revisions\/1098465"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098460"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}