{"id":1076338,"date":"2025-01-08T11:52:26","date_gmt":"2025-01-08T03:52:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076338.html"},"modified":"2025-01-08T11:52:28","modified_gmt":"2025-01-08T03:52:28","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e6%9f%b1%e7%8a%b6%e5%9b%be%e6%98%be%e7%a4%ba%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076338.html","title":{"rendered":"Python\u5982\u4f55\u8ba9\u67f1\u72b6\u56fe\u663e\u793a\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180943\/817fc0d2-2577-474f-8fd5-049eae35a7e9.webp\" alt=\"Python\u5982\u4f55\u8ba9\u67f1\u72b6\u56fe\u663e\u793a\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u8ba9\u67f1\u72b6\u56fe\u663e\u793a\u6570\u636e\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528Matplotlib\u5e93\u3001\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3001\u8c03\u6574\u56fe\u5f62\u5c5e\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u663e\u793a\u6570\u636e\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528Matplotlib\u5e93\u3002<strong>\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u3001\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3001\u8c03\u6574\u56fe\u5f62\u5c5e\u6027<\/strong>\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u67f1\u72b6\u56fe\u663e\u793a\u6570\u636e\u7684\u6548\u679c\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u201c\u4f7f\u7528Matplotlib\u5e93\u201d\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Matplotlib\u5e93<\/strong>\uff1aMatplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u7ed8\u5236\u56fe\u5f62\u7684\u65b9\u6cd5\u548c\u5de5\u5177\u3002\u901a\u8fc7Matplotlib\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ed8\u5236\u51fa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\uff0c\u5305\u62ec\u67f1\u72b6\u56fe\u3002\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165Matplotlib\u5e93\u3001\u521b\u5efa\u6570\u636e\u3001\u7ed8\u5236\u56fe\u5f62\u3001\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u7b49\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u7ed8\u5236\u56fe\u5f62\u7684\u65b9\u6cd5\u548c\u5de5\u5177\u3002\u901a\u8fc7Matplotlib\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ed8\u5236\u51fa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\uff0c\u5305\u62ec\u67f1\u72b6\u56fe\u3002\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165Matplotlib\u5e93\u3001\u521b\u5efa\u6570\u636e\u3001\u7ed8\u5236\u56fe\u5f62\u3001\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165Matplotlib\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5728Python\u73af\u5883\u4e2d\u5bfc\u5165Matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\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>Matplotlib\u7684pyplot\u5b50\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5c\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efa\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u67f1\u72b6\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u51c6\u5907\u597d\u8981\u5c55\u793a\u7684\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u7ec4\u6570\u636e\u8868\u793a\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>values = [10, 20, 15, 25]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u7ed8\u5236\u67f1\u72b6\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684<code>bar<\/code>\u51fd\u6570\u53ef\u4ee5\u7ed8\u5236\u67f1\u72b6\u56fe\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.bar(categories, values)<\/p>\n<p>plt.xlabel(&#39;Categories&#39;)<\/p>\n<p>plt.ylabel(&#39;Values&#39;)<\/p>\n<p>plt.title(&#39;Bar Chart Example&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u7ed8\u5236\u4e00\u4e2a\u67f1\u72b6\u56fe\uff0c\u5e76\u8bbe\u7f6eX\u8f74\u6807\u7b7e\u3001Y\u8f74\u6807\u7b7e\u548c\u56fe\u8868\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h4>4. \u6dfb\u52a0\u6570\u636e\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u67f1\u72b6\u56fe\u4e0a\u663e\u793a\u6570\u636e\uff0c\u53ef\u4ee5\u5728\u6bcf\u4e2a\u67f1\u5b50\u7684\u9876\u90e8\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3002\u4ee5\u4e0b\u662f\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">bars = plt.bar(categories, values)<\/p>\n<p>for bar in bars:<\/p>\n<p>    yval = bar.get_height()<\/p>\n<p>    plt.text(bar.get_x() + bar.get_width()\/2, yval, int(yval), va=&#39;bottom&#39;)<\/p>\n<p>plt.xlabel(&#39;Categories&#39;)<\/p>\n<p>plt.ylabel(&#39;Values&#39;)<\/p>\n<p>plt.title(&#39;Bar Chart with Data Labels&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>plt.text<\/code>\u51fd\u6570\u5728\u6bcf\u4e2a\u67f1\u5b50\u7684\u9876\u90e8\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3002<code>bar.get_x() + bar.get_width()\/2<\/code>\u7528\u4e8e\u8ba1\u7b97\u67f1\u5b50\u7684\u4e2d\u5fc3\u4f4d\u7f6e\uff0c<code>yval<\/code>\u8868\u793a\u67f1\u5b50\u7684\u9ad8\u5ea6\uff0c<code>va=&#39;bottom&#39;<\/code>\u7528\u4e8e\u5c06\u6587\u672c\u5bf9\u9f50\u5230\u67f1\u5b50\u9876\u90e8\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6dfb\u52a0\u6570\u636e\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u662f\u4e3a\u4e86\u4f7f\u56fe\u5f62\u66f4\u5177\u53ef\u8bfb\u6027\u3002\u5728\u67f1\u72b6\u56fe\u4e2d\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u53ef\u4ee5\u5e2e\u52a9\u89c2\u4f17\u5feb\u901f\u4e86\u89e3\u6bcf\u4e2a\u67f1\u5b50\u7684\u6570\u503c\u3002\u53ef\u4ee5\u5728\u7ed8\u5236\u67f1\u72b6\u56fe\u540e\uff0c\u4f7f\u7528<code>plt.text<\/code>\u51fd\u6570\u5728\u6bcf\u4e2a\u67f1\u5b50\u7684\u9876\u90e8\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u67f1\u5b50\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u83b7\u53d6\u67f1\u5b50\u5bf9\u8c61\u3002\u53ef\u4ee5\u901a\u8fc7<code>plt.bar<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u5bf9\u8c61\u6765\u83b7\u53d6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">bars = plt.bar(categories, values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>bars<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u6240\u6709\u67f1\u5b50\u5bf9\u8c61\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2. \u6dfb\u52a0\u6587\u672c\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u904d\u5386\u6bcf\u4e2a\u67f1\u5b50\u5bf9\u8c61\uff0c\u5e76\u5728\u67f1\u5b50\u9876\u90e8\u6dfb\u52a0\u6587\u672c\u6807\u7b7e\u3002\u4f7f\u7528<code>plt.text<\/code>\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for bar in bars:<\/p>\n<p>    yval = bar.get_height()<\/p>\n<p>    plt.text(bar.get_x() + bar.get_width()\/2, yval, int(yval), va=&#39;bottom&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>bar.get_x() + bar.get_width()\/2<\/code>\u7528\u4e8e\u8ba1\u7b97\u67f1\u5b50\u7684\u4e2d\u5fc3\u4f4d\u7f6e\uff0c<code>yval<\/code>\u8868\u793a\u67f1\u5b50\u7684\u9ad8\u5ea6\uff0c<code>va=&#39;bottom&#39;<\/code>\u7528\u4e8e\u5c06\u6587\u672c\u5bf9\u9f50\u5230\u67f1\u5b50\u9876\u90e8\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8c03\u6574\u56fe\u5f62\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u56fe\u5f62\u66f4\u7f8e\u89c2\uff0c\u53ef\u4ee5\u8c03\u6574\u56fe\u5f62\u7684\u5c5e\u6027\uff0c\u5305\u62ec\u989c\u8272\u3001\u5b57\u4f53\u3001\u5e03\u5c40\u7b49\u3002Matplotlib\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u6574\u56fe\u5f62\u5c5e\u6027\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u8c03\u6574\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>color<\/code>\u53c2\u6570\u6765\u8c03\u6574\u67f1\u5b50\u7684\u989c\u8272\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e\u67f1\u5b50\u7684\u989c\u8272\u4e3a\u84dd\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.bar(categories, values, color=&#39;blue&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd8\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u67f1\u5b50\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">colors = [&#39;red&#39;, &#39;blue&#39;, &#39;green&#39;, &#39;purple&#39;]<\/p>\n<p>plt.bar(categories, values, color=colors)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8c03\u6574\u5b57\u4f53<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>fontdict<\/code>\u53c2\u6570\u6765\u8c03\u6574\u6587\u672c\u7684\u5b57\u4f53\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e\u6807\u9898\u7684\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.title(&#39;Bar Chart Example&#39;, fontdict={&#39;fontsize&#39;: 14, &#39;color&#39;: &#39;red&#39;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u53ef\u4ee5\u8bbe\u7f6eX\u8f74\u548cY\u8f74\u6807\u7b7e\u7684\u5b57\u4f53\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.xlabel(&#39;Categories&#39;, fontdict={&#39;fontsize&#39;: 12, &#39;color&#39;: &#39;blue&#39;})<\/p>\n<p>plt.ylabel(&#39;Values&#39;, fontdict={&#39;fontsize&#39;: 12, &#39;color&#39;: &#39;blue&#39;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8c03\u6574\u5e03\u5c40<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>plt.tight_layout<\/code>\u51fd\u6570\u81ea\u52a8\u8c03\u6574\u56fe\u5f62\u7684\u5e03\u5c40\uff0c\u4ee5\u907f\u514d\u6807\u7b7e\u548c\u56fe\u5f62\u91cd\u53e0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.tight_layout()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Seaborn\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\u5e93\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Seaborn\u5e93\u6765\u7ed8\u5236\u66f4\u7f8e\u89c2\u7684\u67f1\u72b6\u56fe\u3002Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684API\u548c\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165Seaborn\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5728Python\u73af\u5883\u4e2d\u5bfc\u5165Seaborn\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u51c6\u5907\u597d\u8981\u5c55\u793a\u7684\u6570\u636e\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u7ec4\u6570\u636e\u8868\u793a\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>values = [10, 20, 15, 25]<\/p>\n<p>data = {&#39;Categories&#39;: categories, &#39;Values&#39;: values}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u7ed8\u5236\u67f1\u72b6\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Seaborn\u7684<code>barplot<\/code>\u51fd\u6570\u53ef\u4ee5\u7ed8\u5236\u67f1\u72b6\u56fe\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sns.barplot(x=&#39;Categories&#39;, y=&#39;Values&#39;, data=data)<\/p>\n<p>plt.xlabel(&#39;Categories&#39;)<\/p>\n<p>plt.ylabel(&#39;Values&#39;)<\/p>\n<p>plt.title(&#39;Bar Chart Example&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u7ed8\u5236\u4e00\u4e2a\u67f1\u72b6\u56fe\uff0c\u5e76\u8bbe\u7f6eX\u8f74\u6807\u7b7e\u3001Y\u8f74\u6807\u7b7e\u548c\u56fe\u8868\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h4>4. \u6dfb\u52a0\u6570\u636e\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u67f1\u72b6\u56fe\u4e0a\u663e\u793a\u6570\u636e\uff0c\u53ef\u4ee5\u5728\u6bcf\u4e2a\u67f1\u5b50\u7684\u9876\u90e8\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3002\u4ee5\u4e0b\u662f\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax = sns.barplot(x=&#39;Categories&#39;, y=&#39;Values&#39;, data=data)<\/p>\n<p>for p in ax.patches:<\/p>\n<p>    ax.annotate(format(p.get_height(), &#39;.1f&#39;),<\/p>\n<p>                (p.get_x() + p.get_width() \/ 2., p.get_height()),<\/p>\n<p>                ha=&#39;center&#39;, va=&#39;center&#39;,<\/p>\n<p>                xytext=(0, 9),<\/p>\n<p>                textcoords=&#39;offset points&#39;)<\/p>\n<p>plt.xlabel(&#39;Categories&#39;)<\/p>\n<p>plt.ylabel(&#39;Values&#39;)<\/p>\n<p>plt.title(&#39;Bar Chart with Data Labels&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>ax.annotate<\/code>\u51fd\u6570\u5728\u6bcf\u4e2a\u67f1\u5b50\u7684\u9876\u90e8\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3002<code>p.get_x() + p.get_width() \/ 2.<\/code>\u7528\u4e8e\u8ba1\u7b97\u67f1\u5b50\u7684\u4e2d\u5fc3\u4f4d\u7f6e\uff0c<code>p.get_height()<\/code>\u8868\u793a\u67f1\u5b50\u7684\u9ad8\u5ea6\uff0c<code>ha=&#39;center&#39;<\/code>\u548c<code>va=&#39;center&#39;<\/code>\u7528\u4e8e\u5c06\u6587\u672c\u5bf9\u9f50\u5230\u67f1\u5b50\u9876\u90e8\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\u548cSeaborn\u5e93\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u7ed8\u5236\u67f1\u72b6\u56fe\u3002Pandas\u5e93\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684API\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u53ef\u89c6\u5316\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165Pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5728Python\u73af\u5883\u4e2d\u5bfc\u5165Pandas\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u51c6\u5907\u597d\u8981\u5c55\u793a\u7684\u6570\u636e\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u7ec4\u6570\u636e\u8868\u793a\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;Categories&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;], &#39;Values&#39;: [10, 20, 15, 25]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u7ed8\u5236\u67f1\u72b6\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u7684<code>plot<\/code>\u51fd\u6570\u53ef\u4ee5\u7ed8\u5236\u67f1\u72b6\u56fe\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax = df.plot(kind=&#39;bar&#39;, x=&#39;Categories&#39;, y=&#39;Values&#39;, legend=False)<\/p>\n<p>plt.xlabel(&#39;Categories&#39;)<\/p>\n<p>plt.ylabel(&#39;Values&#39;)<\/p>\n<p>plt.title(&#39;Bar Chart Example&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u7ed8\u5236\u4e00\u4e2a\u67f1\u72b6\u56fe\uff0c\u5e76\u8bbe\u7f6eX\u8f74\u6807\u7b7e\u3001Y\u8f74\u6807\u7b7e\u548c\u56fe\u8868\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h4>4. \u6dfb\u52a0\u6570\u636e\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u67f1\u72b6\u56fe\u4e0a\u663e\u793a\u6570\u636e\uff0c\u53ef\u4ee5\u5728\u6bcf\u4e2a\u67f1\u5b50\u7684\u9876\u90e8\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3002\u4ee5\u4e0b\u662f\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax = df.plot(kind=&#39;bar&#39;, x=&#39;Categories&#39;, y=&#39;Values&#39;, legend=False)<\/p>\n<p>for p in ax.patches:<\/p>\n<p>    ax.annotate(format(p.get_height(), &#39;.1f&#39;),<\/p>\n<p>                (p.get_x() + p.get_width() \/ 2., p.get_height()),<\/p>\n<p>                ha=&#39;center&#39;, va=&#39;center&#39;,<\/p>\n<p>                xytext=(0, 9),<\/p>\n<p>                textcoords=&#39;offset points&#39;)<\/p>\n<p>plt.xlabel(&#39;Categories&#39;)<\/p>\n<p>plt.ylabel(&#39;Values&#39;)<\/p>\n<p>plt.title(&#39;Bar Chart with Data Labels&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>ax.annotate<\/code>\u51fd\u6570\u5728\u6bcf\u4e2a\u67f1\u5b50\u7684\u9876\u90e8\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3002<code>p.get_x() + p.get_width() \/ 2.<\/code>\u7528\u4e8e\u8ba1\u7b97\u67f1\u5b50\u7684\u4e2d\u5fc3\u4f4d\u7f6e\uff0c<code>p.get_height()<\/code>\u8868\u793a\u67f1\u5b50\u7684\u9ad8\u5ea6\uff0c<code>ha=&#39;center&#39;<\/code>\u548c<code>va=&#39;center&#39;<\/code>\u7528\u4e8e\u5c06\u6587\u672c\u5bf9\u9f50\u5230\u67f1\u5b50\u9876\u90e8\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u663e\u793a\u6570\u636e\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u3001Seaborn\u6216Pandas\u5e93\u3002\u901a\u8fc7<strong>\u4f7f\u7528Matplotlib\u5e93\u3001\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3001\u8c03\u6574\u56fe\u5f62\u5c5e\u6027<\/strong>\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u67f1\u72b6\u56fe\u663e\u793a\u6570\u636e\u7684\u6548\u679c\u3002\u4e0d\u540c\u7684\u5e93\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u7ed8\u56fe\u65b9\u6cd5\u548c\u5c5e\u6027\u8c03\u6574\u9009\u9879\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3001\u8c03\u6574\u989c\u8272\u3001\u5b57\u4f53\u548c\u5e03\u5c40\u7b49\u65b9\u5f0f\uff0c\u4f7f\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u67f1\u72b6\u56fe\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e3a\u67f1\u72b6\u56fe\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u7ed8\u5236\u67f1\u72b6\u56fe\u540e\uff0c\u4f7f\u7528<code>text()<\/code>\u51fd\u6570\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u65b9\u663e\u793a\u6570\u636e\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nx = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;]\ny = [10, 15, 7]\nbars = plt.bar(x, y)\n\nfor bar in bars:\n    yval = bar.get_height()\n    plt.text(bar.get_x() + bar.get_width()\/2, yval, yval, ha=&#39;center&#39;, va=&#39;bottom&#39;)\n\nplt.show()\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u9002\u5408\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u663e\u793a\u6570\u636e\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0cSeaborn\u548cPlotly\u4e5f\u662f\u7ed8\u5236\u67f1\u72b6\u56fe\u7684\u4f18\u79c0\u9009\u62e9\u3002Seaborn\u5728\u7f8e\u89c2\u6027\u4e0a\u6709\u4f18\u52bf\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5e26\u6709\u6570\u636e\u6807\u7b7e\u7684\u67f1\u72b6\u56fe\u3002Plotly\u63d0\u4f9b\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u5de5\u5177\u63d0\u793a\u663e\u793a\u6570\u636e\uff0c\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u67f1\u72b6\u56fe\u7684\u663e\u793a\u6837\u5f0f\u548c\u6570\u636e\u6807\u7b7e\u683c\u5f0f\uff1f<\/strong><br \/>\u81ea\u5b9a\u4e49\u67f1\u72b6\u56fe\u7684\u6837\u5f0f\u53ef\u4ee5\u901a\u8fc7Matplotlib\u4e2d\u7684\u53c2\u6570\u5b9e\u73b0\u3002\u53ef\u4ee5\u8bbe\u7f6e\u989c\u8272\u3001\u8fb9\u6846\u3001\u5b57\u4f53\u5927\u5c0f\u7b49\u5c5e\u6027\u3002\u5728\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u65f6\uff0c\u53ef\u4ee5\u683c\u5f0f\u5316\u6570\u5b57\uff0c\u6bd4\u5982\u4f7f\u7528<code>f-string<\/code>\u6216<code>format()<\/code>\u51fd\u6570\u6765\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u6216\u6dfb\u52a0\u5355\u4f4d\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">plt.text(bar.get_x() + bar.get_width()\/2, yval, f&#39;{yval} units&#39;, ha=&#39;center&#39;, va=&#39;bottom&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u67f1\u72b6\u56fe\u66f4\u52a0\u76f4\u89c2\u548c\u7f8e\u89c2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba9\u67f1\u72b6\u56fe\u663e\u793a\u6570\u636e\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528Matplotlib\u5e93\u3001\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u3001\u8c03\u6574\u56fe\u5f62\u5c5e\u6027 \u5728Python\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1076345,"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\/1076338"}],"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=1076338"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076338\/revisions"}],"predecessor-version":[{"id":1076351,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076338\/revisions\/1076351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076345"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}