{"id":1119914,"date":"2025-01-08T18:49:11","date_gmt":"2025-01-08T10:49:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1119914.html"},"modified":"2025-01-08T18:49:18","modified_gmt":"2025-01-08T10:49:18","slug":"python%e5%a0%86%e5%8f%a0%e6%9f%b1%e7%8a%b6%e5%9b%be%e5%a6%82%e4%bd%95%e4%b8%8d%e9%87%8d%e5%8f%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1119914.html","title":{"rendered":"python\u5806\u53e0\u67f1\u72b6\u56fe\u5982\u4f55\u4e0d\u91cd\u53e0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082802\/ec08c9fc-fb61-45a8-b1e7-97bab99f5f60.webp\" alt=\"python\u5806\u53e0\u67f1\u72b6\u56fe\u5982\u4f55\u4e0d\u91cd\u53e0\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u521b\u5efa\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3001\u8c03\u6574\u67f1\u72b6\u56fe\u7684\u4f4d\u7f6e\u3001\u8bbe\u7f6e\u67f1\u72b6\u56fe\u7684\u5bbd\u5ea6<\/strong>\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>plt.bar<\/code>\u51fd\u6570\u5e76\u8c03\u6574\u6bcf\u4e2a\u67f1\u72b6\u56fe\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u4f7f\u5b83\u4eec\u5e76\u6392\u663e\u793a\uff0c\u800c\u4e0d\u662f\u5806\u53e0\u5728\u4e00\u8d77\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5e76\u5bfc\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\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>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165Matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u6f14\u793a\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e9b\u793a\u4f8b\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e09\u4e2a\u7c7b\u522b\u7684\u6570\u636e\uff0c\u6bcf\u4e2a\u7c7b\u522b\u6709\u56db\u7ec4\u6570\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">categories = [&#39;Category 1&#39;, &#39;Category 2&#39;, &#39;Category 3&#39;, &#39;Category 4&#39;]<\/p>\n<p>values1 = [10, 20, 30, 40]<\/p>\n<p>values2 = [15, 25, 35, 45]<\/p>\n<p>values3 = [20, 30, 40, 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u7f6e\u67f1\u72b6\u56fe\u7684\u4f4d\u7f6e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u67f1\u72b6\u56fe\u4e0d\u91cd\u53e0\uff0c\u9700\u8981\u4e3a\u6bcf\u4e2a\u7c7b\u522b\u7684\u67f1\u72b6\u56fe\u8bbe\u7f6e\u4e0d\u540c\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6bcf\u4e2a\u7c7b\u522b\u7684\u4f4d\u7f6e\u7684\u6570\u7ec4\uff0c\u5e76\u5728\u8fd9\u4e9b\u4f4d\u7f6e\u4e0a\u7ed8\u5236\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">bar_width = 0.25  # \u8bbe\u7f6e\u6bcf\u4e2a\u67f1\u72b6\u56fe\u7684\u5bbd\u5ea6<\/p>\n<p>r1 = np.arange(len(values1))<\/p>\n<p>r2 = [x + bar_width for x in r1]<\/p>\n<p>r3 = [x + bar_width for x in r2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed8\u5236\u67f1\u72b6\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>plt.bar<\/code>\u51fd\u6570\u7ed8\u5236\u67f1\u72b6\u56fe\uff0c\u5e76\u8bbe\u7f6e\u5b83\u4eec\u7684\u989c\u8272\u548c\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.bar(r1, values1, color=&#39;b&#39;, width=bar_width, edgecolor=&#39;grey&#39;, label=&#39;Series 1&#39;)<\/p>\n<p>plt.bar(r2, values2, color=&#39;r&#39;, width=bar_width, edgecolor=&#39;grey&#39;, label=&#39;Series 2&#39;)<\/p>\n<p>plt.bar(r3, values3, color=&#39;g&#39;, width=bar_width, edgecolor=&#39;grey&#39;, label=&#39;Series 3&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u56fe\u8868\u66f4\u52a0\u6e05\u6670\uff0c\u53ef\u4ee5\u6dfb\u52a0\u7c7b\u522b\u6807\u7b7e\u3001\u56fe\u4f8b\u548c\u6807\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.xlabel(&#39;Category&#39;, fontweight=&#39;bold&#39;)<\/p>\n<p>plt.ylabel(&#39;Values&#39;, fontweight=&#39;bold&#39;)<\/p>\n<p>plt.xticks([r + bar_width for r in range(len(values1))], categories)<\/p>\n<p>plt.title(&#39;Non-overlapping Stacked Bar Chart&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u663e\u793a\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528<code>plt.show()<\/code>\u51fd\u6570\u663e\u793a\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>categories = [&#39;Category 1&#39;, &#39;Category 2&#39;, &#39;Category 3&#39;, &#39;Category 4&#39;]<\/p>\n<p>values1 = [10, 20, 30, 40]<\/p>\n<p>values2 = [15, 25, 35, 45]<\/p>\n<p>values3 = [20, 30, 40, 50]<\/p>\n<h2><strong>\u8bbe\u7f6e\u67f1\u72b6\u56fe\u7684\u4f4d\u7f6e<\/strong><\/h2>\n<p>bar_width = 0.25<\/p>\n<p>r1 = np.arange(len(values1))<\/p>\n<p>r2 = [x + bar_width for x in r1]<\/p>\n<p>r3 = [x + bar_width for x in r2]<\/p>\n<h2><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>plt.bar(r1, values1, color=&#39;b&#39;, width=bar_width, edgecolor=&#39;grey&#39;, label=&#39;Series 1&#39;)<\/p>\n<p>plt.bar(r2, values2, color=&#39;r&#39;, width=bar_width, edgecolor=&#39;grey&#39;, label=&#39;Series 2&#39;)<\/p>\n<p>plt.bar(r3, values3, color=&#39;g&#39;, width=bar_width, edgecolor=&#39;grey&#39;, label=&#39;Series 3&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898<\/strong><\/h2>\n<p>plt.xlabel(&#39;Category&#39;, fontweight=&#39;bold&#39;)<\/p>\n<p>plt.ylabel(&#39;Values&#39;, fontweight=&#39;bold&#39;)<\/p>\n<p>plt.xticks([r + bar_width for r in range(len(values1))], categories)<\/p>\n<p>plt.title(&#39;Non-overlapping Stacked Bar Chart&#39;)<\/p>\n<p>plt.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u6210\u529f\u7ed8\u5236\u4e00\u4e2a\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\u3002\u8fd9\u4e2a\u65b9\u6cd5\u540c\u6837\u9002\u7528\u4e8e\u66f4\u591a\u7c7b\u522b\u7684\u6570\u636e\uff0c\u53ea\u9700\u8981\u76f8\u5e94\u5730\u8c03\u6574\u6bcf\u4e2a\u67f1\u72b6\u56fe\u7684\u4f4d\u7f6e\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Seaborn\u5e93\u7ed8\u5236\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528Seaborn\u5e93\u6765\u7ed8\u5236\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\u3002Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684API\u548c\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5\u5e76\u5bfc\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Seaborn\u5e93\u3002\u5982\u679c\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 seaborn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165Seaborn\u548cMatplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u521b\u5efa\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u6f14\u793a\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e9b\u793a\u4f8b\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e09\u4e2a\u7c7b\u522b\u7684\u6570\u636e\uff0c\u6bcf\u4e2a\u7c7b\u522b\u6709\u56db\u7ec4\u6570\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;Category&#39;: [&#39;Category 1&#39;, &#39;Category 2&#39;, &#39;Category 3&#39;, &#39;Category 4&#39;] * 3,<\/p>\n<p>    &#39;Values&#39;: [10, 20, 30, 40, 15, 25, 35, 45, 20, 30, 40, 50],<\/p>\n<p>    &#39;Series&#39;: [&#39;Series 1&#39;] * 4 + [&#39;Series 2&#39;] * 4 + [&#39;Series 3&#39;] * 4<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed8\u5236\u67f1\u72b6\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Seaborn\u7684<code>catplot<\/code>\u51fd\u6570\u7ed8\u5236\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sns.set(style=&quot;whitegrid&quot;)<\/p>\n<p>g = sns.catplot(<\/p>\n<p>    data=df, kind=&quot;bar&quot;,<\/p>\n<p>    x=&quot;Category&quot;, y=&quot;Values&quot;, hue=&quot;Series&quot;,<\/p>\n<p>    ci=&quot;sd&quot;, palette=&quot;dark&quot;, alpha=.6, height=6<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u56fe\u8868\u66f4\u52a0\u6e05\u6670\uff0c\u53ef\u4ee5\u6dfb\u52a0\u7c7b\u522b\u6807\u7b7e\u3001\u56fe\u4f8b\u548c\u6807\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">g.set_axis_labels(&quot;Category&quot;, &quot;Values&quot;)<\/p>\n<p>g.set_titles(&quot;Non-overlapping Stacked Bar Chart&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u663e\u793a\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528<code>plt.show()<\/code>\u51fd\u6570\u663e\u793a\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Category&#39;: [&#39;Category 1&#39;, &#39;Category 2&#39;, &#39;Category 3&#39;, &#39;Category 4&#39;] * 3,<\/p>\n<p>    &#39;Values&#39;: [10, 20, 30, 40, 15, 25, 35, 45, 20, 30, 40, 50],<\/p>\n<p>    &#39;Series&#39;: [&#39;Series 1&#39;] * 4 + [&#39;Series 2&#39;] * 4 + [&#39;Series 3&#39;] * 4<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>sns.set(style=&quot;whitegrid&quot;)<\/p>\n<p>g = sns.catplot(<\/p>\n<p>    data=df, kind=&quot;bar&quot;,<\/p>\n<p>    x=&quot;Category&quot;, y=&quot;Values&quot;, hue=&quot;Series&quot;,<\/p>\n<p>    ci=&quot;sd&quot;, palette=&quot;dark&quot;, alpha=.6, height=6<\/p>\n<p>)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898<\/strong><\/h2>\n<p>g.set_axis_labels(&quot;Category&quot;, &quot;Values&quot;)<\/p>\n<p>g.set_titles(&quot;Non-overlapping Stacked Bar Chart&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Seaborn\u5e93\u7ed8\u5236\u4e00\u4e2a\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\u3002Seaborn\u5e93\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684API\u548c\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\uff0c\u4f7f\u5f97\u7ed8\u5236\u548c\u7f8e\u5316\u56fe\u8868\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6570\u636e\u5904\u7406\u548c\u53ef\u89c6\u5316\u7684\u5176\u4ed6\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u53ef\u89c6\u5316\u8fc7\u7a0b\u4e2d\uff0c\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u5f52\u4e00\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u636e\u53ef\u80fd\u5177\u6709\u4e0d\u540c\u7684\u91cf\u7ea7\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u5c55\u793a\u8fd9\u4e9b\u6570\u636e\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u5f52\u4e00\u5316\u5904\u7406\u3002\u5f52\u4e00\u5316\u662f\u4e00\u79cd\u5c06\u6570\u636e\u8f6c\u6362\u5230\u7279\u5b9a\u8303\u56f4\u5185\u7684\u65b9\u6cd5\uff0c\u901a\u5e38\u662f\u5c06\u6570\u636e\u7f29\u653e\u5230[0, 1]\u8303\u56f4\u5185\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import MinMaxScaler<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Category&#39;: [&#39;Category 1&#39;, &#39;Category 2&#39;, &#39;Category 3&#39;, &#39;Category 4&#39;] * 3,<\/p>\n<p>    &#39;Values&#39;: [10, 20, 30, 40, 15, 25, 35, 45, 20, 30, 40, 50],<\/p>\n<p>    &#39;Series&#39;: [&#39;Series 1&#39;] * 4 + [&#39;Series 2&#39;] * 4 + [&#39;Series 3&#39;] * 4<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5f52\u4e00\u5316\u5904\u7406<\/strong><\/h2>\n<p>scaler = MinMaxScaler()<\/p>\n<p>df[&#39;Values&#39;] = scaler.fit_transform(df[[&#39;Values&#39;]])<\/p>\n<h2><strong>\u7ed8\u5236\u5f52\u4e00\u5316\u540e\u7684\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>sns.set(style=&quot;whitegrid&quot;)<\/p>\n<p>g = sns.catplot(<\/p>\n<p>    data=df, kind=&quot;bar&quot;,<\/p>\n<p>    x=&quot;Category&quot;, y=&quot;Values&quot;, hue=&quot;Series&quot;,<\/p>\n<p>    ci=&quot;sd&quot;, palette=&quot;dark&quot;, alpha=.6, height=6<\/p>\n<p>)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898<\/strong><\/h2>\n<p>g.set_axis_labels(&quot;Category&quot;, &quot;Values&quot;)<\/p>\n<p>g.set_titles(&quot;Normalized Non-overlapping Stacked Bar Chart&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6570\u636e\u805a\u5408<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u5bf9\u6570\u636e\u8fdb\u884c\u805a\u5408\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u53ef\u4ee5\u6309\u6708\u3001\u5b63\u5ea6\u6216\u5e74\u5ea6\u5bf9\u6570\u636e\u8fdb\u884c\u805a\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/p>\n<p>dates = pd.date_range(&#39;2020-01-01&#39;, periods=12, freq=&#39;M&#39;)<\/p>\n<p>data = {<\/p>\n<p>    &#39;Date&#39;: dates,<\/p>\n<p>    &#39;Values1&#39;: np.random.randint(10, 100, len(dates)),<\/p>\n<p>    &#39;Values2&#39;: np.random.randint(20, 110, len(dates)),<\/p>\n<p>    &#39;Values3&#39;: np.random.randint(30, 120, len(dates))<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6309\u5b63\u5ea6\u805a\u5408\u6570\u636e<\/strong><\/h2>\n<p>df[&#39;Quarter&#39;] = df[&#39;Date&#39;].dt.to_period(&#39;Q&#39;)<\/p>\n<p>df_agg = df.groupby(&#39;Quarter&#39;).sum().reset_index()<\/p>\n<h2><strong>\u7ed8\u5236\u805a\u5408\u540e\u7684\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>df_agg_melted = df_agg.melt(id_vars=&#39;Quarter&#39;, value_vars=[&#39;Values1&#39;, &#39;Values2&#39;, &#39;Values3&#39;], var_name=&#39;Series&#39;, value_name=&#39;Values&#39;)<\/p>\n<p>sns.set(style=&quot;whitegrid&quot;)<\/p>\n<p>g = sns.catplot(<\/p>\n<p>    data=df_agg_melted, kind=&quot;bar&quot;,<\/p>\n<p>    x=&quot;Quarter&quot;, y=&quot;Values&quot;, hue=&quot;Series&quot;,<\/p>\n<p>    ci=&quot;sd&quot;, palette=&quot;dark&quot;, alpha=.6, height=6<\/p>\n<p>)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898<\/strong><\/h2>\n<p>g.set_axis_labels(&quot;Quarter&quot;, &quot;Values&quot;)<\/p>\n<p>g.set_titles(&quot;Aggregated Non-overlapping Stacked Bar Chart&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6570\u636e\u5f52\u4e00\u5316\u548c\u6570\u636e\u805a\u5408\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\uff0c\u5e76\u4ece\u4e2d\u63d0\u53d6\u51fa\u66f4\u591a\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002\u8fd9\u4e9b\u6280\u5de7\u5728\u5b9e\u9645\u7684\u6570\u636e\u5904\u7406\u548c\u53ef\u89c6\u5316\u8fc7\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\u662f\u6570\u636e\u53ef\u89c6\u5316\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u4efb\u52a1\u3002\u901a\u8fc7\u4f7f\u7528Matplotlib\u548cSeaborn\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u7ed8\u5236\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\uff0c\u5e76\u8fdb\u884c\u7f8e\u5316\u548c\u4f18\u5316\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u6570\u636e\u5f52\u4e00\u5316\u548c\u6570\u636e\u805a\u5408\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\uff0c\u5e76\u4ece\u4e2d\u63d0\u53d6\u51fa\u66f4\u591a\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u5728\u6570\u636e\u5206\u6790\u3001\u62a5\u544a\u5236\u4f5c\u8fd8\u662f\u79d1\u7814\u9879\u76ee\u4e2d\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u90fd\u5c06\u6781\u5927\u5730\u63d0\u5347\u6211\u4eec\u7684\u6570\u636e\u53ef\u89c6\u5316\u80fd\u529b\u548c\u6548\u679c\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u5b9e\u73b0\u66f4\u52a0\u9ad8\u6548\u548c\u7f8e\u89c2\u7684\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\uff1f<\/strong><\/p>\n<p>\u8981\u7ed8\u5236\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684bar()\u51fd\u6570\u3002\u786e\u4fdd\u5728\u7ed8\u5236\u6bcf\u4e00\u5c42\u65f6\uff0c\u8bbe\u7f6e\u76f8\u5e94\u7684\u5e95\u90e8\u53c2\u6570\uff0c\u4ee5\u4fbf\u6bcf\u4e2a\u90e8\u5206\u90fd\u80fd\u6b63\u786e\u53e0\u52a0\uff0c\u800c\u4e0d\u662f\u91cd\u53e0\u3002\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\n# \u793a\u4f8b\u6570\u636e\ncategories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;]\nvalues1 = [3, 2, 5]\nvalues2 = [4, 3, 2]\nvalues3 = [2, 4, 3]\n\n# \u8bbe\u7f6e\u67f1\u72b6\u56fe\u7684\u4f4d\u7f6e\nbar_width = 0.4\nx = np.arange(len(categories))\n\n# \u7ed8\u5236\u5806\u53e0\u67f1\u72b6\u56fe\nplt.bar(x, values1, width=bar_width, label=&#39;\u6570\u636e\u96c61&#39;)\nplt.bar(x, values2, bottom=values1, width=bar_width, label=&#39;\u6570\u636e\u96c62&#39;)\nplt.bar(x, values3, bottom=np.array(values1) + np.array(values2), width=bar_width, label=&#39;\u6570\u636e\u96c63&#39;)\n\nplt.xlabel(&#39;\u7c7b\u522b&#39;)\nplt.ylabel(&#39;\u503c&#39;)\nplt.title(&#39;\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\u793a\u4f8b&#39;)\nplt.xticks(x, categories)\nplt.legend()\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728\u7ed8\u5236\u5806\u53e0\u67f1\u72b6\u56fe\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u989c\u8272\u548c\u6837\u5f0f\uff1f<\/strong><\/p>\n<p>\u9009\u62e9\u5408\u9002\u7684\u989c\u8272\u548c\u6837\u5f0f\u5bf9\u4e8e\u5806\u53e0\u67f1\u72b6\u56fe\u7684\u53ef\u8bfb\u6027\u81f3\u5173\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u989c\u8272\u65b9\u6848\u6765\u533a\u5206\u5404\u4e2a\u6570\u636e\u96c6\uff0c\u786e\u4fdd\u989c\u8272\u4e4b\u95f4\u5177\u6709\u8db3\u591f\u7684\u5bf9\u6bd4\u5ea6\u3002\u4f7f\u7528Matplotlib\u7684colormap\u529f\u80fd\u6216\u624b\u52a8\u6307\u5b9a\u989c\u8272\u90fd\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002\u901a\u8fc7\u8bbe\u7f6e\u900f\u660e\u5ea6\uff08alpha\u503c\uff09\u4e5f\u53ef\u4ee5\u589e\u5f3a\u89c6\u89c9\u6548\u679c\uff0c\u8ba9\u56fe\u8868\u770b\u8d77\u6765\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528\u5176\u4ed6\u5e93\u7ed8\u5236\u5806\u53e0\u67f1\u72b6\u56fe\u7684\u63a8\u8350\u65b9\u6848\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u9664\u4e86Matplotlib\u5916\uff0cSeaborn\u548cPlotly\u7b49\u5e93\u4e5f\u63d0\u4f9b\u4e86\u7ed8\u5236\u5806\u53e0\u67f1\u72b6\u56fe\u7684\u529f\u80fd\u3002Seaborn\u63d0\u4f9b\u4e86\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\uff0c\u5e76\u4e14\u66f4\u5bb9\u6613\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u800cPlotly\u5219\u5141\u8bb8\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u9002\u5408\u9700\u8981\u52a8\u6001\u5c55\u793a\u6570\u636e\u7684\u573a\u666f\u3002\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u63d0\u5347\u6570\u636e\u53ef\u89c6\u5316\u7684\u6548\u679c\u4e0e\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u521b\u5efa\u4e0d\u91cd\u53e0\u7684\u5806\u53e0\u67f1\u72b6\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3001\u8c03\u6574\u67f1\u72b6\u56fe\u7684\u4f4d\u7f6e\u3001\u8bbe\u7f6e\u67f1\u72b6\u56fe\u7684\u5bbd\u5ea6\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1119929,"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\/1119914"}],"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=1119914"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119914\/revisions"}],"predecessor-version":[{"id":1119932,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119914\/revisions\/1119932"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1119929"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1119914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1119914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1119914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}