{"id":1122614,"date":"2025-01-08T19:22:51","date_gmt":"2025-01-08T11:22:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1122614.html"},"modified":"2025-01-08T19:22:53","modified_gmt":"2025-01-08T11:22:53","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e6%95%b4%e6%9f%b1%e7%8a%b6%e5%9b%be%e6%a8%aa%e5%9d%90%e6%a0%87%e8%8c%83%e5%9b%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1122614.html","title":{"rendered":"python\u5982\u4f55\u8c03\u6574\u67f1\u72b6\u56fe\u6a2a\u5750\u6807\u8303\u56f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084422\/db80c4ea-0102-4317-abb6-477ccdc5b0ac.webp\" alt=\"python\u5982\u4f55\u8c03\u6574\u67f1\u72b6\u56fe\u6a2a\u5750\u6807\u8303\u56f4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8c03\u6574\u67f1\u72b6\u56fe\u7684\u6a2a\u5750\u6807\u8303\u56f4\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6ex\u8f74\u7684\u9650\u5236\u3001\u8c03\u6574\u6570\u636e\u8303\u56f4\u3001\u4f7f\u7528\u9002\u5f53\u7684\u523b\u5ea6\u6807\u7b7e\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/strong> \u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\uff0c\u4f7f\u56fe\u8868\u66f4\u52a0\u6e05\u6670\u6613\u8bfb\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u8bbe\u7f6ex\u8f74\u7684\u9650\u5236<\/p>\n<\/p>\n<p><p>\u8bbe\u7f6ex\u8f74\u7684\u9650\u5236\u662f\u8c03\u6574\u67f1\u72b6\u56fe\u6a2a\u5750\u6807\u8303\u56f4\u7684\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>xlim<\/code>\u51fd\u6570\u6765\u8bbe\u7f6ex\u8f74\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002\u8fd9\u4f7f\u4f60\u80fd\u591f\u7cbe\u786e\u63a7\u5236\u56fe\u8868\u7684\u663e\u793a\u8303\u56f4\u3002<\/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>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>values = [10, 20, 30, 40, 50]<\/p>\n<p>plt.bar(categories, values)<\/p>\n<p>plt.xlim(-0.5, 4.5)  # \u8bbe\u7f6ex\u8f74\u8303\u56f4<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>plt.xlim(-0.5, 4.5)<\/code>\u8bbe\u7f6e\u4e86x\u8f74\u7684\u8303\u56f4\uff0c\u4f7f\u5f97\u56fe\u8868\u4ec5\u663e\u793a\u4ece-0.5\u52304.5\u7684\u90e8\u5206\u3002\u8fd9\u80fd\u591f\u6709\u6548\u9632\u6b62\u56fe\u8868\u663e\u793a\u591a\u4f59\u7684\u7a7a\u767d\u533a\u57df\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8c03\u6574\u6570\u636e\u8303\u56f4<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u53ea\u60f3\u5c55\u793a\u7279\u5b9a\u8303\u56f4\u5185\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u9009\u62e9\u6027\u5730\u8c03\u6574\u6570\u636e\u8303\u56f4\u3002\u901a\u8fc7\u622a\u53d6\u6570\u636e\u6570\u7ec4\uff0c\u6765\u5c55\u793a\u4f60\u6240\u5173\u6ce8\u7684\u6570\u636e\u90e8\u5206\u3002<\/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>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>values = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u53ea\u5c55\u793a\u90e8\u5206\u6570\u636e<\/strong><\/h2>\n<p>categories = categories[1:4]<\/p>\n<p>values = values[1:4]<\/p>\n<p>plt.bar(categories, values)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u622a\u53d6\u4e86<code>categories<\/code>\u548c<code>values<\/code>\u6570\u7ec4\u7684\u90e8\u5206\u6570\u636e\uff0c\u4ec5\u5c55\u793aB\u5230D\u7684\u503c\u3002\u8fd9\u6837\u80fd\u591f\u96c6\u4e2d\u5c55\u793a\u91cd\u8981\u7684\u6570\u636e\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u9002\u5f53\u7684\u523b\u5ea6\u6807\u7b7e<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u8c03\u6574\u67f1\u72b6\u56fe\u6a2a\u5750\u6807\u8303\u56f4\u5e76\u4e0d\u9700\u8981\u6539\u53d8\u6570\u636e\u672c\u8eab\u6216x\u8f74\u9650\u5236\uff0c\u53ea\u9700\u8981\u8c03\u6574\u523b\u5ea6\u6807\u7b7e\u5373\u53ef\u3002\u901a\u8fc7\u8bbe\u7f6e\u523b\u5ea6\u6807\u7b7e\uff0c\u80fd\u591f\u4f7f\u56fe\u8868\u66f4\u52a0\u6613\u8bfb\u3002<\/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>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>values = [10, 20, 30, 40, 50]<\/p>\n<p>plt.bar(categories, values)<\/p>\n<h2><strong>\u8bbe\u7f6e\u523b\u5ea6\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xticks([0, 2, 4], [&#39;A&#39;, &#39;C&#39;, &#39;E&#39;])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>plt.xticks([0, 2, 4], [&#39;A&#39;, &#39;C&#39;, &#39;E&#39;])<\/code>\u8bbe\u7f6e\u4e86x\u8f74\u7684\u523b\u5ea6\u6807\u7b7e\uff0c\u4f7f\u5f97\u56fe\u8868\u53ea\u663e\u793aA\u3001C\u3001E\u7684\u523b\u5ea6\u3002\u8fd9\u6709\u52a9\u4e8e\u51cf\u5c11\u6742\u4e71\u7684\u523b\u5ea6\u6807\u7b7e\uff0c\u4f7f\u6570\u636e\u5c55\u793a\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7efc\u5408\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u7efc\u5408\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u6765\u8c03\u6574\u67f1\u72b6\u56fe\u7684\u6a2a\u5750\u6807\u8303\u56f4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u7684\u4f8b\u5b50\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>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;]<\/p>\n<p>values = [10, 20, 30, 40, 50, 60, 70]<\/p>\n<h2><strong>\u53ea\u5c55\u793a\u90e8\u5206\u6570\u636e<\/strong><\/h2>\n<p>categories_to_display = categories[2:6]<\/p>\n<p>values_to_display = values[2:6]<\/p>\n<p>plt.bar(categories_to_display, values_to_display)<\/p>\n<p>plt.xlim(1, 5)  # \u8bbe\u7f6ex\u8f74\u8303\u56f4<\/p>\n<p>plt.xticks([1, 2, 3, 4], [&#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;])  # \u8bbe\u7f6e\u523b\u5ea6\u6807\u7b7e<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7efc\u5408\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u540c\u65f6\u8c03\u6574\u4e86\u6570\u636e\u8303\u56f4\u3001x\u8f74\u9650\u5236\u548c\u523b\u5ea6\u6807\u7b7e\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u56fe\u8868\u663e\u793a\u6548\u679c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u80fd\u591f\u7075\u6d3b\u5730\u63a7\u5236\u67f1\u72b6\u56fe\u7684\u6a2a\u5750\u6807\u8303\u56f4\uff0c\u4f7f\u6570\u636e\u5c55\u793a\u66f4\u52a0\u4e13\u4e1a\u3001\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6ex\u8f74\u7684\u9650\u5236\u3001\u8c03\u6574\u6570\u636e\u8303\u56f4\u3001\u4f7f\u7528\u9002\u5f53\u7684\u523b\u5ea6\u6807\u7b7e\uff0c\u80fd\u591f\u7075\u6d3b\u5730\u8c03\u6574Python\u67f1\u72b6\u56fe\u7684\u6a2a\u5750\u6807\u8303\u56f4\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5355\u72ec\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u6548\u679c\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u5728\u6570\u636e\u53ef\u89c6\u5316\u8fc7\u7a0b\u4e2d\u66f4\u597d\u5730\u8c03\u6574\u67f1\u72b6\u56fe\u7684\u6a2a\u5750\u6807\u8303\u56f4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u67f1\u72b6\u56fe\u7684\u6a2a\u5750\u6807\u8303\u56f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bbe\u7f6e\u67f1\u72b6\u56fe\u7684\u6a2a\u5750\u6807\u8303\u56f4\u3002\u53ef\u4ee5\u901a\u8fc7<code>plt.xlim()<\/code>\u51fd\u6570\u6765\u6307\u5b9a\u6a2a\u5750\u6807\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>plt.xlim(0, 10)<\/code>\u53ef\u4ee5\u5c06\u6a2a\u5750\u6807\u9650\u5236\u57280\u523010\u4e4b\u95f4\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5728\u7ed8\u5236\u67f1\u72b6\u56fe\u4e4b\u524d\u8bbe\u7f6e\u6a2a\u5750\u6807\u8303\u56f4\uff0c\u4ee5\u4fbf\u5b83\u80fd\u6b63\u786e\u53cd\u6620\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u8c03\u6574\u67f1\u72b6\u56fe\u6a2a\u5750\u6807\u8303\u56f4\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u5728\u7ed8\u56fe\u540e\u624d\u8bbe\u7f6e\u6a2a\u5750\u6807\u8303\u56f4\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u8303\u56f4\u8bbe\u7f6e\u65e0\u6548\u3002\u53e6\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u662f\u672a\u6b63\u786e\u8c03\u6574\u6570\u636e\u96c6\u7684\u8303\u56f4\uff0c\u4f7f\u5f97\u6a2a\u5750\u6807\u663e\u793a\u4e0d\u5b8c\u6574\u6216\u4e0d\u5408\u7406\u3002\u786e\u4fdd\u5728\u7ed8\u5236\u67f1\u72b6\u56fe\u4e4b\u524d\u68c0\u67e5\u6570\u636e\uff0c\u5e76\u5728\u7ed8\u56fe\u524d\u8bbe\u7f6e\u6a2a\u5750\u6807\u8303\u56f4\uff0c\u53ef\u4ee5\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<p><strong>\u4f7f\u7528Seaborn\u5e93\u7ed8\u5236\u67f1\u72b6\u56fe\u65f6\uff0c\u5982\u4f55\u8c03\u6574\u6a2a\u5750\u6807\u8303\u56f4\uff1f<\/strong><br \/>Seaborn\u5e93\u4e5f\u80fd\u65b9\u4fbf\u5730\u8c03\u6574\u67f1\u72b6\u56fe\u7684\u6a2a\u5750\u6807\u8303\u56f4\u3002\u53ef\u4ee5\u5728\u7ed8\u5236\u5b8c\u6210\u540e\u4f7f\u7528<code>plt.xlim()<\/code>\u51fd\u6570\u8fdb\u884c\u8bbe\u7f6e\u3002\u5982\u679c\u60f3\u5728\u7ed8\u5236\u65f6\u76f4\u63a5\u63a7\u5236\uff0c\u53ef\u4ee5\u5229\u7528<code>ax.set_xlim()<\/code>\u65b9\u6cd5\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u7684\u5b50\u56fe\u3002\u6b64\u5916\uff0cSeaborn\u8fd8\u63d0\u4f9b\u4e86\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u548c\u989c\u8272\u9009\u62e9\uff0c\u9002\u5408\u5feb\u901f\u6784\u5efa\u9ad8\u8d28\u91cf\u7684\u56fe\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u6574\u67f1\u72b6\u56fe\u7684\u6a2a\u5750\u6807\u8303\u56f4\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6ex\u8f74\u7684\u9650\u5236\u3001\u8c03\u6574\u6570\u636e\u8303\u56f4\u3001\u4f7f\u7528\u9002\u5f53\u7684\u523b\u5ea6\u6807\u7b7e\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0 [&hellip;]","protected":false},"author":3,"featured_media":1122621,"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\/1122614"}],"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=1122614"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122614\/revisions"}],"predecessor-version":[{"id":1122622,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122614\/revisions\/1122622"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1122621"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1122614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1122614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1122614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}