{"id":1038990,"date":"2024-12-31T12:28:48","date_gmt":"2024-12-31T04:28:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1038990.html"},"modified":"2024-12-31T12:28:51","modified_gmt":"2024-12-31T04:28:51","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e7%9b%b4%e6%96%b9%e5%9b%be%e4%b8%8a%e6%b7%bb%e5%8a%a0%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1038990.html","title":{"rendered":"python\u5982\u4f55\u5728\u76f4\u65b9\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/a21c1f47-068c-4aaa-84b4-1291fb7e5edb.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5728\u76f4\u65b9\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57\" \/><\/p>\n<p><p> \u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Matplotlib \u5e93\u6765\u521b\u5efa\u76f4\u65b9\u56fe\uff0c\u5e76\u5728\u76f4\u65b9\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57\u3002<strong>\u901a\u8fc7\u4f7f\u7528 <code>plt.hist()<\/code> \u521b\u5efa\u76f4\u65b9\u56fe\u3001\u4f7f\u7528 <code>plt.text()<\/code> \u6dfb\u52a0\u6570\u5b57\u3001\u4f7f\u7528 <code>ax.hist()<\/code> \u548c <code>ax.text()<\/code> \u65b9\u6cd5\u5728\u9762\u5411\u5bf9\u8c61\u7684\u754c\u9762\u4e2d\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd<\/strong>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 plt.hist() \u521b\u5efa\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528 Matplotlib \u5e93\u4e2d\u7684 <code>plt.hist()<\/code> \u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u76f4\u65b9\u56fe\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>plt.text()<\/code> \u51fd\u6570\u5728\u6bcf\u4e2a\u6761\u5f62\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57\u3002<\/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>\u751f\u6210\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.randn(1000)<\/p>\n<h2><strong>\u521b\u5efa\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>plt.hist(data, bins=30, edgecolor=&#39;black&#39;)<\/p>\n<h2><strong>\u5728\u6bcf\u4e2a\u6761\u5f62\u4e0a\u6dfb\u52a0\u6570\u5b57<\/strong><\/h2>\n<p>counts, bins, patches = plt.hist(data, bins=30, edgecolor=&#39;black&#39;)<\/p>\n<p>for count, patch in zip(counts, patches):<\/p>\n<p>    height = patch.get_height()<\/p>\n<p>    plt.text(patch.get_x() + patch.get_width() \/ 2, height, int(height), ha=&#39;center&#39;, va=&#39;bottom&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.title(&#39;Histogram with Numbers on Bars&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u751f\u6210\u4e00\u4e9b\u968f\u673a\u6570\u636e\u5e76\u4f7f\u7528 <code>plt.hist()<\/code> \u521b\u5efa\u76f4\u65b9\u56fe\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>plt.text()<\/code> \u5728\u6bcf\u4e2a\u6761\u5f62\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57\u3002<strong>\u5176\u4e2d\uff0c<code>plt.text()<\/code> \u51fd\u6570\u7684\u53c2\u6570\u5305\u62ec x \u5750\u6807\u3001y \u5750\u6807\u3001\u8981\u663e\u793a\u7684\u6587\u672c\u3001\u6c34\u5e73\u5bf9\u9f50\u65b9\u5f0f\u548c\u5782\u76f4\u5bf9\u9f50\u65b9\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u754c\u9762<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u66f4\u559c\u6b22\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u754c\u9762\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ax.hist()<\/code> \u548c <code>ax.text()<\/code> \u65b9\u6cd5\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002<\/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>\u751f\u6210\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.randn(1000)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u548c\u8f74<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u521b\u5efa\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>counts, bins, patches = ax.hist(data, bins=30, edgecolor=&#39;black&#39;)<\/p>\n<h2><strong>\u5728\u6bcf\u4e2a\u6761\u5f62\u4e0a\u6dfb\u52a0\u6570\u5b57<\/strong><\/h2>\n<p>for count, patch in zip(counts, patches):<\/p>\n<p>    height = patch.get_height()<\/p>\n<p>    ax.text(patch.get_x() + patch.get_width() \/ 2, height, int(height), ha=&#39;center&#39;, va=&#39;bottom&#39;)<\/p>\n<p>ax.set_xlabel(&#39;Value&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Frequency&#39;)<\/p>\n<p>ax.set_title(&#39;Histogram with Numbers on Bars&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u751f\u6210\u4e00\u4e9b\u968f\u673a\u6570\u636e\u5e76\u521b\u5efa\u56fe\u5f62\u548c\u8f74\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>ax.hist()<\/code> \u521b\u5efa\u76f4\u65b9\u56fe\uff0c\u5e76\u4f7f\u7528 <code>ax.text()<\/code> \u5728\u6bcf\u4e2a\u6761\u5f62\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8fdb\u4e00\u6b65\u4f18\u5316\u76f4\u65b9\u56fe<\/h3>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u76f4\u65b9\u56fe\uff0c\u4f8b\u5982\u8c03\u6574\u6587\u672c\u7684\u683c\u5f0f\u548c\u4f4d\u7f6e\u3002<\/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>\u751f\u6210\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.randn(1000)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u548c\u8f74<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u521b\u5efa\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>counts, bins, patches = ax.hist(data, bins=30, edgecolor=&#39;black&#39;)<\/p>\n<h2><strong>\u5728\u6bcf\u4e2a\u6761\u5f62\u4e0a\u6dfb\u52a0\u6570\u5b57<\/strong><\/h2>\n<p>for count, patch in zip(counts, patches):<\/p>\n<p>    height = patch.get_height()<\/p>\n<p>    ax.text(patch.get_x() + patch.get_width() \/ 2, height + 1, int(height), ha=&#39;center&#39;, va=&#39;bottom&#39;, fontsize=8, color=&#39;blue&#39;)<\/p>\n<p>ax.set_xlabel(&#39;Value&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Frequency&#39;)<\/p>\n<p>ax.set_title(&#39;Histogram with Numbers on Bars&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 <code>ax.text()<\/code> \u51fd\u6570\u4e2d\u589e\u52a0\u4e86 <code>fontsize<\/code> \u548c <code>color<\/code> \u53c2\u6570\uff0c\u4ee5\u8c03\u6574\u6587\u672c\u7684\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\u3002<strong>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u5c06\u6587\u672c\u7684\u4f4d\u7f6e\u7a0d\u5fae\u5411\u4e0a\u79fb\u52a8\u4e86\u4e00\u4e2a\u5355\u4f4d\uff0c\u4ee5\u4f7f\u5176\u4e0e\u6761\u5f62\u56fe\u66f4\u597d\u5730\u5bf9\u9f50\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728 Python \u4e2d\u4f7f\u7528 Matplotlib \u5e93\u521b\u5efa\u76f4\u65b9\u56fe\uff0c\u5e76\u5728\u6bcf\u4e2a\u6761\u5f62\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57\u3002<strong>\u65e0\u8bba\u662f\u4f7f\u7528 <code>plt.hist()<\/code> \u8fd8\u662f <code>ax.hist()<\/code>\uff0c\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff0c\u5e76\u4e14\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u76f4\u65b9\u56fe\u7684\u5916\u89c2\u3002<\/strong>\u8fd9\u4e9b\u6280\u5de7\u4e0d\u4ec5\u9002\u7528\u4e8e\u76f4\u65b9\u56fe\uff0c\u8fd8\u53ef\u4ee5\u5e94\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u4ee5\u63d0\u9ad8\u6570\u636e\u53ef\u89c6\u5316\u7684\u6548\u679c\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u76f4\u65b9\u56fe\u4e2d\u663e\u793a\u6bcf\u4e2a\u67f1\u5b50\u7684\u6570\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u521b\u5efa\u76f4\u65b9\u56fe\uff0c\u5e76\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u67f1\u5b50\u7684\u4f4d\u7f6e\u6765\u6dfb\u52a0\u6570\u503c\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u7ed8\u5236\u76f4\u65b9\u56fe\u540e\uff0c\u901a\u8fc7<code>ax.text()<\/code>\u65b9\u6cd5\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u65b9\u6216\u5185\u90e8\u6dfb\u52a0\u5bf9\u5e94\u7684\u6570\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\ndata = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]\nplt.hist(data, bins=4, edgecolor=&#39;black&#39;)\nplt.title(&#39;Histogram with Values&#39;)\nplt.xlabel(&#39;Values&#39;)\nplt.ylabel(&#39;Frequency&#39;)\n\n# \u6dfb\u52a0\u6570\u503c\ncounts, bins = np.histogram(data, bins=4)\nfor count, x in zip(counts, bins):\n    plt.text(x + 0.1, count, str(count), ha=&#39;center&#39;)\n\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u663e\u793a\u5176\u5bf9\u5e94\u7684\u9891\u7387\u3002<\/p>\n<p><strong>\u76f4\u65b9\u56fe\u4e0a\u6570\u5b57\u7684\u5b57\u4f53\u548c\u989c\u8272\u5982\u4f55\u81ea\u5b9a\u4e49\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>fontsize<\/code>\u548c<code>color<\/code>\u53c2\u6570\u6765\u5b9a\u5236\u76f4\u65b9\u56fe\u4e0a\u6570\u5b57\u7684\u5916\u89c2\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\uff0c\u4f7f\u5176\u4e0e\u56fe\u8868\u7684\u6574\u4f53\u98ce\u683c\u76f8\u5339\u914d\u3002\u4fee\u6539<code>plt.text()<\/code>\u4e2d\u7684\u53c2\u6570\u5373\u53ef\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\uff1a<\/p>\n<pre><code class=\"language-python\">plt.text(x + 0.1, count, str(count), ha=&#39;center&#39;, fontsize=12, color=&#39;red&#39;)\n<\/code><\/pre>\n<p><strong>\u5728\u76f4\u65b9\u56fe\u4e0a\u6dfb\u52a0\u767e\u5206\u6bd4\u800c\u4e0d\u662f\u9891\u6570\u53ef\u4ee5\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5728\u8ba1\u7b97\u9891\u7387\u6216\u767e\u5206\u6bd4\u65f6\uff0c\u53ef\u4ee5\u5148\u8ba1\u7b97\u6bcf\u4e2a\u67f1\u5b50\u7684\u9891\u6570\uff0c\u7136\u540e\u9664\u4ee5\u6570\u636e\u603b\u91cf\u5e76\u4e58\u4ee5100\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u5728\u76f4\u65b9\u56fe\u4e0a\u663e\u793a\u6bcf\u4e2a\u67f1\u5b50\u6240\u5360\u7684\u767e\u5206\u6bd4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">percentages = (counts \/ sum(counts)) * 100\nfor percent, x in zip(percentages, bins):\n    plt.text(x + 0.1, percent, f&#39;{percent:.1f}%&#39;, ha=&#39;center&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u4f7f\u76f4\u65b9\u56fe\u4e0a\u663e\u793a\u6bcf\u4e2a\u67f1\u5b50\u7684\u767e\u5206\u6bd4\uff0c\u63d0\u4f9b\u66f4\u76f4\u89c2\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Matplotlib \u5e93\u6765\u521b\u5efa\u76f4\u65b9\u56fe\uff0c\u5e76\u5728\u76f4\u65b9\u56fe\u4e0a\u6dfb\u52a0\u6570\u5b57\u3002\u901a\u8fc7\u4f7f\u7528 plt. [&hellip;]","protected":false},"author":3,"featured_media":1039003,"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\/1038990"}],"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=1038990"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1038990\/revisions"}],"predecessor-version":[{"id":1039004,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1038990\/revisions\/1039004"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1039003"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1038990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1038990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1038990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}