{"id":1074052,"date":"2025-01-08T11:31:10","date_gmt":"2025-01-08T03:31:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074052.html"},"modified":"2025-01-08T11:31:12","modified_gmt":"2025-01-08T03:31:12","slug":"python%e4%b8%ad%e6%9f%b1%e7%8a%b6%e5%9b%be%e5%a6%82%e4%bd%95%e6%a0%87%e4%b8%8a%e6%95%b0%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1074052.html","title":{"rendered":"python\u4e2d\u67f1\u72b6\u56fe\u5982\u4f55\u6807\u4e0a\u6570\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103818\/60ae57fc-baf4-4013-b5b3-192345b4336a.webp\" alt=\"python\u4e2d\u67f1\u72b6\u56fe\u5982\u4f55\u6807\u4e0a\u6570\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u5728\u67f1\u72b6\u56fe\u4e0a\u6807\u6ce8\u6570\u503c\u3002<\/strong>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>plt.bar()<\/code>\u51fd\u6570\u7ed8\u5236\u67f1\u72b6\u56fe\uff0c\u5e76\u4f7f\u7528<code>plt.text()<\/code>\u51fd\u6570\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u6dfb\u52a0\u6570\u503c\u6807\u6ce8\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\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\u7ed8\u5236\u67f1\u72b6\u56fe\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\u5b83\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\u5728\u4f60\u7684Python\u811a\u672c\u6216Jupyter Notebook\u4e2d\u5bfc\u5165\u5fc5\u8981\u7684\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\u7ed8\u5236\u67f1\u72b6\u56fe<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u51c6\u5907\u6570\u636e\u5e76\u7ed8\u5236\u67f1\u72b6\u56fe\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>values = [23, 45, 56, 78, 89]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>plt.bar()<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.figure(figsize=(10, 6))<\/p>\n<p>bars = plt.bar(categories, values, color=&#39;skyblue&#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 Value Labels&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5728\u67f1\u72b6\u56fe\u4e0a\u6807\u6ce8\u6570\u503c<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u67f1\u72b6\u56fe\u4e0a\u6807\u6ce8\u6570\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.text()<\/code>\u51fd\u6570\u3002\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u65b9\u6dfb\u52a0\u6570\u503c\u6807\u6ce8\u3002\u6211\u4eec\u53ef\u4ee5\u904d\u5386<code>bars<\/code>\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>bar.get_height()<\/code>\u83b7\u53d6\u6bcf\u4e2a\u67f1\u5b50\u7684\u9ad8\u5ea6\uff0c\u7136\u540e\u5728\u9002\u5f53\u7684\u4f4d\u7f6e\u6dfb\u52a0\u6570\u503c\u6807\u6ce8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for bar in bars:<\/p>\n<p>    height = bar.get_height()<\/p>\n<p>    plt.text(bar.get_x() + bar.get_width() \/ 2.0, height, f&#39;{height}&#39;, ha=&#39;center&#39;, va=&#39;bottom&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\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<h2><strong>\u6570\u636e\u51c6\u5907<\/strong><\/h2>\n<p>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>values = [23, 45, 56, 78, 89]<\/p>\n<h2><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 6))<\/p>\n<p>bars = plt.bar(categories, values, color=&#39;skyblue&#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 Value Labels&#39;)<\/p>\n<h2><strong>\u5728\u67f1\u72b6\u56fe\u4e0a\u6807\u6ce8\u6570\u503c<\/strong><\/h2>\n<p>for bar in bars:<\/p>\n<p>    height = bar.get_height()<\/p>\n<p>    plt.text(bar.get_x() + bar.get_width() \/ 2.0, height, f&#39;{height}&#39;, ha=&#39;center&#39;, va=&#39;bottom&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8c03\u6574\u6807\u6ce8\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8c03\u6574\u6807\u6ce8\u7684\u6837\u5f0f\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u4e0e\u56fe\u8868\u5339\u914d\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u66f4\u6539\u5b57\u4f53\u5927\u5c0f\u3001\u989c\u8272\u3001\u65cb\u8f6c\u89d2\u5ea6\u7b49\u3002\u53ef\u4ee5\u5728<code>plt.text()<\/code>\u51fd\u6570\u4e2d\u6dfb\u52a0\u5176\u4ed6\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for bar in bars:<\/p>\n<p>    height = bar.get_height()<\/p>\n<p>    plt.text(bar.get_x() + bar.get_width() \/ 2.0, height, f&#39;{height}&#39;, ha=&#39;center&#39;, va=&#39;bottom&#39;, fontsize=12, color=&#39;black&#39;, fontweight=&#39;bold&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u8d1f\u503c\u548c\u96f6\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8d1f\u503c\u548c\u96f6\u503c\u65f6\uff0c\u6dfb\u52a0\u6807\u6ce8\u7684\u4f4d\u7f6e\u53ef\u80fd\u9700\u8981\u8c03\u6574\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u8d1f\u503c\uff0c\u53ef\u4ee5\u5c06\u6807\u6ce8\u653e\u5728\u67f1\u5b50\u7684\u4e0b\u65b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for bar in bars:<\/p>\n<p>    height = bar.get_height()<\/p>\n<p>    if height &lt; 0:<\/p>\n<p>        plt.text(bar.get_x() + bar.get_width() \/ 2.0, height, f&#39;{height}&#39;, ha=&#39;center&#39;, va=&#39;top&#39;, fontsize=12, color=&#39;red&#39;, fontweight=&#39;bold&#39;)<\/p>\n<p>    else:<\/p>\n<p>        plt.text(bar.get_x() + bar.get_width() \/ 2.0, height, f&#39;{height}&#39;, ha=&#39;center&#39;, va=&#39;bottom&#39;, fontsize=12, color=&#39;black&#39;, fontweight=&#39;bold&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u81ea\u5b9a\u4e49\u67f1\u72b6\u56fe\u7684\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u81ea\u5b9a\u4e49\u67f1\u72b6\u56fe\u7684\u6837\u5f0f\uff0c\u4f7f\u5176\u66f4\u52a0\u7f8e\u89c2\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u66f4\u6539\u67f1\u5b50\u7684\u989c\u8272\u3001\u6dfb\u52a0\u7f51\u683c\u7ebf\u3001\u8c03\u6574\u56fe\u8868\u7684\u8fb9\u8ddd\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.figure(figsize=(12, 8))<\/p>\n<p>bars = plt.bar(categories, values, color=[&#39;#FF6347&#39;, &#39;#4682B4&#39;, &#39;#FFD700&#39;, &#39;#ADFF2F&#39;, &#39;#FF69B4&#39;])<\/p>\n<p>plt.xlabel(&#39;Categories&#39;, fontsize=14)<\/p>\n<p>plt.ylabel(&#39;Values&#39;, fontsize=14)<\/p>\n<p>plt.title(&#39;Customized Bar Chart with Value Labels&#39;, fontsize=16)<\/p>\n<p>plt.grid(True, linestyle=&#39;--&#39;, alpha=0.6)<\/p>\n<p>for bar in bars:<\/p>\n<p>    height = bar.get_height()<\/p>\n<p>    plt.text(bar.get_x() + bar.get_width() \/ 2.0, height, f&#39;{height}&#39;, ha=&#39;center&#39;, va=&#39;bottom&#39;, fontsize=12, color=&#39;black&#39;, fontweight=&#39;bold&#39;)<\/p>\n<p>plt.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u5728\u67f1\u72b6\u56fe\u4e0a\u6807\u6ce8\u6570\u503c\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u8c03\u6574\u548c\u81ea\u5b9a\u4e49\u56fe\u8868\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u4e3a\u67f1\u72b6\u56fe\u6dfb\u52a0\u6570\u503c\u6807\u7b7e\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u67f1\u72b6\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>text()<\/code>\u51fd\u6570\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u65b9\u6dfb\u52a0\u6570\u503c\u6807\u7b7e\u3002\u9996\u5148\uff0c\u7ed8\u5236\u67f1\u72b6\u56fe\u540e\uff0c\u904d\u5386\u6bcf\u4e2a\u67f1\u5b50\u7684\u9ad8\u5ea6\uff0c\u4f7f\u7528<code>text()<\/code>\u51fd\u6570\u6307\u5b9a\u4f4d\u7f6e\u548c\u663e\u793a\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\ndata = [3, 7, 5, 10]\nplt.bar(range(len(data)), data)\n\nfor i, v in enumerate(data):\n    plt.text(i, v + 0.2, str(v), ha=&#39;center&#39;)\n\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u65b9\u90fd\u4f1a\u663e\u793a\u5bf9\u5e94\u7684\u6570\u503c\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Seaborn\u7ed8\u5236\u7684\u67f1\u72b6\u56fe\u4e2d\u5982\u4f55\u6dfb\u52a0\u6570\u503c\u6807\u7b7e\uff1f<\/strong><br \/>Seaborn\u662f\u6784\u5efa\u5728Matplotlib\u4e4b\u4e0a\u7684\u4e00\u4e2a\u53ef\u89c6\u5316\u5e93\uff0c\u60a8\u53ef\u4ee5\u540c\u6837\u4f7f\u7528<code>text()<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u6807\u7b7e\u3002\u5728\u7ed8\u5236\u5b8cSeaborn\u7684\u67f1\u72b6\u56fe\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u4f3c\u7684\u65b9\u5f0f\u904d\u5386\u6570\u636e\u5e76\u6dfb\u52a0\u6570\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import seaborn as sns\nimport matplotlib.pyplot as plt\n\ndata = [3, 7, 5, 10]\nsns.barplot(x=list(range(len(data))), y=data)\n\nfor i, v in enumerate(data):\n    plt.text(i, v + 0.2, str(v), ha=&#39;center&#39;)\n\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u4e3a\u6bcf\u4e2a\u67f1\u5b50\u6dfb\u52a0\u76f8\u5e94\u7684\u6570\u503c\u6807\u7b7e\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u67f1\u72b6\u56fe\u4e2d\u6570\u503c\u6807\u7b7e\u7684\u6837\u5f0f\uff1f<\/strong><br \/>\u81ea\u5b9a\u4e49\u6570\u503c\u6807\u7b7e\u7684\u6837\u5f0f\u53ef\u4ee5\u901a\u8fc7<code>text()<\/code>\u51fd\u6570\u7684\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\u3001\u989c\u8272\u548c\u5bf9\u9f50\u65b9\u5f0f\u7b49\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">for i, v in enumerate(data):\n    plt.text(i, v + 0.2, str(v), ha=&#39;center&#39;, fontsize=12, color=&#39;blue&#39;, fontweight=&#39;bold&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u8ba9\u6570\u503c\u6807\u7b7e\u66f4\u5177\u5438\u5f15\u529b\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u5728\u67f1\u72b6\u56fe\u4e0a\u6807\u6ce8\u6570\u503c\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528plt.bar [&hellip;]","protected":false},"author":3,"featured_media":1074060,"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\/1074052"}],"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=1074052"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074052\/revisions"}],"predecessor-version":[{"id":1074062,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074052\/revisions\/1074062"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1074060"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1074052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1074052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1074052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}