{"id":1068701,"date":"2024-12-31T16:45:47","date_gmt":"2024-12-31T08:45:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068701.html"},"modified":"2024-12-31T16:45:49","modified_gmt":"2024-12-31T08:45:49","slug":"python%e5%a6%82%e4%bd%95%e5%81%9a%e9%a2%91%e6%95%b0%e5%88%86%e5%b8%83%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1068701.html","title":{"rendered":"python\u5982\u4f55\u505a\u9891\u6570\u5206\u5e03\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c51a362e-038c-4ab1-9fa2-b46b57104693.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u505a\u9891\u6570\u5206\u5e03\u56fe\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528matplotlib\u3001seaborn\u3001pandas\u5e93\u6765\u5236\u4f5c\u9891\u6570\u5206\u5e03\u56fe\u3002<\/strong>\u8fd9\u4e09\u8005\u5404\u6709\u4f18\u52bf\uff0c\u5176\u4e2dmatplotlib\u9002\u5408\u57fa\u7840\u7ed8\u56fe\uff0cseaborn\u9002\u5408\u66f4\u9ad8\u7ea7\u548c\u7f8e\u89c2\u7684\u56fe\u8868\uff0c\u800cpandas\u5219\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u6570\u636e\u5904\u7406\u548c\u76f4\u63a5\u7ed8\u56fe\u7684\u529f\u80fd\u3002\u6211\u4eec\u5c06\u5177\u4f53\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u5de5\u5177\u6765\u5236\u4f5c\u9891\u6570\u5206\u5e03\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Matplotlib\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u7ed8\u56fe\u5e93\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\uff0c\u662f\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Matplotlib<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5matplotlib\uff0c\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><h4>\u4f7f\u7528Matplotlib\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Matplotlib\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]<\/p>\n<h2><strong>\u7ed8\u5236\u9891\u6570\u5206\u5e03\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>plt.hist(data, bins=5, edgecolor=&#39;black&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Frequency Distribution&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>plt.hist()<\/code>\u51fd\u6570\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe\uff0c\u5176\u4e2d<code>data<\/code>\u662f\u6211\u4eec\u7684\u6570\u636e\uff0c<code>bins<\/code>\u53c2\u6570\u6307\u5b9a\u4e86\u5206\u7ec4\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Seaborn\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u7ed8\u5236\u66f4\u7f8e\u89c2\u548c\u590d\u6742\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Seaborn<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5seaborn\uff0c\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 seaborn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Seaborn\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Seaborn\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]<\/p>\n<h2><strong>\u7ed8\u5236\u9891\u6570\u5206\u5e03\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>sns.histplot(data, bins=5, kde=False)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Frequency Distribution&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>sns.histplot()<\/code>\u51fd\u6570\u7528\u4e8e\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe\uff0c<code>kde=False<\/code>\u53c2\u6570\u8868\u793a\u4e0d\u663e\u793a\u6838\u5bc6\u5ea6\u4f30\u8ba1\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u6700\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\u4e4b\u4e00\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5904\u7406\uff0c\u8fd8\u63d0\u4f9b\u4e86\u76f4\u63a5\u7ed8\u56fe\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5pandas\uff0c\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 pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Pandas\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Pandas\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data, columns=[&#39;Value&#39;])<\/p>\n<h2><strong>\u7ed8\u5236\u9891\u6570\u5206\u5e03\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>df[&#39;Value&#39;].plot(kind=&#39;hist&#39;, bins=5, edgecolor=&#39;black&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Frequency Distribution&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aDataFrame\uff0c\u7136\u540e\u4f7f\u7528<code>plot()<\/code>\u51fd\u6570\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u4f7f\u7528Numpy\u548cPandas\u8fdb\u884c\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u9700\u8981\u8fdb\u884c\u9884\u5904\u7406\uff0cNumpy\u548cPandas\u7684\u7ed3\u5408\u4f7f\u7528\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Numpy<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5numpy\uff0c\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 numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6570\u636e\u5904\u7406\u548c\u7ed8\u5236\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed3\u5408\u4f7f\u7528Numpy\u548cPandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u5e76\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>np.random.seed(0)<\/p>\n<p>data = np.random.normal(loc=0, scale=1, size=1000)<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data, columns=[&#39;Value&#39;])<\/p>\n<h2><strong>\u7ed8\u5236\u9891\u6570\u5206\u5e03\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>df[&#39;Value&#39;].plot(kind=&#39;hist&#39;, bins=30, edgecolor=&#39;black&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Frequency Distribution&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Numpy\u751f\u6210\u4e86\u4e00\u7ec4\u6b63\u6001\u5206\u5e03\u7684\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u5e76\u7ed8\u5236\u9891\u6570\u5206\u5e03\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Matplotlib\u9002\u5408\u57fa\u7840\u7ed8\u56fe\uff0cseaborn\u9002\u5408\u66f4\u9ad8\u7ea7\u548c\u7f8e\u89c2\u7684\u56fe\u8868\uff0cpandas\u5219\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u6570\u636e\u5904\u7406\u548c\u76f4\u63a5\u7ed8\u56fe\u7684\u529f\u80fd\u3002<\/strong>\u5728\u9009\u62e9\u5de5\u5177\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u5177\u4f53\u6570\u636e\u8fdb\u884c\u66f4\u591a\u7684\u9884\u5904\u7406\u548c\u8c03\u6574\uff0c\u5e76\u8fdb\u884c\u66f4\u8be6\u7ec6\u7684\u56fe\u8868\u7f8e\u5316\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u5206\u6790\uff0c\u4e3a\u51b3\u7b56\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u9891\u6570\u5206\u5e03\u56fe\u662f\u4ec0\u4e48\uff1f\u5b83\u6709\u4ec0\u4e48\u7528\u5904\uff1f<\/strong><br \/>\u9891\u6570\u5206\u5e03\u56fe\u662f\u4e00\u79cd\u5c55\u793a\u6570\u636e\u96c6\u4e2d\u7279\u5b9a\u503c\u6216\u533a\u95f4\u5185\u6570\u636e\u9891\u7387\u7684\u56fe\u5f62\u5de5\u5177\u3002\u5b83\u901a\u8fc7\u53ef\u89c6\u5316\u65b9\u5f0f\u5e2e\u52a9\u5206\u6790\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\uff0c\u8bc6\u522b\u6570\u636e\u4e2d\u7684\u6a21\u5f0f\u3001\u8d8b\u52bf\u548c\u5f02\u5e38\u503c\u3002\u9891\u6570\u5206\u5e03\u56fe\u5728\u6570\u636e\u5206\u6790\u3001\u7edf\u8ba1\u7814\u7a76\u548c\u5e02\u573a\u8c03\u67e5\u4e2d\u5e7f\u6cdb\u5e94\u7528\uff0c\u80fd\u591f\u5e2e\u52a9\u51b3\u7b56\u8005\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u9891\u6570\u5206\u5e03\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecMatplotlib\u3001Seaborn\u548cPandas\u3002Matplotlib\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u7ed8\u56fe\u529f\u80fd\uff0cSeaborn\u80fd\u591f\u521b\u5efa\u66f4\u7f8e\u89c2\u7684\u7edf\u8ba1\u56fe\u5f62\uff0c\u800cPandas\u5219\u65b9\u4fbf\u4e8e\u6570\u636e\u5904\u7406\u548c\u5feb\u901f\u7ed8\u56fe\u3002\u7ed3\u5408\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u9ad8\u6548\u5730\u751f\u6210\u9891\u6570\u5206\u5e03\u56fe\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u53ef\u89c6\u5316\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6570\u636e\u4ee5\u751f\u6210\u9891\u6570\u5206\u5e03\u56fe\uff1f<\/strong><br \/>\u751f\u6210\u9891\u6570\u5206\u5e03\u56fe\u7684\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u6570\u636e\u6536\u96c6\u3001\u6570\u636e\u6e05\u6d17\u548c\u6570\u636e\u53ef\u89c6\u5316\u3002\u9996\u5148\uff0c\u9700\u8981\u5c06\u6570\u636e\u5bfc\u5165Python\u73af\u5883\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528Pandas\u8bfb\u53d6CSV\u6216Excel\u6587\u4ef6\u3002\u63a5\u7740\uff0c\u4f7f\u7528Pandas\u7684<code>value_counts()<\/code>\u65b9\u6cd5\u7edf\u8ba1\u5404\u4e2a\u7c7b\u522b\u7684\u9891\u6570\uff0c\u7136\u540e\u53ef\u4ee5\u7528Matplotlib\u6216Seaborn\u5c06\u7ed3\u679c\u53ef\u89c6\u5316\u4e3a\u6761\u5f62\u56fe\u6216\u76f4\u65b9\u56fe\u3002\u8fd9\u4e00\u8fc7\u7a0b\u4f7f\u5f97\u7528\u6237\u80fd\u591f\u8f7b\u677e\u5730\u7406\u89e3\u548c\u5206\u6790\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528matplotlib\u3001seaborn\u3001pandas\u5e93\u6765\u5236\u4f5c\u9891\u6570\u5206\u5e03\u56fe\u3002\u8fd9\u4e09\u8005\u5404\u6709\u4f18\u52bf\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1068712,"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\/1068701"}],"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=1068701"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068701\/revisions"}],"predecessor-version":[{"id":1068713,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068701\/revisions\/1068713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068712"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}