{"id":1128982,"date":"2025-01-08T20:25:27","date_gmt":"2025-01-08T12:25:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128982.html"},"modified":"2025-01-08T20:25:30","modified_gmt":"2025-01-08T12:25:30","slug":"%e7%94%a8python%e7%94%bb%e5%ae%8c%e5%9b%be%e5%90%8e%e5%a6%82%e4%bd%95%e4%b8%8d%e6%b6%88%e5%a4%b1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1128982.html","title":{"rendered":"\u7528Python\u753b\u5b8c\u56fe\u540e\u5982\u4f55\u4e0d\u6d88\u5931"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095546\/590b80ed-b3ca-4bd6-b140-df935f381f4e.webp\" alt=\"\u7528Python\u753b\u5b8c\u56fe\u540e\u5982\u4f55\u4e0d\u6d88\u5931\" \/><\/p>\n<p><p> <strong>\u7528Python\u753b\u5b8c\u56fe\u540e\u5982\u4f55\u4e0d\u6d88\u5931\uff1a\u4f7f\u7528 <code>plt.show()<\/code>\u3001\u907f\u514d\u811a\u672c\u8fd0\u884c\u5b8c\u6210\u540e\u5173\u95ed\u7a97\u53e3\u3001\u4f7f\u7528\u4ea4\u4e92\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7528Python\u7ed8\u5236\u56fe\u5f62\u540e\uff0c\u5f88\u591a\u521d\u5b66\u8005\u4f1a\u9047\u5230\u56fe\u5f62\u7a97\u53e3\u4e00\u95ea\u800c\u8fc7\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>plt.show()<\/code> \u51fd\u6570\u3001\u907f\u514d\u811a\u672c\u8fd0\u884c\u5b8c\u6210\u540e\u7acb\u5373\u5173\u95ed\u7a97\u53e3\u4ee5\u53ca\u5229\u7528\u4ea4\u4e92\u6a21\u5f0f\u7b49\u65b9\u6cd5\u3002<strong><code>plt.show()<\/code> \u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\uff0c\u5b83\u4f1a\u963b\u585e\u811a\u672c\uff0c\u76f4\u5230\u5173\u95ed\u56fe\u5f62\u7a97\u53e3\u4e3a\u6b62\uff0c\u4ece\u800c\u786e\u4fdd\u56fe\u5f62\u7a97\u53e3\u4e0d\u4f1a\u7acb\u5373\u6d88\u5931\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>plt.show()<\/code><\/h3>\n<\/p>\n<p><p><code>plt.show()<\/code> \u662fMatplotlib\u5e93\u4e2d\u6700\u5e38\u7528\u7684\u51fd\u6570\u4e4b\u4e00\uff0c\u7528\u4e8e\u663e\u793a\u56fe\u5f62\u3002\u8c03\u7528 <code>plt.show()<\/code> \u540e\uff0c\u7a0b\u5e8f\u4f1a\u6682\u505c\u5e76\u7b49\u5f85\u7528\u6237\u5173\u95ed\u56fe\u5f62\u7a97\u53e3\uff0c\u8fd9\u6837\u56fe\u5f62\u5c31\u4e0d\u4f1a\u7acb\u5373\u6d88\u5931\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7b80\u5355\u7ed8\u56fe\u793a\u4f8b<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&quot;Simple Plot&quot;)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u5728\u7ed8\u5236\u5b8c\u56fe\u5f62\u540e\u8c03\u7528 <code>plt.show()<\/code>\uff0c\u6b64\u65f6\u7a0b\u5e8f\u4f1a\u6682\u505c\u5e76\u7b49\u5f85\u7528\u6237\u5173\u95ed\u56fe\u5f62\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u907f\u514d\u811a\u672c\u8fd0\u884c\u5b8c\u6210\u540e\u5173\u95ed\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5373\u4f7f\u4f7f\u7528\u4e86 <code>plt.show()<\/code>\uff0c\u56fe\u5f62\u7a97\u53e3\u53ef\u80fd\u4f9d\u7136\u4f1a\u5728\u811a\u672c\u8fd0\u884c\u5b8c\u6210\u540e\u7acb\u5373\u5173\u95ed\u3002\u8fd9\u901a\u5e38\u662f\u56e0\u4e3a\u811a\u672c\u5728\u7ed8\u56fe\u5b8c\u6210\u540e\u6ca1\u6709\u5176\u4ed6\u64cd\u4f5c\uff0c\u5bfc\u81f4Python\u89e3\u91ca\u5668\u9000\u51fa\u65f6\u81ea\u52a8\u5173\u95ed\u6240\u6709\u56fe\u5f62\u7a97\u53e3\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u811a\u672c\u7ed3\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u963b\u585e\u6027\u7684\u8f93\u5165\u64cd\u4f5c\uff0c\u5982 <code>input()<\/code> \u6216 <code>time.sleep()<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528 <code>input()<\/code><\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7b80\u5355\u7ed8\u56fe\u793a\u4f8b<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&quot;Simple Plot&quot;)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<h2><strong>\u963b\u585e\u811a\u672c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>input(&quot;Press Enter to exit...&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528 <code>time.sleep()<\/code><\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import time<\/p>\n<h2><strong>\u7b80\u5355\u7ed8\u56fe\u793a\u4f8b<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&quot;Simple Plot&quot;)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<h2><strong>\u963b\u585e\u811a\u672c\uff0c\u7b49\u5f85\u4e00\u5b9a\u65f6\u95f4<\/strong><\/h2>\n<p>time.sleep(10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u4ea4\u4e92\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>Matplotlib \u63d0\u4f9b\u4e86\u4ea4\u4e92\u6a21\u5f0f\uff08interactive mode\uff09\uff0c\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u56fe\u5f62\u7a97\u53e3\u4f1a\u5728\u751f\u6210\u540e\u7acb\u5373\u663e\u793a\uff0c\u5e76\u4e14\u4e0d\u4f1a\u56e0\u4e3a\u811a\u672c\u8fd0\u884c\u5b8c\u6210\u800c\u5173\u95ed\u3002\u53ef\u4ee5\u901a\u8fc7 <code>plt.ion()<\/code> \u5f00\u542f\u4ea4\u4e92\u6a21\u5f0f\uff0c\u901a\u8fc7 <code>plt.ioff()<\/code> \u5173\u95ed\u4ea4\u4e92\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5f00\u542f\u4ea4\u4e92\u6a21\u5f0f<\/strong><\/h2>\n<p>plt.ion()<\/p>\n<h2><strong>\u7b80\u5355\u7ed8\u56fe\u793a\u4f8b<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&quot;Simple Plot&quot;)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<h2><strong>\u5176\u4ed6\u64cd\u4f5c<\/strong><\/h2>\n<p>print(&quot;Plot should rem<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n open until you manually close it.&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u4ea4\u4e92\u6a21\u5f0f\uff08\u53ef\u9009\uff09<\/strong><\/h2>\n<p>plt.ioff()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Jupyter Notebook<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u662f\u5728Jupyter Notebook\u4e2d\u7ed8\u56fe\uff0c\u56fe\u5f62\u4f1a\u81ea\u52a8\u5d4c\u5165\u5728Notebook\u7684\u8f93\u51fa\u5355\u5143\u4e2d\uff0c\u4e0d\u4f1a\u51fa\u73b0\u7a97\u53e3\u4e00\u95ea\u800c\u8fc7\u7684\u95ee\u9898\u3002\u4f7f\u7528 <code>%matplotlib inline<\/code> \u9b54\u6cd5\u547d\u4ee4\u53ef\u4ee5\u786e\u4fdd\u56fe\u5f62\u6b63\u786e\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u793a\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">%matplotlib inline<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7b80\u5355\u7ed8\u56fe\u793a\u4f8b<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&quot;Simple Plot&quot;)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u7528Python\u7ed8\u5236\u56fe\u5f62\u540e\uff0c\u786e\u4fdd\u56fe\u5f62\u7a97\u53e3\u4e0d\u6d88\u5931\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528 <code>plt.show()<\/code> \u963b\u585e\u811a\u672c<\/strong>\uff0c\u7b49\u5f85\u7528\u6237\u5173\u95ed\u56fe\u5f62\u7a97\u53e3\u3002<\/li>\n<li><strong>\u907f\u514d\u811a\u672c\u8fd0\u884c\u5b8c\u6210\u540e\u7acb\u5373\u5173\u95ed\u7a97\u53e3<\/strong>\uff0c\u53ef\u4ee5\u5728\u811a\u672c\u7ed3\u5c3e\u6dfb\u52a0\u963b\u585e\u6027\u7684\u8f93\u5165\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u4f7f\u7528\u4ea4\u4e92\u6a21\u5f0f<\/strong>\uff0c\u901a\u8fc7 <code>plt.ion()<\/code> \u5f00\u542f\u4ea4\u4e92\u6a21\u5f0f\uff0c\u4f7f\u56fe\u5f62\u7a97\u53e3\u5728\u751f\u6210\u540e\u7acb\u5373\u663e\u793a\uff0c\u5e76\u4e14\u4e0d\u4f1a\u81ea\u52a8\u5173\u95ed\u3002<\/li>\n<li><strong>\u5728Jupyter Notebook\u4e2d\u7ed8\u56fe<\/strong>\uff0c\u4f7f\u7528 <code>%matplotlib inline<\/code> \u9b54\u6cd5\u547d\u4ee4\uff0c\u786e\u4fdd\u56fe\u5f62\u6b63\u786e\u5d4c\u5165\u5728Notebook\u7684\u8f93\u51fa\u5355\u5143\u4e2d\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u7528Python\u7ed8\u5236\u56fe\u5f62\u540e\u7a97\u53e3\u4e00\u95ea\u800c\u8fc7\u7684\u95ee\u9898\uff0c\u4ece\u800c\u66f4\u597d\u5730\u5206\u6790\u548c\u5c55\u793a\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u4f7f\u7528Python\u7ed8\u56fe\u540e\u4fdd\u6301\u56fe\u5f62\u7a97\u53e3\u6253\u5f00\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u50cfMatplotlib\u8fd9\u6837\u7684\u7ed8\u56fe\u5e93\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u7ed8\u5236\u7684\u56fe\u5f62\u7a97\u53e3\u53ef\u80fd\u4f1a\u5728\u7ed8\u5236\u5b8c\u6210\u540e\u7acb\u5373\u5173\u95ed\u3002\u4e3a\u4e86\u4fdd\u6301\u56fe\u5f62\u7a97\u53e3\u6253\u5f00\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.show()<\/code>\u51fd\u6570\uff0c\u5b83\u4f1a\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\u5e76\u4fdd\u6301\u7a97\u53e3\u6253\u5f00\uff0c\u76f4\u5230\u7528\u6237\u624b\u52a8\u5173\u95ed\u3002<\/p>\n<p><strong>\u5728Jupyter Notebook\u4e2d\u5982\u4f55\u663e\u793a\u56fe\u5f62\u800c\u4e0d\u6d88\u5931\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>%matplotlib inline<\/code>\u9b54\u6cd5\u547d\u4ee4\u6765\u786e\u4fdd\u7ed8\u5236\u7684\u56fe\u5f62\u76f4\u63a5\u5d4c\u5165\u5728\u7b14\u8bb0\u672c\u4e2d\uff0c\u800c\u4e0d\u662f\u5355\u72ec\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8ba9\u56fe\u5f62\u5728\u7b14\u8bb0\u672c\u4e2d\u6c38\u4e45\u663e\u793a\uff0c\u907f\u514d\u6d88\u5931\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u679c\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86\u591a\u6b21\u7ed8\u56fe\uff0c\u5982\u4f55\u786e\u4fdd\u6240\u6709\u56fe\u5f62\u90fd\u80fd\u6b63\u5e38\u663e\u793a\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\u65f6\uff0c\u53ef\u4ee5\u5728\u6bcf\u6b21\u8c03\u7528<code>plt.plot()<\/code>\u540e\u4f7f\u7528<code>plt.show()<\/code>\u6765\u663e\u793a\u6bcf\u4e00\u4e2a\u56fe\u5f62\u3002\u5982\u679c\u5e0c\u671b\u5728\u4e00\u4e2a\u7a97\u53e3\u4e2d\u663e\u793a\u591a\u4e2a\u56fe\u5f62\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.figure()<\/code>\u521b\u5efa\u591a\u4e2a\u56fe\u5f62\u5b9e\u4f8b\uff0c\u5e76\u5728\u6bcf\u4e2a\u5b9e\u4f8b\u540e\u8c03\u7528<code>plt.show()<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u6240\u6709\u56fe\u5f62\u90fd\u80fd\u6b63\u5e38\u663e\u793a\uff0c\u800c\u4e0d\u4f1a\u56e0\u4e3a\u8986\u76d6\u800c\u6d88\u5931\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u753b\u5b8c\u56fe\u540e\u5982\u4f55\u4e0d\u6d88\u5931\uff1a\u4f7f\u7528 plt.show()\u3001\u907f\u514d\u811a\u672c\u8fd0\u884c\u5b8c\u6210\u540e\u5173\u95ed\u7a97\u53e3\u3001\u4f7f\u7528\u4ea4\u4e92\u6a21\u5f0f \u5728\u7528P [&hellip;]","protected":false},"author":3,"featured_media":1128991,"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\/1128982"}],"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=1128982"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128982\/revisions"}],"predecessor-version":[{"id":1128996,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128982\/revisions\/1128996"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1128991"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1128982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1128982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1128982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}