{"id":996848,"date":"2024-12-27T09:18:26","date_gmt":"2024-12-27T01:18:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/996848.html"},"modified":"2024-12-27T09:18:28","modified_gmt":"2024-12-27T01:18:28","slug":"python%e6%95%b0%e6%8d%ae%e7%9b%b4%e6%96%b9%e5%9b%be%e5%a6%82%e4%bd%95%e7%94%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/996848.html","title":{"rendered":"python\u6570\u636e\u76f4\u65b9\u56fe\u5982\u4f55\u753b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072919\/0388fb48-4ecf-4c03-b0a4-8e427b86886d.webp\" alt=\"python\u6570\u636e\u76f4\u65b9\u56fe\u5982\u4f55\u753b\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u7ed8\u5236\u6570\u636e\u76f4\u65b9\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5de5\u5177\u548c\u5e93\uff0c\u5982Matplotlib\u3001Seaborn\u548cPandas\u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ee5\u4e0d\u540c\u65b9\u5f0f\u53ef\u89c6\u5316\u6570\u636e\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u6240\u9700\u7684\u5e93\uff0c\u7136\u540e\u901a\u8fc7\u5bfc\u5165\u6570\u636e\u3001\u9009\u62e9\u5408\u9002\u7684\u53c2\u6570\u548c\u6837\u5f0f\u3001\u7ed8\u5236\u5e76\u8c03\u6574\u76f4\u65b9\u56fe\u3001\u89e3\u91ca\u6570\u636e\u5206\u5e03\u7b49\u6b65\u9aa4\u5b8c\u6210\u7ed8\u56fe\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u4f7f\u7528Matplotlib\u7ed8\u5236\u76f4\u65b9\u56fe\u7684\u6b65\u9aa4\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u76f4\u65b9\u56fe\u4e4b\u524d\uff0c\u9996\u5148\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p>pip install numpy  # \u5982\u679c\u9700\u8981\u751f\u6210\u968f\u673a\u6570\u636e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5bfc\u5165\u5e93\u548c\u51c6\u5907\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u76f4\u65b9\u56fe\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\u4ee5\u53canumpy\u5e93\uff08\u5982\u679c\u9700\u8981\u751f\u6210\u968f\u673a\u6570\u636e\uff09\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u6216\u5bfc\u5165\u6211\u4eec\u60f3\u8981\u5206\u6790\u7684\u6570\u636e\u96c6\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\u968f\u673a\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.randn(1000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u7ed8\u5236\u76f4\u65b9\u56fe<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u53ef\u4ee5\u8f7b\u677e\u5730\u7ed8\u5236\u76f4\u65b9\u56fe\u3002\u6211\u4eec\u901a\u8fc7\u8c03\u7528<code>plt.hist()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u76f4\u65b9\u56fe\uff0c\u5e76\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570\u6765\u8c03\u6574\u76f4\u65b9\u56fe\u7684\u5916\u89c2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.hist(data, bins=30, alpha=0.7, color=&#39;blue&#39;, edgecolor=&#39;black&#39;)<\/p>\n<p>plt.title(&#39;Data Distribution Histogram&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li><strong><code>bins<\/code>\u53c2\u6570<\/strong>\uff1a\u6307\u5b9a\u76f4\u65b9\u56fe\u7684\u67f1\u6570\uff0c\u8fd9\u4f1a\u5f71\u54cd\u6570\u636e\u7684\u5206\u7ec4\u65b9\u5f0f\u3002\u9009\u62e9\u5408\u9002\u7684\u67f1\u6570\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u5206\u6790\u6570\u636e\u7684\u5206\u5e03\u3002<\/li>\n<li><strong><code>alpha<\/code>\u53c2\u6570<\/strong>\uff1a\u63a7\u5236\u76f4\u65b9\u56fe\u7684\u900f\u660e\u5ea6\uff0c\u8303\u56f4\u57280\u52301\u4e4b\u95f4\u3002\u8f83\u4f4e\u7684\u503c\u4f7f\u76f4\u65b9\u56fe\u66f4\u900f\u660e\u3002<\/li>\n<li><strong><code>color<\/code>\u53c2\u6570<\/strong>\uff1a\u6307\u5b9a\u76f4\u65b9\u56fe\u7684\u989c\u8272\u3002<\/li>\n<li><strong><code>edgecolor<\/code>\u53c2\u6570<\/strong>\uff1a\u8bbe\u7f6e\u76f4\u65b9\u56fe\u67f1\u7684\u8fb9\u7f18\u989c\u8272\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u533a\u5206\u3002<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u8c03\u6574\u548c\u7f8e\u5316\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u57fa\u672c\u76f4\u65b9\u56fe\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u56fe\u5f62\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u8c03\u6574\u548c\u7f8e\u5316\uff0c\u4f7f\u5176\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u89c6\u89c9\u5438\u5f15\u529b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6dfb\u52a0\u7f51\u683c\u7ebf<\/strong>\uff1a\u901a\u8fc7<code>plt.grid(True)<\/code>\u6765\u589e\u52a0\u56fe\u5f62\u7684\u53ef\u8bfb\u6027\u3002<\/li>\n<li><strong>\u6dfb\u52a0\u6807\u9898\u548c\u8f74\u6807\u7b7e<\/strong>\uff1a<code>plt.title()<\/code>\u3001<code>plt.xlabel()<\/code>\u3001<code>plt.ylabel()<\/code>\u53ef\u4ee5\u5206\u522b\u4e3a\u56fe\u5f62\u3001x\u8f74\u548cy\u8f74\u6dfb\u52a0\u6807\u9898\u3002<\/li>\n<li><strong>\u8c03\u6574\u56fe\u5f62\u5c3a\u5bf8<\/strong>\uff1a\u4f7f\u7528<code>plt.figure(figsize=(width, height))<\/code>\u8c03\u6574\u56fe\u5f62\u7684\u5927\u5c0f\u3002<\/li>\n<li><strong>\u6dfb\u52a0\u6ce8\u91ca<\/strong>\uff1a\u901a\u8fc7<code>plt.annotate()<\/code>\u6dfb\u52a0\u6ce8\u91ca\u4ee5\u7a81\u51fa\u663e\u793a\u67d0\u4e9b\u6570\u636e\u70b9\u3002<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u89e3\u91ca\u6570\u636e\u5206\u5e03<\/p>\n<\/p>\n<p><p>\u7ed8\u5236\u76f4\u65b9\u56fe\u7684\u6700\u7ec8\u76ee\u7684\u662f\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u89c2\u5bdf\u76f4\u65b9\u56fe\u7684\u5f62\u72b6\u6765\u5f97\u51fa\u7ed3\u8bba\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bf9\u79f0\u5206\u5e03<\/strong>\uff1a\u5982\u679c\u76f4\u65b9\u56fe\u4e24\u4fa7\u5927\u81f4\u5bf9\u79f0\uff0c\u6570\u636e\u53ef\u80fd\u5448\u73b0\u6b63\u6001\u5206\u5e03\u3002<\/li>\n<li><strong>\u504f\u6001\u5206\u5e03<\/strong>\uff1a\u5982\u679c\u76f4\u65b9\u56fe\u5411\u5de6\u6216\u5411\u53f3\u503e\u659c\uff0c\u5219\u6570\u636e\u53ef\u80fd\u662f\u8d1f\u504f\u6001\u6216\u6b63\u504f\u6001\u3002<\/li>\n<li><strong>\u591a\u5cf0\u5206\u5e03<\/strong>\uff1a\u5982\u679c\u76f4\u65b9\u56fe\u663e\u793a\u591a\u4e2a\u5cf0\u503c\uff0c\u5219\u53ef\u80fd\u5b58\u5728\u591a\u4e2a\u6570\u636e\u7fa4\u7ec4\u3002<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u4f7f\u7528Seaborn\u7ed8\u5236\u66f4\u9ad8\u7ea7\u7684\u76f4\u65b9\u56fe<\/p>\n<\/p>\n<p><p>Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u53ef\u89c6\u5316\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u66f4\u7f8e\u89c2\u7684\u7edf\u8ba1\u56fe\u8868\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Seaborn\u7ed8\u5236\u76f4\u65b9\u56fe\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>sns.set(style=&quot;whitegrid&quot;)<\/p>\n<p>sns.histplot(data, bins=30, kde=True, color=&#39;blue&#39;)<\/p>\n<p>plt.title(&#39;Data Distribution Histogram with Seaborn&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Seaborn\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u6dfb\u52a0\u6838\u5bc6\u5ea6\u4f30\u8ba1\uff08KDE\uff09\u66f2\u7ebf\uff0c\u901a\u8fc7<code>kde=True<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u6761\u66f2\u7ebf\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u7ed8\u5236\u76f4\u65b9\u56fe\u662f\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u7406\u89e3\u6570\u636e\u7684\u603b\u4f53\u5206\u5e03\u7279\u5f81\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u76f4\u89c2\u4e14\u4fe1\u606f\u4e30\u5bcc\u7684\u53ef\u89c6\u5316\u56fe\u8868\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u6570\u636e\u5206\u6790\u4eba\u5458\u7406\u89e3\u6570\u636e\uff0c\u4e5f\u4e3a\u4e0e\u4ed6\u4eba\u5206\u4eab\u6570\u636e\u6d1e\u5bdf\u63d0\u4f9b\u4e86\u6709\u6548\u7684\u5de5\u5177\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u6570\u636e\u96c6\u7684\u7279\u70b9\u548c\u5206\u6790\u9700\u6c42\uff0c\u7075\u6d3b\u5730\u8c03\u6574\u7ed8\u56fe\u53c2\u6570\u548c\u6837\u5f0f\uff0c\u5c06\u5927\u5927\u63d0\u5347\u6570\u636e\u53ef\u89c6\u5316\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u6570\u636e\u76f4\u65b9\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u6570\u636e\u76f4\u65b9\u56fe\u901a\u5e38\u4f7f\u7528Matplotlib\u548cSeaborn\u5e93\u3002\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>plt.hist()<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u76f4\u65b9\u56fe\u3002\u4f60\u9700\u8981\u63d0\u4f9b\u6570\u636e\u96c6\u548c\u4e00\u4e9b\u53ef\u9009\u7684\u53c2\u6570\uff0c\u4f8b\u5982\u6876\u7684\u6570\u91cf\u548c\u989c\u8272\uff0c\u4ee5\u4fbf\u5b9a\u5236\u56fe\u8868\u7684\u5916\u89c2\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\ndata = np.random.randn(1000)  # \u751f\u6210\u968f\u673a\u6570\u636e\nplt.hist(data, bins=30, color=&#39;blue&#39;, alpha=0.7)\nplt.title(&#39;Data Histogram&#39;)\nplt.xlabel(&#39;Value&#39;)\nplt.ylabel(&#39;Frequency&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u6570\u636e\u76f4\u65b9\u56fe\u4e2d\u6876\uff08bins\uff09\u7684\u9009\u62e9\u6709\u4ec0\u4e48\u5efa\u8bae\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u6876\u6570\u91cf\u5bf9\u4e8e\u76f4\u65b9\u56fe\u7684\u53ef\u8bfb\u6027\u81f3\u5173\u91cd\u8981\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6876\u7684\u6570\u91cf\u5e94\u8be5\u6839\u636e\u6570\u636e\u7684\u8303\u56f4\u548c\u5206\u5e03\u6765\u51b3\u5b9a\u3002\u53ef\u4ee5\u4f7f\u7528Sturges\u516c\u5f0f\u6216Freedman-Diaconis\u516c\u5f0f\u6765\u8ba1\u7b97\u63a8\u8350\u7684\u6876\u6570\u3002\u6bd4\u5982\uff0cSturges\u516c\u5f0f\u4e3a<code>bins = 1 + log2(n)<\/code>\uff0c\u5176\u4e2dn\u662f\u6570\u636e\u70b9\u7684\u6570\u91cf\u3002\u9009\u62e9\u6876\u6570\u65f6\uff0c\u786e\u4fdd\u76f4\u65b9\u56fe\u80fd\u6e05\u6670\u5730\u5c55\u793a\u6570\u636e\u7684\u5206\u5e03\u7279\u5f81\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u76f4\u65b9\u56fe\u4e2d\u6dfb\u52a0\u56fe\u4f8b\u548c\u6807\u7b7e\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u76f4\u65b9\u56fe\u65f6\uff0c\u4e3a\u4e86\u66f4\u597d\u5730\u4f20\u8fbe\u4fe1\u606f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>plt.legend()<\/code>\u548c<code>plt.xlabel()<\/code>\u3001<code>plt.ylabel()<\/code>\u51fd\u6570\u6dfb\u52a0\u56fe\u4f8b\u548c\u8f74\u6807\u7b7e\u3002\u56fe\u4f8b\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u4e0d\u540c\u6570\u636e\u7cfb\u5217\u7684\u542b\u4e49\uff0c\u800c\u8f74\u6807\u7b7e\u5219\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u4e0a\u4e0b\u6587\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">plt.hist(data1, bins=30, alpha=0.5, label=&#39;Data Set 1&#39;)\nplt.hist(data2, bins=30, alpha=0.5, label=&#39;Data Set 2&#39;)\nplt.legend()\nplt.xlabel(&#39;Value&#39;)\nplt.ylabel(&#39;Frequency&#39;)\nplt.title(&#39;Comparison of Two Data Sets&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u8bfb\u8005\u80fd\u591f\u66f4\u6e05\u6670\u5730\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u6bd4\u8f83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u7ed8\u5236\u6570\u636e\u76f4\u65b9\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5de5\u5177\u548c\u5e93\uff0c\u5982Matplotlib\u3001Seaborn\u548cPandas\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":996852,"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\/996848"}],"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=996848"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996848\/revisions"}],"predecessor-version":[{"id":996856,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996848\/revisions\/996856"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/996852"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=996848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=996848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=996848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}