{"id":1105840,"date":"2025-01-08T16:34:41","date_gmt":"2025-01-08T08:34:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1105840.html"},"modified":"2025-01-08T16:34:44","modified_gmt":"2025-01-08T08:34:44","slug":"python%e4%bd%9c%e5%9b%be%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aexy%e8%8c%83%e5%9b%b4%e4%b8%80%e8%87%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1105840.html","title":{"rendered":"python\u4f5c\u56fe\u5982\u4f55\u8bbe\u7f6exy\u8303\u56f4\u4e00\u81f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070552\/a99eb2c9-d5bc-413f-9959-7bef4ad4e7ae.webp\" alt=\"python\u4f5c\u56fe\u5982\u4f55\u8bbe\u7f6exy\u8303\u56f4\u4e00\u81f4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bbe\u7f6e\u56fe\u8868\u7684x\u548cy\u8f74\u8303\u56f4\u4e00\u81f4\u3002<\/strong> \u5177\u4f53\u65b9\u6cd5\u662f\u901a\u8fc7\u8bbe\u7f6e\u56fe\u8868\u7684x\u8f74\u548cy\u8f74\u7684\u9650\u5236\uff08limits\uff09\u4f7f\u5f97\u5b83\u4eec\u7684\u8303\u56f4\u76f8\u540c\u3002\u8fd9\u53ef\u4ee5\u786e\u4fdd\u56fe\u8868\u7684\u6bd4\u4f8b\u76f8\u540c\uff0c\u4ece\u800c\u4f7f\u5f97\u6570\u636e\u70b9\u4ee5\u76f8\u540c\u7684\u6bd4\u4f8b\u663e\u793a\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u6765\u8bbe\u7f6e\u56fe\u8868\u7684x\u548cy\u8f74\u8303\u56f4\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><p><strong>\u6b65\u9aa4\u5982\u4e0b\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/strong>\uff1a\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>import matplotlib.pyplot as plt<\/code>\u6765\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u521b\u5efa\u56fe\u8868<\/strong>\uff1a\u4f60\u53ef\u4ee5\u4f7f\u7528<code>plt.figure()<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u8868\u3002<\/li>\n<li><strong>\u7ed8\u5236\u6570\u636e<\/strong>\uff1a\u4f7f\u7528<code>plt.plot()<\/code>\u6216\u5176\u4ed6\u7ed8\u56fe\u51fd\u6570\u6765\u7ed8\u5236\u4f60\u7684\u6570\u636e\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u8f74\u7684\u8303\u56f4<\/strong>\uff1a\u4f7f\u7528<code>plt.xlim()<\/code>\u548c<code>plt.ylim()<\/code>\u6765\u8bbe\u7f6ex\u8f74\u548cy\u8f74\u7684\u8303\u56f4\uff0c\u4f7f\u5f97\u5b83\u4eec\u76f8\u540c\u3002<\/li>\n<\/ol>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u56fe\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u4f60\u7684Python\u811a\u672c\u6216Jupyter Notebook\u4e2d\u5bfc\u5165Matplotlib\u5e93\uff1a<\/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\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.figure()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2aFigure\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd9\u4e2a\u5bf9\u8c61\u4e0a\u8fdb\u884c\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig = plt.figure()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>plt.plot()<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u4f60\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4ee5\u4e0b\u6570\u636e\uff1a<\/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><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u8fd9\u6837\u7ed8\u5236\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8bbe\u7f6e\u8f74\u7684\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fddx\u8f74\u548cy\u8f74\u7684\u8303\u56f4\u4e00\u81f4\uff0c\u4f60\u9700\u8981\u627e\u5230x\u548cy\u6570\u636e\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\uff0c\u7136\u540e\u4f7f\u7528<code>plt.xlim()<\/code>\u548c<code>plt.ylim()<\/code>\u51fd\u6570\u8bbe\u7f6e\u5b83\u4eec\u7684\u8303\u56f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">min_val = min(min(x), min(y))<\/p>\n<p>max_val = max(max(x), max(y))<\/p>\n<p>plt.xlim(min_val, max_val)<\/p>\n<p>plt.ylim(min_val, max_val)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fddx\u8f74\u548cy\u8f74\u7684\u8303\u56f4\u4e00\u81f4\uff0c\u4ece\u800c\u4f7f\u5f97\u56fe\u8868\u7684\u6bd4\u4f8b\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u663e\u793a\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528<code>plt.show()<\/code>\u51fd\u6570\u6765\u663e\u793a\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u7efc\u5408\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<h2><strong>\u7ed8\u5236\u6570\u636e<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8f74\u7684\u8303\u56f4\u4e00\u81f4<\/strong><\/h2>\n<p>min_val = min(min(x), min(y))<\/p>\n<p>max_val = max(max(x), max(y))<\/p>\n<p>plt.xlim(min_val, max_val)<\/p>\n<p>plt.ylim(min_val, max_val)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5176\u4ed6\u76f8\u5173\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u8bbe\u7f6e\u8f74\u7684\u8303\u56f4\u4e00\u81f4\uff0c\u4f60\u8fd8\u53ef\u4ee5\u8fdb\u884c\u5176\u4ed6\u4e00\u4e9b\u8bbe\u7f6e\u6765\u7f8e\u5316\u4f60\u7684\u56fe\u8868\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6dfb\u52a0\u6807\u9898\u3001\u6807\u7b7e\u548c\u7f51\u683c\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.title(&#39;Example Plot with Equal Axis Ranges&#39;)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u4f7f\u4f60\u7684\u56fe\u8868\u66f4\u52a0\u6613\u8bfb\u548c\u4e13\u4e1a\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u8bbe\u7f6e\u56fe\u8868\u7684x\u8f74\u548cy\u8f74\u8303\u56f4\u4e00\u81f4\u3002\u8fd9\u4e0d\u4ec5\u53ef\u4ee5\u786e\u4fdd\u56fe\u8868\u7684\u6bd4\u4f8b\u76f8\u540c\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u6570\u636e\u70b9\u4ee5\u76f8\u540c\u7684\u6bd4\u4f8b\u663e\u793a\uff0c\u4ece\u800c\u4f7f\u5f97\u56fe\u8868\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u8bfb\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u5728Python\u4f5c\u56fe\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4f5c\u56fe\u4e2d\u8bbe\u7f6ex\u8f74\u548cy\u8f74\u7684\u8303\u56f4\u76f8\u540c\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u5e93\u8fdb\u884c\u4f5c\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4\u6765\u786e\u4fddx\u8f74\u548cy\u8f74\u7684\u6bd4\u4f8b\u4e00\u81f4\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>plt.axis(&#39;equal&#39;)<\/code>\u6216\u5206\u522b\u8bbe\u7f6ex\u8f74\u548cy\u8f74\u7684\u8303\u56f4\uff0c\u4f8b\u5982<code>plt.xlim(min_x, max_x)<\/code>\u548c<code>plt.ylim(min_y, max_y)<\/code>\uff0c\u4ee5\u786e\u4fdd\u4e24\u8005\u7684\u8303\u56f4\u76f8\u540c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u54ea\u4e2a\u5e93\u6700\u9002\u5408\u8fdb\u884c\u56fe\u5f62\u7ed8\u5236\u5e76\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4\uff1f<\/strong><br \/>Matplotlib\u662f\u6700\u5e38\u7528\u7684Python\u7ed8\u56fe\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u7684\u53c2\u6570\u8bbe\u7f6e\uff0c\u9002\u5408\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\u7ed8\u5236\u3002\u901a\u8fc7Matplotlib\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4\u3001\u6bd4\u4f8b\u548c\u5176\u4ed6\u56fe\u5f62\u5c5e\u6027\u3002\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u53ef\u89c6\u5316\uff0cSeaborn\u548cPlotly\u4e5f\u662f\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4\u65f6\uff0c\u5982\u4f55\u907f\u514d\u56fe\u5f62\u53d8\u5f62\uff1f<\/strong><br \/>\u4e3a\u4e86\u907f\u514d\u56fe\u5f62\u53d8\u5f62\uff0c\u786e\u4fddx\u8f74\u548cy\u8f74\u7684\u6bd4\u4f8b\u4e00\u81f4\u81f3\u5173\u91cd\u8981\u3002\u4f7f\u7528<code>plt.axis(&#39;equal&#39;)<\/code>\u53ef\u4ee5\u4fdd\u6301\u56fe\u5f62\u7684\u6bd4\u4f8b\u4e0d\u53d8\u3002\u6b64\u5916\uff0c\u5728\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4\u65f6\uff0c\u5efa\u8bae\u6839\u636e\u6570\u636e\u7684\u5b9e\u9645\u8303\u56f4\u8fdb\u884c\u8c03\u6574\uff0c\u786e\u4fdd\u56fe\u5f62\u6e05\u6670\u6613\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bbe\u7f6e\u56fe\u8868\u7684x\u548cy\u8f74\u8303\u56f4\u4e00\u81f4\u3002 \u5177\u4f53\u65b9\u6cd5\u662f\u901a\u8fc7\u8bbe\u7f6e\u56fe\u8868\u7684x\u8f74 [&hellip;]","protected":false},"author":3,"featured_media":1105850,"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\/1105840"}],"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=1105840"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105840\/revisions"}],"predecessor-version":[{"id":1105852,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105840\/revisions\/1105852"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1105850"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1105840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1105840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1105840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}