{"id":1167057,"date":"2025-01-15T15:39:24","date_gmt":"2025-01-15T07:39:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167057.html"},"modified":"2025-01-15T15:39:27","modified_gmt":"2025-01-15T07:39:27","slug":"python%e7%94%bb%e9%a5%bc%e5%9b%be%e5%a6%82%e4%bd%95%e9%80%89%e6%8b%a9%e9%a2%9c%e8%89%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1167057.html","title":{"rendered":"python\u753b\u997c\u56fe\u5982\u4f55\u9009\u62e9\u989c\u8272"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25211041\/2dd54e70-3782-40b9-b128-2342a9840ad5.webp\" alt=\"python\u753b\u997c\u56fe\u5982\u4f55\u9009\u62e9\u989c\u8272\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u753b\u997c\u56fe\u65f6\u9009\u62e9\u989c\u8272\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982\u4f7f\u7528Matplotlib\u7684\u9ed8\u8ba4\u8c03\u8272\u677f\u3001\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u3001\u5229\u7528\u8272\u5f69\u5e93\u7b49\u3002\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Matplotlib\u7684\u9ed8\u8ba4\u8c03\u8272\u677f\u3001\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u3001\u5229\u7528\u8272\u5f69\u5e93\uff0c\u5982Seaborn\u6216Colorcet\u3002<\/strong> \u5176\u4e2d\uff0c\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u662f\u4e00\u79cd\u7075\u6d3b\u4e14\u53ef\u63a7\u7684\u65b9\u5f0f\uff0c\u80fd\u786e\u4fdd\u997c\u56fe\u7684\u989c\u8272\u7b26\u5408\u8bbe\u8ba1\u9700\u6c42\u548c\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u7684\u65b9\u6cd5\uff1a\u901a\u8fc7Matplotlib\u7684<code>colors<\/code>\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u4e3a\u6bcf\u4e00\u5757\u997c\u56fe\u6307\u5b9a\u989c\u8272\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u989c\u8272\u5217\u8868\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u7ed9<code>plt.pie<\/code>\u51fd\u6570\u7684<code>colors<\/code>\u53c2\u6570\u3002\u8fd9\u6837\uff0c\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e00\u5757\u997c\u56fe\u7684\u989c\u8272\u7b26\u5408\u60a8\u7684\u9700\u6c42\u548c\u8bbe\u8ba1\u6807\u51c6\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u753b\u997c\u56fe\u65f6\u5982\u4f55\u9009\u62e9\u989c\u8272\u7684\u591a\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Matplotlib\u7684\u9ed8\u8ba4\u8c03\u8272\u677f<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9ed8\u8ba4\u8c03\u8272\u677f\u3002\u4f7f\u7528\u8fd9\u4e9b\u8c03\u8272\u677f\u53ef\u4ee5\u5feb\u901f\u7ed8\u5236\u51fa\u8272\u5f69\u4e30\u5bcc\u7684\u997c\u56fe\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u57fa\u672c\u7528\u6cd5<\/p>\n<\/p>\n<p><p>Matplotlib\u7684<code>pyplot<\/code>\u6a21\u5757\u4e2d\u6709\u4e00\u4e2a<code>pie<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u997c\u56fe\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>pie<\/code>\u51fd\u6570\u4f1a\u81ea\u52a8\u9009\u62e9\u4e00\u7ec4\u989c\u8272\u6765\u586b\u5145\u997c\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\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>plt.pie(sizes, labels=labels)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4fee\u6539\u9ed8\u8ba4\u8c03\u8272\u677f<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u4fee\u6539\u9ed8\u8ba4\u8c03\u8272\u677f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>matplotlib.rcParams<\/code>\u6765\u8bbe\u7f6e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u9ed8\u8ba4\u8c03\u8272\u677f\u8bbe\u7f6e\u4e3a<code>viridis<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib as mpl<\/p>\n<p>mpl.rcParams[&#39;axes.prop_cycle&#39;] = mpl.cycler(color=plt.cm.viridis.colors)<\/p>\n<p>plt.pie(sizes, labels=labels)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u624b\u52a8\u8bbe\u7f6e\u989c\u8272<\/p>\n<\/p>\n<p><p>\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u662f\u4e00\u79cd\u7075\u6d3b\u4e14\u53ef\u63a7\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e00\u5757\u997c\u56fe\u7684\u989c\u8272\u7b26\u5408\u8bbe\u8ba1\u9700\u6c42\u548c\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u901a\u8fc7<code>colors<\/code>\u53c2\u6570\u8bbe\u7f6e\u989c\u8272<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u989c\u8272\u5217\u8868\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u7ed9<code>plt.pie<\/code>\u51fd\u6570\u7684<code>colors<\/code>\u53c2\u6570\u3002<\/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>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528Hex\u989c\u8272\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Hex\u989c\u8272\u4ee3\u7801\u6765\u6307\u5b9a\u989c\u8272\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u63a7\u5236\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">colors = [&#39;#ff9999&#39;,&#39;#66b3ff&#39;,&#39;#99ff99&#39;,&#39;#ffcc99&#39;]<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528\u8272\u5f69\u5e93<\/p>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\u9ed8\u8ba4\u7684\u8c03\u8272\u677f\u548c\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u5916\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u5176\u4ed6\u8272\u5f69\u5e93\uff0c\u5982Seaborn\u548cColorcet\uff0c\u6765\u9009\u62e9\u989c\u8272\u3002<\/p>\n<\/p>\n<p><p>1\u3001Seaborn<\/p>\n<\/p>\n<p><p>Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u7f8e\u89c2\u548c\u4e30\u5bcc\u7684\u8c03\u8272\u677f\u3002\u4f7f\u7528Seaborn\u53ef\u4ee5\u8f7b\u677e\u5730\u751f\u6210\u4e00\u7ec4\u989c\u8272\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u5230\u997c\u56fe\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>colors = sns.color_palette(&quot;pastel&quot;, len(labels))<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001Colorcet<\/p>\n<\/p>\n<p><p>Colorcet\u662f\u4e00\u4e2a\u4e13\u95e8\u4e3a\u79d1\u5b66\u6570\u636e\u53ef\u89c6\u5316\u8bbe\u8ba1\u7684\u8272\u5f69\u5e93\uff0c\u63d0\u4f9b\u4e86\u5927\u91cf\u9ad8\u8d28\u91cf\u7684\u8c03\u8272\u677f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import colorcet as cc<\/p>\n<p>colors = cc.glasbey[:len(labels)]<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u52a8\u6001\u751f\u6210\u989c\u8272<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u6570\u636e\u52a8\u6001\u751f\u6210\u989c\u8272\u3002\u4f8b\u5982\uff0c\u5f53\u6570\u636e\u96c6\u8f83\u5927\u4e14\u989c\u8272\u6570\u91cf\u4e0d\u56fa\u5b9a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b97\u6cd5\u6765\u52a8\u6001\u751f\u6210\u989c\u8272\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528<code>matplotlib.cm<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>Matplotlib\u7684<code>cm<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u989c\u8272\u6620\u5c04\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u52a8\u6001\u751f\u6210\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>cmap = plt.get_cmap(&quot;tab20&quot;)<\/p>\n<p>colors = [cmap(i) for i in np.linspace(0, 1, len(labels))]<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528<code>random<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u751f\u6210\u968f\u673a\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def random_color():<\/p>\n<p>    return &quot;#{:06x}&quot;.format(random.randint(0, 0xFFFFFF))<\/p>\n<p>colors = [random_color() for _ in range(len(labels))]<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u989c\u8272\u6e10\u53d8<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u89c6\u89c9\u6548\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u989c\u8272\u6e10\u53d8\u6765\u7ed8\u5236\u997c\u56fe\u3002\u989c\u8272\u6e10\u53d8\u53ef\u4ee5\u4f7f\u997c\u56fe\u770b\u8d77\u6765\u66f4\u52a0\u4e30\u5bcc\u548c\u7acb\u4f53\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528<code>matplotlib.colors<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>Matplotlib\u7684<code>colors<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u989c\u8272\u6e10\u53d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from matplotlib.colors import LinearSegmentedColormap<\/p>\n<p>colors = [plt.cm.Blues(i\/float(len(labels))) for i in range(len(labels))]<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u81ea\u5b9a\u4e49\u989c\u8272\u6e10\u53d8<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u81ea\u5b9a\u4e49\u989c\u8272\u6e10\u53d8\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_gradient_colors(c1, c2, n):<\/p>\n<p>    return [c1 * (1 - i \/ n) + c2 * (i \/ n) for i in range(n)]<\/p>\n<p>c1 = np.array([1, 0, 0])<\/p>\n<p>c2 = np.array([0, 0, 1])<\/p>\n<p>colors = get_gradient_colors(c1, c2, len(labels))<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u8272\u76f2\u53cb\u597d\u914d\u8272<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u997c\u56fe\u5bf9\u8272\u76f2\u7528\u6237\u66f4\u52a0\u53cb\u597d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u4e13\u95e8\u4e3a\u8272\u76f2\u8bbe\u8ba1\u7684\u914d\u8272\u65b9\u6848\u3002\u8fd9\u4e9b\u914d\u8272\u65b9\u6848\u53ef\u4ee5\u786e\u4fdd\u8272\u76f2\u7528\u6237\u80fd\u591f\u6b63\u786e\u533a\u5206\u4e0d\u540c\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528<code>colorcet<\/code>\u7684\u8272\u76f2\u53cb\u597d\u8c03\u8272\u677f<\/p>\n<\/p>\n<p><p>Colorcet\u63d0\u4f9b\u4e86\u4e00\u4e9b\u8272\u76f2\u53cb\u597d\u7684\u8c03\u8272\u677f\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">colors = cc.blues[:len(labels)]<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528<code>seaborn<\/code>\u7684\u8272\u76f2\u53cb\u597d\u8c03\u8272\u677f<\/p>\n<\/p>\n<p><p>Seaborn\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u8272\u76f2\u53cb\u597d\u7684\u8c03\u8272\u677f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">colors = sns.color_palette(&quot;colorblind&quot;, len(labels))<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u989c\u8272\u5bf9\u6bd4\u5ea6<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u997c\u56fe\u7684\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u9ad8\u5bf9\u6bd4\u5ea6\u7684\u989c\u8272\u3002\u9ad8\u5bf9\u6bd4\u5ea6\u7684\u989c\u8272\u53ef\u4ee5\u4f7f\u4e0d\u540c\u5757\u4e4b\u95f4\u7684\u8fb9\u754c\u66f4\u52a0\u6e05\u6670\uff0c\u6613\u4e8e\u533a\u5206\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528<code>matplotlib.colors<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>Matplotlib\u7684<code>colors<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u5bf9\u6bd4\u5ea6\u7684\u8c03\u8272\u677f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">colors = plt.cm.tab10.colors<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u624b\u52a8\u9009\u62e9\u9ad8\u5bf9\u6bd4\u5ea6\u989c\u8272<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u624b\u52a8\u9009\u62e9\u4e00\u4e9b\u9ad8\u5bf9\u6bd4\u5ea6\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">colors = [&#39;black&#39;, &#39;red&#39;, &#39;blue&#39;, &#39;green&#39;]<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u900f\u660e\u5ea6\u8bbe\u7f6e<\/p>\n<\/p>\n<p><p>\u8bbe\u7f6e\u989c\u8272\u7684\u900f\u660e\u5ea6\u53ef\u4ee5\u4f7f\u997c\u56fe\u770b\u8d77\u6765\u66f4\u52a0\u67d4\u548c\u548c\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528<code>alpha<\/code>\u53c2\u6570<\/p>\n<\/p>\n<p><p>Matplotlib\u7684<code>pie<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>alpha<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u989c\u8272\u7684\u900f\u660e\u5ea6\u3002<\/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>plt.pie(sizes, labels=labels, colors=colors, alpha=0.7)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528<code>matplotlib.colors<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>matplotlib.colors<\/code>\u6a21\u5757\u4e2d\u7684<code>to_rgba<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u989c\u8272\u7684\u900f\u660e\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from matplotlib.colors import to_rgba<\/p>\n<p>colors = [to_rgba(c, alpha=0.7) for c in [&#39;gold&#39;, &#39;yellowgreen&#39;, &#39;lightcoral&#39;, &#39;lightskyblue&#39;]]<\/p>\n<p>plt.pie(sizes, labels=labels, colors=colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u989c\u8272\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u5f53\u6570\u636e\u96c6\u8f83\u5927\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u989c\u8272\u5faa\u73af\u6765\u91cd\u590d\u4f7f\u7528\u4e00\u7ec4\u989c\u8272\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528<code>cycler<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>Matplotlib\u7684<code>cycler<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u989c\u8272\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cycler import cycler<\/p>\n<p>colors = plt.cm.tab20.colors<\/p>\n<p>plt.rc(&#39;axes&#39;, prop_cycle=cycler(&#39;color&#39;, colors))<\/p>\n<p>plt.pie(sizes, labels=labels)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u5faa\u73af\u3002<\/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>cycle_colors = [colors[i % len(colors)] for i in range(len(labels))]<\/p>\n<p>plt.pie(sizes, labels=labels, colors=cycle_colors)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u753b\u997c\u56fe\u65f6\u9009\u62e9\u989c\u8272\u7684\u65b9\u6cd5\u975e\u5e38\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u4f7f\u7528Matplotlib\u7684\u9ed8\u8ba4\u8c03\u8272\u677f\u3001\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u3001\u5229\u7528\u8272\u5f69\u5e93\u3001\u52a8\u6001\u751f\u6210\u989c\u8272\u3001\u989c\u8272\u6e10\u53d8\u3001\u8272\u76f2\u53cb\u597d\u914d\u8272\u3001\u989c\u8272\u5bf9\u6bd4\u5ea6\u3001\u900f\u660e\u5ea6\u8bbe\u7f6e\u8fd8\u662f\u989c\u8272\u5faa\u73af\uff0c\u90fd\u53ef\u4ee5\u4f7f\u997c\u56fe\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u8bfb\u3002\u5e0c\u671b\u672c\u6587\u6240\u4ecb\u7ecd\u7684\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u60a8\u5728\u7ed8\u5236\u997c\u56fe\u65f6\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u997c\u56fe\u9009\u62e9\u81ea\u5b9a\u4e49\u989c\u8272\uff1f<\/strong><br \/>\u5728Python\u4e2d\u7ed8\u5236\u997c\u56fe\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u4e2d\u7684<code>colors<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u6bcf\u4e2a\u6247\u533a\u7684\u989c\u8272\u3002\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u4e00\u4e2a\u989c\u8272\u5217\u8868\u6765\u5b9a\u4e49\u8fd9\u4e9b\u989c\u8272\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>[&#39;red&#39;, &#39;blue&#39;, &#39;green&#39;]<\/code>\u6765\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u90e8\u5206\u9009\u62e9\u7ea2\u8272\u3001\u84dd\u8272\u548c\u7eff\u8272\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\uff0c\u5982<code>[&#39;#FF5733&#39;, &#39;#33FF57&#39;, &#39;#3357FF&#39;]<\/code>\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u6e10\u53d8\u8272\u6765\u586b\u5145\u997c\u56fe\uff1f<\/strong><br \/>\u867d\u7136<code>matplotlib<\/code>\u4e0d\u76f4\u63a5\u652f\u6301\u6e10\u53d8\u8272\u586b\u5145\u997c\u56fe\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u624b\u52a8\u521b\u5efa\u591a\u4e2a\u6247\u533a\uff0c\u6bcf\u4e2a\u6247\u533a\u4f7f\u7528\u4e0d\u540c\u7684\u989c\u8272\uff0c\u4ece\u800c\u5b9e\u73b0\u6e10\u53d8\u6548\u679c\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528<code>matplotlib<\/code>\u7684<code>Patch<\/code>\u5bf9\u8c61\uff0c\u7ed3\u5408\u81ea\u5b9a\u4e49\u7684\u989c\u8272\u6620\u5c04\u3002<\/p>\n<p><strong>\u5728\u7ed8\u5236\u997c\u56fe\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u989c\u8272\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\uff1f<\/strong><br \/>\u9009\u62e9\u989c\u8272\u65f6\uff0c\u5e94\u8003\u8651\u989c\u8272\u7684\u5bf9\u6bd4\u5ea6\u548c\u8272\u76f2\u53cb\u597d\u6027\u3002\u4f7f\u7528\u914d\u8272\u5de5\u5177\uff08\u5982ColorBrewer\u6216Adobe Color\uff09\u53ef\u4ee5\u5e2e\u52a9\u9009\u62e9\u548c\u8c10\u7684\u989c\u8272\u7ec4\u5408\u3002\u6b64\u5916\uff0c\u907f\u514d\u4f7f\u7528\u8fc7\u4e8e\u9c9c\u8273\u6216\u76f8\u4f3c\u7684\u989c\u8272\uff0c\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u90e8\u5206\u90fd\u80fd\u6e05\u6670\u533a\u5206\u3002\u786e\u4fdd\u56fe\u4f8b\u4e2d\u989c\u8272\u4e0e\u997c\u56fe\u4e2d\u7684\u989c\u8272\u4e00\u81f4\uff0c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u753b\u997c\u56fe\u65f6\u9009\u62e9\u989c\u8272\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982\u4f7f\u7528Matplotlib\u7684\u9ed8\u8ba4\u8c03\u8272\u677f\u3001\u624b\u52a8\u8bbe\u7f6e\u989c\u8272\u3001\u5229\u7528\u8272\u5f69\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1167062,"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\/1167057"}],"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=1167057"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167057\/revisions"}],"predecessor-version":[{"id":1167065,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167057\/revisions\/1167065"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167062"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}