{"id":1135591,"date":"2025-01-08T21:27:04","date_gmt":"2025-01-08T13:27:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1135591.html"},"modified":"2025-01-08T21:27:07","modified_gmt":"2025-01-08T13:27:07","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e6%9f%b1%e7%8a%b6%e5%9b%be%e4%b8%8a%e5%8a%a0%e4%b8%8a%e5%b9%b3%e5%9d%87%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135591.html","title":{"rendered":"Python\u5982\u4f55\u5728\u67f1\u72b6\u56fe\u4e0a\u52a0\u4e0a\u5e73\u5747\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104214\/5cae8b3f-4f9d-4eda-8dc1-85dfae5954ec.webp\" alt=\"Python\u5982\u4f55\u5728\u67f1\u72b6\u56fe\u4e0a\u52a0\u4e0a\u5e73\u5747\u7ebf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7matplotlib\u5e93\u5728\u67f1\u72b6\u56fe\u4e0a\u52a0\u4e0a\u5e73\u5747\u7ebf\u3002<\/strong> \u8fd9\u79cd\u65b9\u6cd5\u65e2\u80fd\u5e2e\u52a9\u6211\u4eec\u76f4\u89c2\u5730\u5c55\u793a\u6570\u636e\u5206\u5e03\uff0c\u4e5f\u80fd\u8ba9\u6211\u4eec\u6e05\u6670\u5730\u770b\u5230\u6570\u636e\u7684\u603b\u4f53\u8d8b\u52bf\u3002<strong>\u67f1\u72b6\u56fe\u3001\u5e73\u5747\u7ebf<\/strong>\u90fd\u662f\u6570\u636e\u53ef\u89c6\u5316\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u5176\u4e2d\uff0c<strong>\u5e73\u5747\u7ebf<\/strong>\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8ba1\u7b97\u83b7\u5f97\uff0c\u5e76\u5229\u7528matplotlib\u5e93\u7684\u7ed8\u56fe\u529f\u80fd\u6dfb\u52a0\u5230\u67f1\u72b6\u56fe\u4e0a\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86matplotlib\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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\u6211\u4eec\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8fd9\u4e9b\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\u751f\u6210\u67f1\u72b6\u56fe\u6240\u9700\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u751f\u6210\u67f1\u72b6\u56fe\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u7ec4\u6570\u636e\uff0c\u7528\u4e8e\u8868\u793a\u67d0\u4e2a\u73ed\u7ea7\u4e2d\u5b66\u751f\u7684\u8003\u8bd5\u6210\u7ee9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u793a\u4f8b\u6570\u636e<\/p>\n<p>data = [85, 87, 78, 91, 73, 89, 84, 82, 95, 76]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8ba1\u7b97\u6570\u636e\u7684\u5e73\u5747\u503c<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u8fd9\u7ec4\u6570\u636e\u7684\u5e73\u5747\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u4e2d\u7684\u51fd\u6570\u6765\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u5e73\u5747\u503c<\/p>\n<p>mean_value = np.mean(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed8\u5236\u67f1\u72b6\u56fe<\/h3>\n<\/p>\n<p><p>\u6709\u4e86\u6570\u636e\u548c\u5e73\u5747\u503c\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u67f1\u72b6\u56fe\uff0c\u5e76\u5728\u56fe\u4e0a\u6dfb\u52a0\u5e73\u5747\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u67f1\u72b6\u56fe<\/p>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>plt.bar(range(len(data)), data, color=&#39;skyblue&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u5e73\u5747\u7ebf<\/strong><\/h2>\n<p>plt.axhline(y=mean_value, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2, label=f&#39;\u5e73\u5747\u503c: {mean_value:.2f}&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;\u5b66\u751f\u8003\u8bd5\u6210\u7ee9\u67f1\u72b6\u56fe&#39;)<\/p>\n<p>plt.xlabel(&#39;\u5b66\u751f\u7f16\u53f7&#39;)<\/p>\n<p>plt.ylabel(&#39;\u6210\u7ee9&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u4f8b<\/strong><\/h2>\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><h3>\u4e94\u3001\u8be6\u7ec6\u89e3\u6790\u548c\u4f18\u5316\u56fe\u8868<\/h3>\n<\/p>\n<p><h4>1\u3001\u6dfb\u52a0\u6570\u636e\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u56fe\u8868\u66f4\u52a0\u76f4\u89c2\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u65b9\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u67f1\u72b6\u56fe<\/p>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>bars = plt.bar(range(len(data)), data, color=&#39;skyblue&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u5e73\u5747\u7ebf<\/strong><\/h2>\n<p>plt.axhline(y=mean_value, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2, label=f&#39;\u5e73\u5747\u503c: {mean_value:.2f}&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e\u6807\u7b7e<\/strong><\/h2>\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 + 1, round(yval, 2), ha=&#39;center&#39;, va=&#39;bottom&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;\u5b66\u751f\u8003\u8bd5\u6210\u7ee9\u67f1\u72b6\u56fe&#39;)<\/p>\n<p>plt.xlabel(&#39;\u5b66\u751f\u7f16\u53f7&#39;)<\/p>\n<p>plt.ylabel(&#39;\u6210\u7ee9&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u4f8b<\/strong><\/h2>\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><h4>2\u3001\u8c03\u6574\u56fe\u8868\u6837\u5f0f<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u56fe\u8868\u66f4\u52a0\u7f8e\u89c2\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u6574\u4e00\u4e9b\u6837\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u67f1\u72b6\u56fe<\/p>\n<p>plt.figure(figsize=(12, 8))<\/p>\n<p>bars = plt.bar(range(len(data)), data, color=&#39;skyblue&#39;, edgecolor=&#39;black&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u5e73\u5747\u7ebf<\/strong><\/h2>\n<p>plt.axhline(y=mean_value, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2, label=f&#39;\u5e73\u5747\u503c: {mean_value:.2f}&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e\u6807\u7b7e<\/strong><\/h2>\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 + 1, round(yval, 2), ha=&#39;center&#39;, va=&#39;bottom&#39;, fontsize=10, color=&#39;black&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;\u5b66\u751f\u8003\u8bd5\u6210\u7ee9\u67f1\u72b6\u56fe&#39;, fontsize=16)<\/p>\n<p>plt.xlabel(&#39;\u5b66\u751f\u7f16\u53f7&#39;, fontsize=14)<\/p>\n<p>plt.ylabel(&#39;\u6210\u7ee9&#39;, fontsize=14)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u523b\u5ea6<\/strong><\/h2>\n<p>plt.xticks(range(len(data)), [f&#39;\u5b66\u751f{i+1}&#39; for i in range(len(data))], fontsize=12)<\/p>\n<h2><strong>\u8bbe\u7f6ey\u8f74\u523b\u5ea6<\/strong><\/h2>\n<p>plt.yticks(fontsize=12)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend(fontsize=12)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u6dfb\u52a0\u5e73\u5747\u7ebf\u3002<strong>\u67f1\u72b6\u56fe<\/strong>\u80fd\u591f\u76f4\u89c2\u5730\u5c55\u793a\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\uff0c\u800c<strong>\u5e73\u5747\u7ebf<\/strong>\u5219\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u8bc6\u522b\u6570\u636e\u7684\u6574\u4f53\u8d8b\u52bf\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u6dfb\u52a0\u6570\u636e\u6807\u7b7e\u548c\u8c03\u6574\u56fe\u8868\u6837\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u56fe\u8868\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u89e3\u91ca\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u638c\u63e1\u5728Python\u4e2d\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u6dfb\u52a0\u5e73\u5747\u7ebf\u7684\u6280\u5de7\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7684\u67f1\u72b6\u56fe\u4e2d\u8ba1\u7b97\u5e73\u5747\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u8ba1\u7b97\u6570\u636e\u7684\u5e73\u5747\u503c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u5b89\u88c5\u5e76\u5bfc\u5165NumPy\u3002\u53ef\u4ee5\u901a\u8fc7<code>numpy.mean()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u6570\u636e\u96c6\u7684\u5e73\u5747\u503c\uff0c\u968f\u540e\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u503c\u7528\u4e8e\u7ed8\u5236\u5e73\u5747\u7ebf\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e2a\u5e93\u6765\u7ed8\u5236\u67f1\u72b6\u56fe\u548c\u5e73\u5747\u7ebf\u6700\u4e3a\u5408\u9002\uff1f<\/strong><br \/>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\uff0c\u5305\u62ec\u67f1\u72b6\u56fe\u3002\u7ed3\u5408Seaborn\u5e93\uff0c\u53ef\u4ee5\u4f7f\u56fe\u8868\u66f4\u52a0\u7f8e\u89c2\u3002\u4f7f\u7528\u8fd9\u4e24\u4e2a\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u67f1\u72b6\u56fe\u4e0a\u6dfb\u52a0\u5e73\u5747\u7ebf\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u67f1\u72b6\u56fe\u4e2d\u6dfb\u52a0\u5e73\u5747\u7ebf\u7684\u4ee3\u7801\u793a\u4f8b\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Matplotlib\u4e2d\u7684<code>axhline()<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u6c34\u5e73\u7ebf\uff0c\u8868\u793a\u5e73\u5747\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\ndata = [3, 5, 2, 8, 7]\nmean_value = np.mean(data)\n\nplt.bar(range(len(data)), data)\nplt.axhline(mean_value, color=&#39;red&#39;, linestyle=&#39;--&#39;, label=&#39;\u5e73\u5747\u503c&#39;)\nplt.legend()\nplt.show()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u5728\u67f1\u72b6\u56fe\u4e0a\u6e05\u6670\u5730\u770b\u5230\u5e73\u5747\u7ebf\uff0c\u4fbf\u4e8e\u8fdb\u884c\u6570\u636e\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7matplotlib\u5e93\u5728\u67f1\u72b6\u56fe\u4e0a\u52a0\u4e0a\u5e73\u5747\u7ebf\u3002 \u8fd9\u79cd\u65b9\u6cd5\u65e2\u80fd\u5e2e\u52a9\u6211\u4eec\u76f4\u89c2\u5730\u5c55\u793a\u6570\u636e\u5206\u5e03 [&hellip;]","protected":false},"author":3,"featured_media":1135600,"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\/1135591"}],"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=1135591"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135591\/revisions"}],"predecessor-version":[{"id":1135602,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135591\/revisions\/1135602"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135600"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}