{"id":1181138,"date":"2025-01-15T18:47:28","date_gmt":"2025-01-15T10:47:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1181138.html"},"modified":"2025-01-15T18:47:31","modified_gmt":"2025-01-15T10:47:31","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bb%98%e5%88%b6%e9%a5%bc%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1181138.html","title":{"rendered":"\u5982\u4f55\u7528Python\u7ed8\u5236\u997c\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25125606\/b71e477a-3dda-4a43-91da-bf3dc79cf64d.webp\" alt=\"\u5982\u4f55\u7528Python\u7ed8\u5236\u997c\u56fe\" \/><\/p>\n<p><p> \u8981\u7528Python\u7ed8\u5236\u997c\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>Matplotlib<\/strong>\u5e93\u6216<strong>Plotly<\/strong>\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u5730\u521b\u5efa\u548c\u81ea\u5b9a\u4e49\u997c\u56fe\u3002<strong>\u4f7f\u7528Matplotlib\u5e93\u3001\u4f7f\u7528Plotly\u5e93<\/strong>\uff0c\u5176\u4e2dMatplotlib\u5e93\u66f4\u4e3a\u5e38\u89c1\u548c\u57fa\u7840\uff0c\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p><strong>Matplotlib<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u56fe\u8868\u7ed8\u5236\u4efb\u52a1\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u997c\u56fe\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Matplotlib\u5e93<\/h4>\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\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><h4>2. \u5bfc\u5165Matplotlib\u5e93\u5e76\u7ed8\u5236\u57fa\u672c\u997c\u56fe<\/h4>\n<\/p>\n<p><p>\u7ed8\u5236\u997c\u56fe\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\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;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#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)  # \u7a81\u51fa\u663e\u793aA\u90e8\u5206<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\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;)  # \u4fdd\u8bc1\u997c\u56fe\u662f\u5706\u7684<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u7ed8\u5236\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u997c\u56fe\uff0c\u5e76\u901a\u8fc7<code>explode<\/code>\u53c2\u6570\u5c06\u5176\u4e2d\u4e00\u4e2a\u90e8\u5206\u7a81\u51fa\u663e\u793a\u3002<code>autopct<\/code>\u53c2\u6570\u7528\u4e8e\u663e\u793a\u6bcf\u4e2a\u90e8\u5206\u7684\u767e\u5206\u6bd4\uff0c<code>shadow<\/code>\u53c2\u6570\u7528\u4e8e\u6dfb\u52a0\u9634\u5f71\u6548\u679c\uff0c<code>startangle<\/code>\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u8d77\u59cb\u89d2\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>3. \u81ea\u5b9a\u4e49\u997c\u56fe<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u997c\u56fe\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\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;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#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)  # \u7a81\u51fa\u663e\u793aA\u90e8\u5206<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.pie(sizes, explode=explode, labels=labels, colors=colors,<\/p>\n<p>       autopct=&#39;%1.1f%%&#39;, shadow=True, startangle=140)<\/p>\n<p>ax.axis(&#39;equal&#39;)  # \u4fdd\u8bc1\u997c\u56fe\u662f\u5706\u7684<\/p>\n<p>plt.title(&#39;Customized Pie Chart&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>fig, ax = plt.subplots()<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u548c\u4e00\u4e2a\u5b50\u56fe\uff0c\u53ef\u4ee5\u5bf9\u56fe\u8868\u8fdb\u884c\u66f4\u591a\u7684\u5b9a\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Plotly\u5e93<\/h3>\n<\/p>\n<p><p><strong>Plotly<\/strong>\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Plotly\u5e93\u7ed8\u5236\u997c\u56fe\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Plotly\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Plotly\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install plotly<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bfc\u5165Plotly\u5e93\u5e76\u7ed8\u5236\u57fa\u672c\u997c\u56fe<\/h4>\n<\/p>\n<p><p>\u7ed8\u5236\u997c\u56fe\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>labels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>fig = go.Figure(data=[go.Pie(labels=labels, values=sizes)])<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528Plotly\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u997c\u56fe\u3002<\/p>\n<\/p>\n<p><h4>3. \u81ea\u5b9a\u4e49\u997c\u56fe<\/h4>\n<\/p>\n<p><p>Plotly\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>labels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>fig = go.Figure(data=[go.Pie(labels=labels, values=sizes, <\/p>\n<p>                             hole=.3, # \u521b\u5efa\u73af\u5f62\u56fe<\/p>\n<p>                             pull=[0.1, 0, 0, 0])]) # \u7a81\u51fa\u663e\u793aA\u90e8\u5206<\/p>\n<p>fig.update_layout(title_text=&#39;Customized Pie Chart&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>hole<\/code>\u53c2\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u73af\u5f62\u56fe\uff0c\u4f7f\u7528<code>pull<\/code>\u53c2\u6570\u5c06\u5176\u4e2d\u4e00\u4e2a\u90e8\u5206\u7a81\u51fa\u663e\u793a\uff0c\u5e76\u901a\u8fc7<code>update_layout<\/code>\u51fd\u6570\u8bbe\u7f6e\u4e86\u56fe\u8868\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u4f7f\u7528Matplotlib\u5e93\u548cPlotly\u5e93\u90fd\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u997c\u56fe\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u81ea\u5b9a\u4e49<\/strong>\u3002Matplotlib\u5e93\u9002\u5408\u521d\u5b66\u8005\uff0c\u63d0\u4f9b\u4e86\u57fa\u7840\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u800cPlotly\u5e93\u5219\u9002\u7528\u4e8e\u9700\u8981\u4ea4\u4e92\u5f0f\u56fe\u8868\u7684\u573a\u666f\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u5e93\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u9009\u62e9\uff0c\u5e76\u7ed3\u5408\u5177\u4f53\u7684\u53c2\u6570\u548c\u65b9\u6cd5\uff0c\u7ed8\u5236\u51fa\u6ee1\u8db3\u9700\u6c42\u7684\u997c\u56fe\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u6bcf\u4e2a\u6b65\u9aa4\u548c\u53c2\u6570\u7684\u5177\u4f53\u7528\u6cd5\u548c\u5e94\u7528\u573a\u666f\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u638c\u63e1Python\u7ed8\u5236\u997c\u56fe\u7684\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8be6\u7ec6\u63a2\u8ba8Matplotlib\u5e93\u7684\u7528\u6cd5<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u53c2\u6570\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u997c\u56fe\u65f6\uff0c\u5e38\u7528\u7684\u53c2\u6570\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>labels<\/code>\uff1a\u6807\u7b7e\uff0c\u8868\u793a\u997c\u56fe\u6bcf\u4e2a\u90e8\u5206\u7684\u540d\u79f0\u3002<\/li>\n<li><code>sizes<\/code>\uff1a\u5927\u5c0f\uff0c\u8868\u793a\u997c\u56fe\u6bcf\u4e2a\u90e8\u5206\u7684\u6570\u503c\u3002<\/li>\n<li><code>colors<\/code>\uff1a\u989c\u8272\uff0c\u8868\u793a\u997c\u56fe\u6bcf\u4e2a\u90e8\u5206\u7684\u989c\u8272\u3002<\/li>\n<li><code>explode<\/code>\uff1a\u7a81\u51fa\u663e\u793a\uff0c\u8868\u793a\u5c06\u67d0\u4e2a\u90e8\u5206\u4ece\u997c\u56fe\u4e2d\u7a81\u51fa\u663e\u793a\u3002<\/li>\n<li><code>autopct<\/code>\uff1a\u81ea\u52a8\u767e\u5206\u6bd4\uff0c\u8868\u793a\u5728\u997c\u56fe\u4e0a\u663e\u793a\u6bcf\u4e2a\u90e8\u5206\u7684\u767e\u5206\u6bd4\u3002<\/li>\n<li><code>shadow<\/code>\uff1a\u9634\u5f71\uff0c\u8868\u793a\u5728\u997c\u56fe\u4e0a\u6dfb\u52a0\u9634\u5f71\u6548\u679c\u3002<\/li>\n<li><code>startangle<\/code>\uff1a\u8d77\u59cb\u89d2\u5ea6\uff0c\u8868\u793a\u997c\u56fe\u7684\u8d77\u59cb\u89d2\u5ea6\u3002<\/li>\n<\/ul>\n<p><h4>2. \u81ea\u5b9a\u4e49\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>colors<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u6bcf\u4e2a\u90e8\u5206\u7684\u989c\u8272\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;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<p>colors = [&#39;#ff9999&#39;,&#39;#66b3ff&#39;,&#39;#99ff99&#39;,&#39;#ffcc99&#39;]<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>plt.pie(sizes, labels=labels, colors=colors, 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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u4e86\u81ea\u5b9a\u4e49\u7684\u989c\u8272\u5217\u8868\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u90e8\u5206\u7684\u989c\u8272\u66f4\u52a0\u4e30\u5bcc\u591a\u5f69\u3002<\/p>\n<\/p>\n<p><h4>3. \u7a81\u51fa\u663e\u793a\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>explode<\/code>\u53c2\u6570\u5c06\u67d0\u4e2a\u90e8\u5206\u4ece\u997c\u56fe\u4e2d\u7a81\u51fa\u663e\u793a\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;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<p>explode = (0.1, 0, 0, 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;, 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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5c06\u7b2c\u4e00\u4e2a\u90e8\u5206\u4ece\u997c\u56fe\u4e2d\u7a81\u51fa\u663e\u793a\uff0c\u4f7f\u5176\u66f4\u52a0\u9192\u76ee\u3002<\/p>\n<\/p>\n<p><h4>4. \u6dfb\u52a0\u9634\u5f71\u548c\u8bbe\u7f6e\u8d77\u59cb\u89d2\u5ea6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>shadow<\/code>\u53c2\u6570\u6dfb\u52a0\u9634\u5f71\u6548\u679c\uff0c\u901a\u8fc7<code>startangle<\/code>\u53c2\u6570\u8bbe\u7f6e\u8d77\u59cb\u89d2\u5ea6\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;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>plt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;, shadow=True, startangle=90)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6dfb\u52a0\u4e86\u9634\u5f71\u6548\u679c\uff0c\u5e76\u5c06\u8d77\u59cb\u89d2\u5ea6\u8bbe\u7f6e\u4e3a90\u5ea6\uff0c\u4f7f\u5f97\u997c\u56fe\u7684\u89c6\u89c9\u6548\u679c\u66f4\u52a0\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8be6\u7ec6\u63a2\u8ba8Plotly\u5e93\u7684\u7528\u6cd5<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u53c2\u6570\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Plotly\u5e93\u7ed8\u5236\u997c\u56fe\u65f6\uff0c\u5e38\u7528\u7684\u53c2\u6570\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>labels<\/code>\uff1a\u6807\u7b7e\uff0c\u8868\u793a\u997c\u56fe\u6bcf\u4e2a\u90e8\u5206\u7684\u540d\u79f0\u3002<\/li>\n<li><code>values<\/code>\uff1a\u6570\u503c\uff0c\u8868\u793a\u997c\u56fe\u6bcf\u4e2a\u90e8\u5206\u7684\u6570\u503c\u3002<\/li>\n<li><code>hole<\/code>\uff1a\u5b54\u5f84\uff0c\u8868\u793a\u521b\u5efa\u73af\u5f62\u56fe\u7684\u5b54\u5f84\u5927\u5c0f\u3002<\/li>\n<li><code>pull<\/code>\uff1a\u7a81\u51fa\u663e\u793a\uff0c\u8868\u793a\u5c06\u67d0\u4e2a\u90e8\u5206\u4ece\u997c\u56fe\u4e2d\u7a81\u51fa\u663e\u793a\u3002<\/li>\n<\/ul>\n<p><h4>2. \u521b\u5efa\u73af\u5f62\u56fe<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>hole<\/code>\u53c2\u6570\u521b\u5efa\u73af\u5f62\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>labels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<h2><strong>\u521b\u5efa\u73af\u5f62\u56fe<\/strong><\/h2>\n<p>fig = go.Figure(data=[go.Pie(labels=labels, values=sizes, hole=.3)])<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u8bbe\u7f6e<code>hole<\/code>\u53c2\u6570\u4e3a0.3\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u5e26\u6709\u5b54\u5f84\u7684\u73af\u5f62\u56fe\u3002<\/p>\n<\/p>\n<p><h4>3. \u7a81\u51fa\u663e\u793a\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>pull<\/code>\u53c2\u6570\u5c06\u67d0\u4e2a\u90e8\u5206\u4ece\u997c\u56fe\u4e2d\u7a81\u51fa\u663e\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>labels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>fig = go.Figure(data=[go.Pie(labels=labels, values=sizes, pull=[0.1, 0, 0, 0])])<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5c06\u7b2c\u4e00\u4e2a\u90e8\u5206\u4ece\u997c\u56fe\u4e2d\u7a81\u51fa\u663e\u793a\uff0c\u4f7f\u5176\u66f4\u52a0\u9192\u76ee\u3002<\/p>\n<\/p>\n<p><h4>4. \u6dfb\u52a0\u56fe\u8868\u6807\u9898<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>update_layout<\/code>\u51fd\u6570\u6dfb\u52a0\u56fe\u8868\u6807\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>labels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>fig = go.Figure(data=[go.Pie(labels=labels, values=sizes)])<\/p>\n<p>fig.update_layout(title_text=&#39;Customized Pie Chart&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7<code>update_layout<\/code>\u51fd\u6570\u8bbe\u7f6e\u4e86\u56fe\u8868\u6807\u9898\uff0c\u4f7f\u56fe\u8868\u66f4\u52a0\u5b8c\u6574\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u4f7f\u7528Matplotlib\u5e93\u548cPlotly\u5e93\u90fd\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u548c\u81ea\u5b9a\u4e49\u997c\u56fe<\/strong>\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u997c\u56fe\u8fd8\u662f\u590d\u6742\u7684\u73af\u5f62\u56fe\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u548c\u5e94\u7528\uff0c\u7ed8\u5236\u51fa\u6ee1\u8db3\u9700\u6c42\u7684\u997c\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b9e\u8df5\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1. \u9500\u552e\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u997c\u56fe\u5e38\u7528\u4e8e\u5206\u6790\u9500\u552e\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5206\u6790\u5404\u4e2a\u4ea7\u54c1\u7c7b\u522b\u7684\u9500\u552e\u5360\u6bd4\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;Electronics&#39;, &#39;Clothing&#39;, &#39;Home Appliances&#39;, &#39;Books&#39;]<\/p>\n<p>sizes = [25, 35, 20, 20]<\/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<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\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;)<\/p>\n<p>plt.title(&#39;Sales Distribution by Category&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5206\u6790\u4e86\u5404\u4e2a\u4ea7\u54c1\u7c7b\u522b\u7684\u9500\u552e\u5360\u6bd4\uff0c\u5e76\u901a\u8fc7\u997c\u56fe\u76f4\u89c2\u5730\u5c55\u793a\u4e86\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2. \u5e02\u573a\u4efd\u989d\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u997c\u56fe\u4e5f\u5e38\u7528\u4e8e\u5206\u6790\u5e02\u573a\u4efd\u989d\u3002\u4f8b\u5982\uff0c\u5206\u6790\u5404\u4e2a\u54c1\u724c\u5728\u5e02\u573a\u4e2d\u7684\u5360\u6bd4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>labels = [&#39;Brand A&#39;, &#39;Brand B&#39;, &#39;Brand C&#39;, &#39;Brand D&#39;]<\/p>\n<p>sizes = [30, 25, 20, 25]<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>fig = go.Figure(data=[go.Pie(labels=labels, values=sizes, pull=[0.1, 0, 0, 0])])<\/p>\n<p>fig.update_layout(title_text=&#39;Market Share by Brand&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5206\u6790\u4e86\u5404\u4e2a\u54c1\u724c\u5728\u5e02\u573a\u4e2d\u7684\u5360\u6bd4\uff0c\u5e76\u901a\u8fc7\u997c\u56fe\u76f4\u89c2\u5730\u5c55\u793a\u4e86\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7ed3\u8bed<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\u548c\u5b9e\u8df5\u5e94\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u4f7f\u7528Python\u7ed8\u5236\u997c\u56fe\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u975e\u5e38\u4e30\u5bcc\u548c\u7075\u6d3b<\/strong>\u3002\u65e0\u8bba\u662f\u4f7f\u7528Matplotlib\u5e93\u8fd8\u662fPlotly\u5e93\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u548c\u5e94\u7528\uff0c\u7ed8\u5236\u51fa\u6ee1\u8db3\u9700\u6c42\u7684\u997c\u56fe\u3002\u5e0c\u671b\u672c\u6587\u7684\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\u7ed8\u5236\u997c\u56fe\u7684\u6280\u5de7\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u53d6\u5f97\u66f4\u597d\u7684\u6548\u679c\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 \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7ed8\u5236\u997c\u56fe\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fMatplotlib\u548cSeaborn\u3002Matplotlib\u662f\u4e00\u4e2a\u975e\u5e38\u7075\u6d3b\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u5404\u79cd\u7c7b\u578b\u7684\u53ef\u89c6\u5316\uff0c\u800cSeaborn\u5219\u66f4\u6ce8\u91cd\u7f8e\u89c2\u548c\u7b80\u6d01\uff0c\u9002\u5408\u5feb\u901f\u751f\u6210\u5438\u5f15\u4eba\u7684\u56fe\u8868\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u590d\u6742\u7684\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Plotly\u6216Bokeh\u3002<\/p>\n<p><strong>\u997c\u56fe\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u997c\u56fe\u65f6\uff0c\u5e94\u786e\u4fdd\u6bcf\u4e2a\u6247\u533a\u7684\u5927\u5c0f\u80fd\u591f\u51c6\u786e\u53cd\u6620\u5176\u6570\u636e\u6bd4\u4f8b\u3002\u907f\u514d\u4f7f\u7528\u8fc7\u591a\u7684\u6247\u533a\uff0c\u901a\u5e38\u4e0d\u8d85\u8fc75\u52306\u4e2a\u7c7b\u522b\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u6301\u56fe\u8868\u7684\u6e05\u6670\u5ea6\u3002\u6b64\u5916\uff0c\u8003\u8651\u4f7f\u7528\u6807\u7b7e\u6216\u767e\u5206\u6bd4\u663e\u793a\uff0c\u5e2e\u52a9\u89c2\u4f17\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u3002\u989c\u8272\u9009\u62e9\u4e5f\u5f88\u91cd\u8981\uff0c\u786e\u4fdd\u4f7f\u7528\u5bf9\u6bd4\u5ea6\u8f83\u9ad8\u7684\u989c\u8272\uff0c\u4ee5\u4fbf\u533a\u5206\u4e0d\u540c\u7684\u6247\u533a\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u997c\u56fe\u7684\u6837\u5f0f\u548c\u6807\u7b7e\uff1f<\/strong><br \/>\u4f7f\u7528Matplotlib\u7ed8\u5236\u997c\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u53c2\u6570\u6765\u4fee\u6539\u6247\u533a\u7684\u989c\u8272\u3001\u8fb9\u6846\u3001\u6807\u7b7e\u548c\u5b57\u4f53\u7b49\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>colors<\/code>\u53c2\u6570\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6bcf\u4e2a\u6247\u533a\u7684\u989c\u8272\uff0c\u4f7f\u7528<code>autopct<\/code>\u53c2\u6570\u53ef\u4ee5\u5728\u6247\u533a\u4e0a\u663e\u793a\u767e\u5206\u6bd4\u6216\u5176\u4ed6\u4fe1\u606f\u3002\u901a\u8fc7\u8fd9\u4e9b\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u53ef\u4ee5\u4f7f\u997c\u56fe\u66f4\u7b26\u5408\u4f60\u7684\u9700\u6c42\u548c\u7f8e\u5b66\u6807\u51c6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u7ed8\u5236\u997c\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6216Plotly\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":1181147,"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\/1181138"}],"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=1181138"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181138\/revisions"}],"predecessor-version":[{"id":1181149,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181138\/revisions\/1181149"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1181147"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1181138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1181138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1181138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}