{"id":1042783,"date":"2024-12-31T13:00:19","date_gmt":"2024-12-31T05:00:19","guid":{"rendered":""},"modified":"2024-12-31T13:00:22","modified_gmt":"2024-12-31T05:00:22","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%a8%aa%e7%ba%b5%e5%9d%90%e6%a0%87%e8%8c%83%e5%9b%b4%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1042783.html","title":{"rendered":"\u5982\u4f55\u7528python\u6a2a\u7eb5\u5750\u6807\u8303\u56f4\u8bbe\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/44c4173d-f800-42d6-bfd9-cd01da756286.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u6a2a\u7eb5\u5750\u6807\u8303\u56f4\u8bbe\u7f6e\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528python\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u8303\u56f4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u3001\u8bbe\u7f6e<code>xlim<\/code>\u548c<code>ylim<\/code>\u65b9\u6cd5\u3001\u81ea\u5b9a\u4e49\u5750\u6807\u8f74\u8303\u56f4\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u662f\u6700\u5e38\u89c1\u548c\u4fbf\u6377\u7684\u65b9\u6cd5\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u56fe\u8868\u548c\u6570\u636e\u53ef\u89c6\u5316\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u6765\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u7684\u8303\u56f4\uff0c\u5e76\u5c55\u793a\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5f15\u5165 <code>matplotlib<\/code> \u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u8303\u56f4\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u5e76\u5f15\u5165\u4e86<code>matplotlib<\/code>\u5e93\u3002\u8be5\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\u4e4b\u4e00\uff0c\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u5404\u79cd\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u57fa\u672c\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u8303\u56f4\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u56fe\u8868\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u7ed8\u5236\u4e86\u4e00\u6761\u7ebf\u6027\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u7f6e\u6a2a\u5750\u6807\u8303\u56f4 (<code>xlim<\/code> \u65b9\u6cd5)<\/h3>\n<\/p>\n<p><p><code>xlim<\/code> \u65b9\u6cd5\u7528\u6765\u8bbe\u7f6e\u6a2a\u5750\u6807\u7684\u8303\u56f4\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5143\u7ec4\u6765\u6307\u5b9a\u6a2a\u5750\u6807\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y)<\/p>\n<p>plt.xlim(0, 6)  # \u8bbe\u7f6e\u6a2a\u5750\u6807\u8303\u56f4\u4ece0\u52306<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u770b\u5230\uff0c\u901a\u8fc7\u8bbe\u7f6e<code>xlim(0, 6)<\/code>\uff0c\u6211\u4eec\u5c06\u6a2a\u5750\u6807\u8303\u56f4\u8bbe\u5b9a\u4e3a\u4ece0\u52306\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8bbe\u7f6e\u7eb5\u5750\u6807\u8303\u56f4 (<code>ylim<\/code> \u65b9\u6cd5)<\/h3>\n<\/p>\n<p><p><code>ylim<\/code> \u65b9\u6cd5\u7528\u6765\u8bbe\u7f6e\u7eb5\u5750\u6807\u7684\u8303\u56f4\u3002\u4e0e<code>xlim<\/code>\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5143\u7ec4\u6765\u6307\u5b9a\u7eb5\u5750\u6807\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y)<\/p>\n<p>plt.ylim(0, 30)  # \u8bbe\u7f6e\u7eb5\u5750\u6807\u8303\u56f4\u4ece0\u523030<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e<code>ylim(0, 30)<\/code>\uff0c\u6211\u4eec\u5c06\u7eb5\u5750\u6807\u8303\u56f4\u8bbe\u5b9a\u4e3a\u4ece0\u523030\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u540c\u65f6\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u540c\u65f6\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u7684\u8303\u56f4\u3002\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528<code>xlim<\/code>\u548c<code>ylim<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y)<\/p>\n<p>plt.xlim(0, 6)  # \u8bbe\u7f6e\u6a2a\u5750\u6807\u8303\u56f4<\/p>\n<p>plt.ylim(0, 30)  # \u8bbe\u7f6e\u7eb5\u5750\u6807\u8303\u56f4<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u81ea\u52a8\u8c03\u6574\u5750\u6807\u8f74\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u5e0c\u671b\u5750\u6807\u8f74\u8303\u56f4\u6839\u636e\u6570\u636e\u81ea\u52a8\u8c03\u6574\u3002<code>matplotlib<\/code>\u63d0\u4f9b\u4e86<code>autoscale<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y)<\/p>\n<p>plt.autoscale()  # \u81ea\u52a8\u8c03\u6574\u5750\u6807\u8f74\u8303\u56f4<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u81ea\u5b9a\u4e49\u5750\u6807\u8f74\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u66f4\u7075\u6d3b\u5730\u81ea\u5b9a\u4e49\u5750\u6807\u8f74\u8303\u56f4\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u7684\u903b\u8f91\u52a8\u6001\u8bbe\u7f6e\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_xlim(data):<\/p>\n<p>    return (min(data) - 1, max(data) + 1)<\/p>\n<p>def custom_ylim(data):<\/p>\n<p>    return (min(data) - 5, max(data) + 5)<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlim(custom_xlim(x))<\/p>\n<p>plt.ylim(custom_ylim(y))<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570<code>custom_xlim<\/code>\u548c<code>custom_ylim<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u52a8\u6001\u5730\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u7684\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u6765\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u7684\u8303\u56f4\uff0c\u5e76\u7ed3\u5408\u524d\u9762\u7684\u6240\u6709\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>def custom_xlim(data):<\/p>\n<p>    return (min(data) - 1, max(data) + 1)<\/p>\n<p>def custom_ylim(data):<\/p>\n<p>    return (min(data) - 5, max(data) + 5)<\/p>\n<p>plt.plot(x, y, label=&#39;Data Line&#39;)<\/p>\n<p>plt.xlabel(&#39;X Axis&#39;)  # \u8bbe\u7f6e\u6a2a\u5750\u6807\u6807\u7b7e<\/p>\n<p>plt.ylabel(&#39;Y Axis&#39;)  # \u8bbe\u7f6e\u7eb5\u5750\u6807\u6807\u7b7e<\/p>\n<p>plt.title(&#39;Customizing Axis Limits&#39;)  # \u8bbe\u7f6e\u56fe\u8868\u6807\u9898<\/p>\n<p>plt.legend()  # \u663e\u793a\u56fe\u4f8b<\/p>\n<p>plt.xlim(custom_xlim(x))  # \u81ea\u5b9a\u4e49\u6a2a\u5750\u6807\u8303\u56f4<\/p>\n<p>plt.ylim(custom_ylim(y))  # \u81ea\u5b9a\u4e49\u7eb5\u5750\u6807\u8303\u56f4<\/p>\n<p>plt.grid(True)  # \u663e\u793a\u7f51\u683c<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u6211\u4eec\u4e0d\u4ec5\u5c55\u793a\u4e86\u5982\u4f55\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u7684\u8303\u56f4\uff0c\u8fd8\u6dfb\u52a0\u4e86\u56fe\u8868\u6807\u9898\u3001\u5750\u6807\u8f74\u6807\u7b7e\u3001\u56fe\u4f8b\u548c\u7f51\u683c\uff0c\u4f7f\u56fe\u8868\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u8303\u56f4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>xlim<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u6a2a\u5750\u6807\u8303\u56f4\u3001\u4f7f\u7528<code>ylim<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u7eb5\u5750\u6807\u8303\u56f4\u3001\u4f7f\u7528<code>autoscale<\/code>\u65b9\u6cd5\u81ea\u52a8\u8c03\u6574\u5750\u6807\u8f74\u8303\u56f4\u3001\u81ea\u5b9a\u4e49\u5750\u6807\u8f74\u8303\u56f4\u3002<\/strong> \u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4\uff0c\u4ece\u800c\u4f7f\u56fe\u8868\u66f4\u52a0\u6e05\u6670\u548c\u4e13\u4e1a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u56fe\u8868\u7684\u5750\u6807\u8303\u56f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bbe\u7f6e\u56fe\u8868\u7684\u6a2a\u7eb5\u5750\u6807\u8303\u56f4\u3002\u901a\u8fc7\u8c03\u7528<code>plt.xlim()<\/code>\u548c<code>plt.ylim()<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5206\u522b\u6307\u5b9ax\u8f74\u548cy\u8f74\u7684\u8303\u56f4\u3002\u4f8b\u5982\uff0c<code>plt.xlim(0, 10)<\/code>\u5c06x\u8f74\u8303\u56f4\u8bbe\u7f6e\u4e3a0\u523010\uff0c\u800c<code>plt.ylim(-5, 5)<\/code>\u4f1a\u5c06y\u8f74\u8303\u56f4\u8bbe\u7f6e\u4e3a-5\u52305\u3002\u786e\u4fdd\u5728\u7ed8\u5236\u56fe\u5f62\u4e4b\u524d\u8bbe\u7f6e\u8fd9\u4e9b\u8303\u56f4\uff0c\u4ee5\u4fbf\u5b83\u4eec\u80fd\u6b63\u786e\u5e94\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528NumPy\u548cMatplotlib\u7ed8\u5236\u7279\u5b9a\u8303\u56f4\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u5728\u4f7f\u7528NumPy\u751f\u6210\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408Matplotlib\u6765\u7ed8\u5236\u7279\u5b9a\u8303\u56f4\u7684\u56fe\u5f62\u3002\u4f7f\u7528<code>numpy.linspace()<\/code>\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u5747\u5300\u5206\u5e03\u7684\u6570\u636e\u70b9\uff0c\u63a5\u7740\u901a\u8fc7<code>plt.plot()<\/code>\u7ed8\u5236\u8fd9\u4e9b\u6570\u636e\u3002\u8bbe\u7f6e\u5750\u6807\u8303\u56f4\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528<code>plt.xlim()<\/code>\u548c<code>plt.ylim()<\/code>\u6765\u63a7\u5236\u663e\u793a\u7684\u533a\u57df\uff0c\u4f7f\u56fe\u5f62\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff0c\u5f53\u8bbe\u7f6e\u5750\u6807\u8303\u56f4\u65f6\uff1f<\/strong><br \/>\u5728\u8bbe\u7f6e\u5750\u6807\u8303\u56f4\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u5c06\u8303\u56f4\u8bbe\u7f6e\u5f97\u8fc7\u4e8e\u72ed\u7a84\uff0c\u5bfc\u81f4\u6570\u636e\u70b9\u65e0\u6cd5\u663e\u793a\u3002\u786e\u4fdd\u6839\u636e\u6570\u636e\u7684\u5b9e\u9645\u8303\u56f4\u8fdb\u884c\u5408\u7406\u8bbe\u7f6e\u3002\u53e6\u5916\uff0c\u5fd8\u8bb0\u66f4\u65b0\u5750\u6807\u8303\u56f4\u4f1a\u4f7f\u65b0\u7684\u6570\u636e\u70b9\u88ab\u5ffd\u7565\uff0c\u56e0\u6b64\u5728\u66f4\u65b0\u56fe\u5f62\u65f6\uff0c\u53ca\u65f6\u8c03\u6574\u5750\u6807\u8303\u56f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8fd8\u9700\u6ce8\u610f\uff0c\u8bbe\u7f6e\u8303\u56f4\u65f6\u5e94\u8003\u8651\u6570\u636e\u7684\u6781\u503c\uff0c\u4ee5\u514d\u6570\u636e\u88ab\u622a\u65ad\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528python\u8bbe\u7f6e\u6a2a\u7eb5\u5750\u6807\u8303\u56f4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u8bbe\u7f6exlim\u548cylim\u65b9\u6cd5\u3001\u81ea\u5b9a\u4e49\u5750\u6807 [&hellip;]","protected":false},"author":3,"featured_media":1042788,"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\/1042783"}],"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=1042783"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042783\/revisions"}],"predecessor-version":[{"id":1042793,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1042783\/revisions\/1042793"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1042788"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1042783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1042783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1042783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}