{"id":995945,"date":"2024-12-27T09:09:09","date_gmt":"2024-12-27T01:09:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/995945.html"},"modified":"2024-12-27T09:09:12","modified_gmt":"2024-12-27T01:09:12","slug":"python%e7%bb%98%e5%9b%be%e5%90%8e%e5%a6%82%e4%bd%95%e5%81%9c%e7%95%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/995945.html","title":{"rendered":"python\u7ed8\u56fe\u540e\u5982\u4f55\u505c\u7559"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072346\/d82fa3cc-7a75-4556-a56b-d451faa2f372.webp\" alt=\"python\u7ed8\u56fe\u540e\u5982\u4f55\u505c\u7559\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7ed8\u56fe\u540e\u505c\u7559\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528plt.show()\u3001\u8bbe\u7f6e\u963b\u585e\u6a21\u5f0f\u548c\u4f7f\u7528\u4ea4\u4e92\u6a21\u5f0f\uff0c\u5176\u4e2dplt.show()\u6700\u4e3a\u5e38\u7528\u3002<\/strong> <code>plt.show()<\/code> \u662fMatplotlib\u5e93\u4e2d\u7528\u4e8e\u663e\u793a\u56fe\u5f62\u7684\u51fd\u6570\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u540e\uff0c\u7a0b\u5e8f\u4f1a\u6682\u505c\u5e76\u663e\u793a\u7ed8\u5236\u7684\u56fe\u5f62\u7a97\u53e3\uff0c\u76f4\u5230\u7528\u6237\u5173\u95ed\u7a97\u53e3\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PLT.SHOW()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u8fdb\u884c\u7ed8\u56fe\u65f6\uff0c<code>plt.show()<\/code> \u662f\u6700\u4e3a\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u80fd\u591f\u963b\u6b62\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\uff0c\u5e76\u663e\u793a\u56fe\u5f62\u7a97\u53e3\u3002\u7528\u6237\u9700\u8981\u624b\u52a8\u5173\u95ed\u7a97\u53e3\u4ee5\u7ee7\u7eed\u6267\u884c\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u56fe\u4ee3\u7801\u7684\u6700\u540e\u4e00\u884c\u8c03\u7528<code>plt.show()<\/code>\uff0c\u53ef\u4ee5\u786e\u4fdd\u56fe\u5f62\u7a97\u53e3\u5728\u811a\u672c\u6267\u884c\u65f6\u4fdd\u6301\u6253\u5f00\u72b6\u6001\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot([1, 2, 3, 4], [10, 20, 25, 30])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>plt.show()<\/code> \u5c06\u4f1a\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3\uff0c\u663e\u793a\u7ed8\u5236\u7684\u7ebf\u56fe\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u5173\u95ed\u7a97\u53e3\u6765\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u591a\u6b21\u8c03\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u590d\u6742\u7684\u7ed8\u56fe\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u591a\u6b21\u8c03\u7528 <code>plt.show()<\/code> \u6765\u5206\u9636\u6bb5\u663e\u793a\u4e0d\u540c\u7684\u56fe\u5f62\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7b2c\u4e00\u4e2a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.plot([1, 2, 3, 4], [10, 20, 25, 30])<\/p>\n<p>plt.title(&#39;First Plot&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7b2c\u4e8c\u4e2a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.plot([1, 2, 3, 4], [30, 25, 20, 10])<\/p>\n<p>plt.title(&#39;Second Plot&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6bcf\u4e2a <code>plt.show()<\/code> \u5c06\u5206\u522b\u663e\u793a\u4e00\u4e2a\u56fe\u5f62\u7a97\u53e3\uff0c\u7528\u6237\u9700\u8981\u5173\u95ed\u7b2c\u4e00\u4e2a\u7a97\u53e3\u540e\uff0c\u7b2c\u4e8c\u4e2a\u7a97\u53e3\u624d\u4f1a\u51fa\u73b0\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u963b\u585e\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u63a7\u5236\u56fe\u5f62\u7a97\u53e3\u7684\u963b\u585e\u884c\u4e3a\u3002Matplotlib \u63d0\u4f9b\u4e86 <code>plt.show(block=True\/False)<\/code> \u6765\u8bbe\u7f6e\u7a97\u53e3\u7684\u963b\u585e\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u963b\u585e\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>plt.show()<\/code> \u662f\u963b\u585e\u7684\uff0c\u5373 <code>block=True<\/code>\u3002\u8fd9\u610f\u5473\u7740\u8c03\u7528 <code>plt.show()<\/code> \u540e\uff0c\u7a0b\u5e8f\u4f1a\u6682\u505c\uff0c\u76f4\u5230\u7528\u6237\u5173\u95ed\u56fe\u5f62\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show(block=True)  # \u963b\u585e\u6a21\u5f0f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u975e\u963b\u585e\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e <code>block=False<\/code>\uff0c\u53ef\u4ee5\u4f7f <code>plt.show()<\/code> \u975e\u963b\u585e\u3002\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u4ee3\u7801\uff0c\u800c\u4e0d\u7b49\u5f85\u7528\u6237\u5173\u95ed\u56fe\u5f62\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show(block=False)  # \u975e\u963b\u585e\u6a21\u5f0f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u975e\u963b\u585e\u6a21\u5f0f\u4e0b\uff0c\u7528\u6237\u9700\u8981\u624b\u52a8\u8c03\u7528 <code>plt.pause()<\/code> \u6216\u5176\u4ed6\u540c\u6b65\u673a\u5236\u6765\u786e\u4fdd\u56fe\u5f62\u7a97\u53e3\u7684\u663e\u793a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u4ea4\u4e92\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>Matplotlib \u7684\u4ea4\u4e92\u6a21\u5f0f\u5141\u8bb8\u5728\u811a\u672c\u4e2d\u52a8\u6001\u66f4\u65b0\u548c\u663e\u793a\u56fe\u5f62\uff0c\u800c\u65e0\u9700\u591a\u6b21\u8c03\u7528 <code>plt.show()<\/code>\u3002\u4ea4\u4e92\u6a21\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u5b9e\u65f6\u66f4\u65b0\u56fe\u5f62\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u542f\u7528\u4ea4\u4e92\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>plt.ion()<\/code> \u542f\u7528\u4ea4\u4e92\u6a21\u5f0f\u3002\u5728\u6b64\u6a21\u5f0f\u4e0b\uff0c\u7ed8\u56fe\u51fd\u6570\u4f1a\u7acb\u5373\u663e\u793a\u7ed3\u679c\uff0c\u800c\u65e0\u9700\u8c03\u7528 <code>plt.show()<\/code>\u3002<\/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>plt.ion()  # \u542f\u7528\u4ea4\u4e92\u6a21\u5f0f<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<p>for i in range(10):<\/p>\n<p>    plt.plot(x, y * (i+1))<\/p>\n<p>    plt.draw()  # \u7ed8\u5236\u5f53\u524d\u56fe\u5f62<\/p>\n<p>    plt.pause(0.5)  # \u6682\u505c0.5\u79d2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ea4\u4e92\u6a21\u5f0f\u4e0b\uff0c<code>plt.draw()<\/code> \u7528\u4e8e\u66f4\u65b0\u5f53\u524d\u56fe\u5f62\uff0c\u800c <code>plt.pause()<\/code> \u7528\u4e8e\u63a7\u5236\u66f4\u65b0\u9891\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7981\u7528\u4ea4\u4e92\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>plt.ioff()<\/code> \u53ef\u4ee5\u7981\u7528\u4ea4\u4e92\u6a21\u5f0f\uff0c\u6062\u590d\u5230\u9ed8\u8ba4\u7684\u963b\u585e\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.ioff()  # \u7981\u7528\u4ea4\u4e92\u6a21\u5f0f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5176\u4ed6\u4fdd\u6301\u56fe\u5f62\u7a97\u53e3\u663e\u793a\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u6280\u5de7\u53ef\u4ee5\u4fdd\u6301\u56fe\u5f62\u7a97\u53e3\u7684\u663e\u793a\uff0c\u8fd9\u4e9b\u6280\u5de7\u901a\u5e38\u7ed3\u5408\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ed3\u5408GUI\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u57fa\u4e8eGUI\u7684\uff0c\u5982\u4f7f\u7528Tkinter\u6216PyQt\uff0c\u53ef\u4ee5\u901a\u8fc7\u96c6\u6210Matplotlib\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u5c06\u56fe\u5f62\u5d4c\u5165\u5230\u5e94\u7528\u7a0b\u5e8f\u7a97\u53e3\u4e2d\uff0c\u5b9e\u73b0\u66f4\u597d\u7684\u4ea4\u4e92\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u540e\u53f0\u8fd0\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u81ea\u52a8\u5316\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u540e\u53f0\u8fd0\u884c\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7 <code>plt.savefig()<\/code> \u4fdd\u5b58\u56fe\u5f62\uff0c\u7136\u540e\u5728\u9700\u8981\u65f6\u6253\u5f00\u6587\u4ef6\u67e5\u770b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u8bd5\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u6a21\u5f0f\u4e0b\uff0c\u4f7f\u7528\u4ea4\u4e92\u5f0fPython\u89e3\u91ca\u5668\uff08\u5982IPython\u6216Jupyter Notebook\uff09\uff0c\u53ef\u4ee5\u5728\u7ed8\u56fe\u540e\u624b\u52a8\u67e5\u770b\u548c\u8c03\u6574\u56fe\u5f62\uff0c\u800c\u65e0\u9700\u7acb\u5373\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0c<strong>\u5728Python\u4e2d\u7ed8\u56fe\u540e\u4fdd\u6301\u56fe\u5f62\u7a97\u53e3\u663e\u793a\u7684\u65b9\u6cd5\u7075\u6d3b\u591a\u6837\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u5e94\u7528\u573a\u666f\u548c\u7528\u6237\u9700\u6c42<\/strong>\u3002<code>plt.show()<\/code> \u662f\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u9759\u6001\u7ed8\u56fe\u573a\u666f\uff0c\u800c\u4ea4\u4e92\u6a21\u5f0f\u548c\u975e\u963b\u585e\u6a21\u5f0f\u5219\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u66f4\u65b0\u56fe\u5f62\u7684\u5e94\u7528\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u4e3a\u4e30\u5bcc\u548c\u4e13\u4e1a\u7684\u56fe\u5f62\u5c55\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7ed8\u56fe\u540e\u4fdd\u6301\u56fe\u5f62\u7a97\u53e3\u6253\u5f00\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u7684matplotlib\u5e93\u8fdb\u884c\u7ed8\u56fe\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7ed8\u56fe\u7a97\u53e3\u53ef\u80fd\u4f1a\u5728\u7ed8\u56fe\u5b8c\u6210\u540e\u7acb\u5373\u5173\u95ed\u3002\u8981\u4fdd\u6301\u56fe\u5f62\u7a97\u53e3\u6253\u5f00\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.show()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u4f1a\u542f\u52a8\u4e00\u4e2a\u4e8b\u4ef6\u5faa\u73af\uff0c\u76f4\u5230\u7528\u6237\u5173\u95ed\u7a97\u53e3\u3002\u786e\u4fdd\u5728\u7ed8\u56fe\u547d\u4ee4\u4e4b\u540e\u8c03\u7528\u6b64\u51fd\u6570\uff0c\u4ee5\u4fbf\u6b63\u786e\u5730\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5176\u4ed6\u65b9\u6cd5\u6765\u6682\u505cPython\u7ed8\u56fe\uff1f<\/strong><br \/>\u9664\u4e86<code>plt.show()<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u6765\u6682\u505c\u7a0b\u5e8f\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u6309\u4e0b\u56de\u8f66\u952e\u3002\u6bd4\u5982\u5728\u7ed8\u56fe\u4ee3\u7801\u540e\u6dfb\u52a0<code>input(&quot;Press Enter to continue...&quot;)<\/code>\u53ef\u4ee5\u5728\u56fe\u5f62\u663e\u793a\u540e\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u4ece\u800c\u8fbe\u5230\u6682\u505c\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Jupyter Notebook\u4e2d\u4fdd\u6301\u56fe\u5f62\u663e\u793a\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u4f7f\u7528<code>%matplotlib inline<\/code>\u53ef\u4ee5\u4f7f\u56fe\u5f62\u76f4\u63a5\u5d4c\u5165\u5728\u8f93\u51fa\u5355\u5143\u4e2d\uff0c\u800c\u4e0d\u9700\u8981\u989d\u5916\u7684\u7a97\u53e3\u3002\u8fd9\u6837\uff0c\u56fe\u5f62\u4f1a\u5728\u4ee3\u7801\u8fd0\u884c\u540e\u81ea\u52a8\u663e\u793a\uff0c\u5e76\u4e14\u4e0d\u4f1a\u5173\u95ed\u3002\u5982\u679c\u5e0c\u671b\u5728\u5355\u72ec\u7a97\u53e3\u4e2d\u67e5\u770b\u56fe\u5f62\uff0c\u53ef\u4ee5\u4f7f\u7528<code>%matplotlib qt<\/code>\u547d\u4ee4\uff0c\u4e4b\u540e\u4f7f\u7528<code>plt.show()<\/code>\u6765\u4fdd\u6301\u7a97\u53e3\u6253\u5f00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed8\u56fe\u540e\u505c\u7559\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528plt.show()\u3001\u8bbe\u7f6e\u963b\u585e\u6a21\u5f0f\u548c\u4f7f\u7528\u4ea4\u4e92\u6a21\u5f0f\uff0c\u5176\u4e2dplt.show [&hellip;]","protected":false},"author":3,"featured_media":995957,"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\/995945"}],"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=995945"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995945\/revisions"}],"predecessor-version":[{"id":995961,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995945\/revisions\/995961"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/995957"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=995945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=995945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=995945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}