{"id":1028791,"date":"2024-12-31T11:02:38","date_gmt":"2024-12-31T03:02:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1028791.html"},"modified":"2024-12-31T11:02:41","modified_gmt":"2024-12-31T03:02:41","slug":"%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bb%98%e5%88%b6%e8%8c%8e%e5%8f%b6%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1028791.html","title":{"rendered":"\u5728Python\u4e2d\u5982\u4f55\u7ed8\u5236\u830e\u53f6\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/20708e6c-840f-4af5-9aa8-2e3239a41f27.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5728Python\u4e2d\u5982\u4f55\u7ed8\u5236\u830e\u53f6\u56fe\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u7ed8\u5236\u830e\u53f6\u56fe\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u3001\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001\u53ef\u4ee5\u4f7f\u7528stemgraphic\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528stemgraphic\u5e93\u662f\u4e00\u4e2a\u6bd4\u8f83\u7b80\u4fbf\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u8be5\u5e93\u4e13\u95e8\u7528\u4e8e\u7ed8\u5236\u830e\u53f6\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u8bf4\u660e\uff0c\u6211\u4eec\u53ef\u4ee5\u91cd\u70b9\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528stemgraphic\u5e93\u6765\u7ed8\u5236\u830e\u53f6\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528stemgraphic\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install stemgraphic<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import stemgraphic<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u51c6\u5907\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u7ed8\u5236\u830e\u53f6\u56fe\u7684\u7b2c\u4e00\u6b65\u662f\u51c6\u5907\u597d\u6570\u636e\u3002\u6570\u636e\u901a\u5e38\u662f\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [5, 7, 8, 9, 10, 12, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 25, 28, 30]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u830e\u53f6\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528stemgraphic\u5e93\u7ed8\u5236\u830e\u53f6\u56fe\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u8c03\u7528<code>stem_graphic.stem_graphic<\/code>\u65b9\u6cd5\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">stemgraphic.stem_graphic(data)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f1a\u751f\u6210\u4e00\u4e2a\u830e\u53f6\u56fe\uff0c\u5e76\u663e\u793a\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u66f4\u591a\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>stemgraphic\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9009\u9879\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u830e\u53f6\u56fe\u7684\u5916\u89c2\u548c\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6dfb\u52a0\u6807\u9898\u3001\u8c03\u6574\u830e\u548c\u53f6\u7684\u683c\u5f0f\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">stemgraphic.stem_graphic(data, scale=1.0, leaf_order=&#39;ascending&#39;, orientation=&#39;horizontal&#39;, title=&#39;Stem-and-Leaf Plot&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5728Jupyter Notebook\u4e2d\u663e\u793a\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u662f\u5728Jupyter Notebook\u4e2d\u4f7f\u7528\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5d4c\u5165\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">%matplotlib inline<\/p>\n<p>import stemgraphic<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>data = [5, 7, 8, 9, 10, 12, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 25, 28, 30]<\/p>\n<p>stemgraphic.stem_graphic(data)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\u7ed8\u5236\u830e\u53f6\u56fe<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1stemgraphic\u662f\u4e13\u95e8\u7528\u4e8e\u7ed8\u5236\u830e\u53f6\u56fe\u7684\u5e93\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u5982matplotlib\u548cpandas\u6765\u7ed8\u5236\u830e\u53f6\u56fe\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528matplotlib\u548cpandas\u7ed8\u5236\u830e\u53f6\u56fe\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528matplotlib\u7ed8\u5236\u830e\u53f6\u56fe<\/h4>\n<\/p>\n<p><p>\u5c3d\u7ba1matplotlib\u4e3b\u8981\u7528\u4e8e\u7ed8\u5236\u4f20\u7edf\u7684\u56fe\u5f62\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6280\u5de7\u6765\u7ed8\u5236\u830e\u53f6\u56fe\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>data = [5, 7, 8, 9, 10, 12, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 25, 28, 30]<\/p>\n<h2><strong>\u5206\u7ec4\u6570\u636e<\/strong><\/h2>\n<p>stem, leaf = np.divmod(data, 10)<\/p>\n<h2><strong>\u521b\u5efa\u830e\u53f6\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.stem(stem, leaf, basefmt=&quot; &quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_xlabel(&#39;Stem&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Leaf&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528pandas\u7ed8\u5236\u830e\u53f6\u56fe<\/h4>\n<\/p>\n<p><p>pandas\u867d\u7136\u6ca1\u6709\u76f4\u63a5\u7ed8\u5236\u830e\u53f6\u56fe\u7684\u529f\u80fd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5b83\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u6765\u95f4\u63a5\u7ed8\u5236\u830e\u53f6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = [5, 7, 8, 9, 10, 12, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 25, 28, 30]<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data, columns=[&#39;value&#39;])<\/p>\n<h2><strong>\u8ba1\u7b97\u830e\u548c\u53f6<\/strong><\/h2>\n<p>df[&#39;stem&#39;] = df[&#39;value&#39;] \/\/ 10<\/p>\n<p>df[&#39;leaf&#39;] = df[&#39;value&#39;] % 10<\/p>\n<h2><strong>\u5206\u7ec4\u5e76\u5c55\u793a\u830e\u53f6\u56fe<\/strong><\/h2>\n<p>stem_leaf = df.groupby(&#39;stem&#39;)[&#39;leaf&#39;].apply(list)<\/p>\n<p>for stem, leaf in stem_leaf.items():<\/p>\n<p>    print(f&quot;{stem} | {&#39; &#39;.join(map(str, leaf))}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7ed8\u5236\u830e\u53f6\u56fe\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528stemgraphic\u5e93\u3001\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528stemgraphic\u5e93\u662f\u6700\u7b80\u4fbf\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u8be5\u5e93\u4e13\u95e8\u7528\u4e8e\u7ed8\u5236\u830e\u53f6\u56fe\u3002\u901a\u8fc7\u5b66\u4e60\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u5de5\u5177\u6765\u7ed8\u5236\u830e\u53f6\u56fe\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u830e\u53f6\u56fe\u662f\u4ec0\u4e48\uff1f\u5b83\u6709\u4ec0\u4e48\u7528\u5904\uff1f<\/strong><br \/>\u830e\u53f6\u56fe\u662f\u4e00\u79cd\u7528\u4e8e\u5c55\u793a\u6570\u636e\u5206\u5e03\u7684\u56fe\u8868\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u5c0f\u578b\u6570\u636e\u96c6\u3002\u5b83\u901a\u8fc7\u5c06\u6570\u636e\u5206\u6210\u201c\u830e\u201d\uff08\u901a\u5e38\u662f\u6570\u636e\u7684\u9ad8\u4f4d\u6570\u5b57\uff09\u548c\u201c\u53f6\u201d\uff08\u901a\u5e38\u662f\u6570\u636e\u7684\u4f4e\u4f4d\u6570\u5b57\uff09\u6765\u53ef\u89c6\u5316\u6570\u636e\u7684\u5206\u5e03\u3002\u8fd9\u79cd\u56fe\u8868\u80fd\u591f\u4fdd\u7559\u6570\u636e\u7684\u539f\u59cb\u503c\uff0c\u65b9\u4fbf\u7528\u6237\u5feb\u901f\u4e86\u89e3\u6570\u636e\u7684\u6574\u4f53\u8d8b\u52bf\u548c\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u7ed8\u5236\u830e\u53f6\u56fe\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u7ed8\u5236\u830e\u53f6\u56fe\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u548c<code>pandas<\/code>\u5e93\u3002<code>matplotlib<\/code>\u63d0\u4f9b\u4e86\u7ed8\u5236\u56fe\u5f62\u7684\u529f\u80fd\uff0c\u800c<code>pandas<\/code>\u5219\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u548c\u6e05\u6d17\u6570\u636e\u3002\u5728\u5b89\u88c5\u8fd9\u4e9b\u5e93\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5305\u542b\u4e86\u5b83\u4eec\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip install matplotlib pandas<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u830e\u53f6\u56fe\u7684\u793a\u4f8b\u4ee3\u7801\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>matplotlib<\/code>\u548c<code>pandas<\/code>\u7ed8\u5236\u830e\u53f6\u56fe\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\nimport matplotlib.pyplot as plt\n\n# \u793a\u4f8b\u6570\u636e\ndata = [12, 15, 22, 23, 25, 27, 31, 32, 35, 41]\ndf = pd.DataFrame(data, columns=[&#39;Values&#39;])\n\n# \u7ed8\u5236\u830e\u53f6\u56fe\nplt.stem(df[&#39;Values&#39;], linefmt=&#39;b-&#39;, markerfmt=&#39;bo&#39;, basefmt=&#39;r-&#39;)\nplt.title(&#39;Stem-and-Leaf Plot&#39;)\nplt.xlabel(&#39;Stem (Tens)&#39;)\nplt.ylabel(&#39;Leaf (Units)&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u830e\u53f6\u56fe\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u6570\u636e\u96c6\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed8\u5236\u830e\u53f6\u56fe\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u3001\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3001\u53ef\u4ee5\u4f7f\u7528ste [&hellip;]","protected":false},"author":3,"featured_media":1028806,"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\/1028791"}],"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=1028791"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028791\/revisions"}],"predecessor-version":[{"id":1028808,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028791\/revisions\/1028808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1028806"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1028791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1028791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1028791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}