{"id":1154154,"date":"2025-01-13T17:47:47","date_gmt":"2025-01-13T09:47:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154154.html"},"modified":"2025-01-13T17:47:49","modified_gmt":"2025-01-13T09:47:49","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9%e7%94%bb%e5%9b%be%e5%81%9c%e7%95%99python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1154154.html","title":{"rendered":"\u5982\u4f55\u8ba9\u753b\u56fe\u505c\u7559python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25183825\/f598a571-174f-43da-bac2-695d63a370c5.webp\" alt=\"\u5982\u4f55\u8ba9\u753b\u56fe\u505c\u7559python\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7\u8c03\u6574\u7ed8\u56fe\u7a97\u53e3\u7684\u663e\u793a\u65f6\u95f4\u3001\u4f7f\u7528\u963b\u585e\u51fd\u6570\u3001\u624b\u52a8\u63a7\u5236\u7a97\u53e3\u5173\u95ed\u3001\u5728\u7ed8\u56fe\u4e2d\u52a0\u5165\u4ea4\u4e92\u529f\u80fd\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9Python\u7ed8\u56fe\u505c\u7559\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u963b\u585e\u51fd\u6570\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u8c03\u7528<code>plt.show(block=True)<\/code>\uff0c\u53ef\u4ee5\u5f3a\u5236\u7a0b\u5e8f\u5728\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\u65f6\u6682\u505c\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u624b\u52a8\u5173\u95ed\u7a97\u53e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u884c\uff0c\u9002\u7528\u4e8e\u5927\u90e8\u5206\u7ed8\u56fe\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u963b\u585e\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5728Python\u7684Matplotlib\u5e93\u4e2d\uff0c<code>plt.show()<\/code>\u51fd\u6570\u7528\u4e8e\u663e\u793a\u56fe\u5f62\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u975e\u963b\u585e\u7684\uff0c\u5373\u51fd\u6570\u6267\u884c\u540e\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u8fd0\u884c\u3002\u800c\u901a\u8fc7\u8bbe\u7f6e<code>block=True<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u8ba9<code>plt.show()<\/code>\u53d8\u4e3a\u963b\u585e\u6a21\u5f0f\uff0c\u7a0b\u5e8f\u4f1a\u6682\u505c\u6267\u884c\u76f4\u5230\u7a97\u53e3\u88ab\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62\u5e76\u963b\u585e\u7a0b\u5e8f\u6267\u884c<\/strong><\/h2>\n<p>plt.show(block=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u7a0b\u5e8f\u4f1a\u5728\u663e\u793a\u56fe\u5f62\u7a97\u53e3\u65f6\u6682\u505c\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u624b\u52a8\u63a7\u5236\u7a97\u53e3\u5173\u95ed<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u624b\u52a8\u63a7\u5236\u7a97\u53e3\u7684\u5173\u95ed\u3002\u901a\u8fc7Matplotlib\u7684<code>pyplot<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u56fe\u5f62\u7a97\u53e3\u7684\u53e5\u67c4\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u8bbe\u7f6e\u5173\u95ed\u7a97\u53e3\u7684\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u56fe\u5f62\u7a97\u53e3\u7684\u53e5\u67c4<\/strong><\/h2>\n<p>fig = plt.gcf()<\/p>\n<p>fig.canvas.manager.window.protocol(&quot;WM_DELETE_WINDOW&quot;, lambda: print(&quot;\u7a97\u53e3\u5173\u95ed\u4e8b\u4ef6&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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u7a97\u53e3\u5173\u95ed\u4e8b\u4ef6\uff0c\u5f53\u7528\u6237\u5173\u95ed\u7a97\u53e3\u65f6\uff0c\u4f1a\u6253\u5370\u4e00\u6761\u6d88\u606f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5173\u95ed\u7a97\u53e3\u65f6\u6267\u884c\u4e00\u4e9b\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5728\u7ed8\u56fe\u4e2d\u52a0\u5165\u4ea4\u4e92\u529f\u80fd<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u7ed8\u56fe\u7a97\u53e3\u66f4\u5177\u4ea4\u4e92\u6027\uff0c\u53ef\u4ee5\u5728\u56fe\u5f62\u4e2d\u52a0\u5165\u5404\u79cd\u4ea4\u4e92\u529f\u80fd\uff0c\u4f8b\u5982\u6309\u94ae\u3001\u6ed1\u5757\u7b49\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7Matplotlib\u7684<code>widgets<\/code>\u6a21\u5757\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from matplotlib.widgets import Button<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot(x, y)<\/p>\n<h2><strong>\u5b9a\u4e49\u6309\u94ae\u7684\u56de\u8c03\u51fd\u6570<\/strong><\/h2>\n<p>def on_button_click(event):<\/p>\n<p>    print(&quot;\u6309\u94ae\u88ab\u70b9\u51fb&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae<\/strong><\/h2>\n<p>button_ax = plt.axes([0.8, 0.05, 0.1, 0.075])<\/p>\n<p>button = Button(button_ax, &#39;\u70b9\u51fb\u6211&#39;)<\/p>\n<p>button.on_clicked(on_button_click)<\/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\u6211\u4eec\u5728\u56fe\u5f62\u4e2d\u52a0\u5165\u4e86\u4e00\u4e2a\u6309\u94ae\uff0c\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u65f6\uff0c\u4f1a\u6267\u884c\u56de\u8c03\u51fd\u6570<code>on_button_click<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u7ed8\u56fe\u7a97\u53e3\u5177\u5907\u66f4\u591a\u7684\u4ea4\u4e92\u529f\u80fd\uff0c\u4ece\u800c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u8c03\u6574\u7ed8\u56fe\u7a97\u53e3\u7684\u663e\u793a\u65f6\u95f4<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u7ed8\u56fe\u7a97\u53e3\u663e\u793a\u4e00\u6bb5\u65f6\u95f4\u540e\u81ea\u52a8\u5173\u95ed\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>time.sleep<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show(block=False)<\/p>\n<h2><strong>\u7b49\u5f855\u79d2\u949f<\/strong><\/h2>\n<p>time.sleep(5)<\/p>\n<h2><strong>\u5173\u95ed\u56fe\u5f62\u7a97\u53e3<\/strong><\/h2>\n<p>plt.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u7ed8\u56fe\u7a97\u53e3\u663e\u793a5\u79d2\u949f\u540e\u81ea\u52a8\u5173\u95ed\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u7279\u5b9a\u65f6\u95f4\u5185\u5c55\u793a\u56fe\u5f62\uff0c\u800c\u4e0d\u9700\u8981\u7528\u6237\u624b\u52a8\u5173\u95ed\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u4ea4\u4e92\u5f0f\u540e\u7aef<\/h2>\n<\/p>\n<p><p>Matplotlib\u63d0\u4f9b\u4e86\u591a\u79cd\u4ea4\u4e92\u5f0f\u540e\u7aef\uff0c\u53ef\u4ee5\u8ba9\u7ed8\u56fe\u7a97\u53e3\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u6b63\u5e38\u663e\u793a\u548c\u4ea4\u4e92\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>TkAgg<\/code>\u540e\u7aef\uff0c\u53ef\u4ee5\u5728Tkinter\u73af\u5883\u4e2d\u663e\u793a\u56fe\u5f62\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib<\/p>\n<p>matplotlib.use(&#39;TkAgg&#39;)<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e9b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u540e\u7aef\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u6b63\u5e38\u663e\u793a\u548c\u64cd\u4f5c\u7ed8\u56fe\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u6574\u7ed8\u56fe\u7a97\u53e3\u7684\u663e\u793a\u65f6\u95f4\u3001\u4f7f\u7528\u963b\u585e\u51fd\u6570\u3001\u624b\u52a8\u63a7\u5236\u7a97\u53e3\u5173\u95ed\u3001\u5728\u7ed8\u56fe\u4e2d\u52a0\u5165\u4ea4\u4e92\u529f\u80fd\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9Python\u7ed8\u56fe\u505c\u7559\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u63a7\u5236Python\u7ed8\u56fe\u7a97\u53e3\u7684\u663e\u793a\u548c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u9759\u6001\u56fe\u50cf\u800c\u4e0d\u81ea\u52a8\u5173\u95ed\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7b49\u5e93\u7ed8\u5236\u56fe\u50cf\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.show(block=True)<\/code>\u6765\u4fdd\u6301\u56fe\u50cf\u7a97\u53e3\u6253\u5f00\u3002\u8fd9\u6837\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u56fe\u5f62\u754c\u9762\u4e2d\u4e0e\u56fe\u50cf\u8fdb\u884c\u4ea4\u4e92\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u56fe\u50cf\u5728\u7ed8\u5236\u540e\u7acb\u5373\u5173\u95ed\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4fdd\u5b58\u7ed8\u5236\u7684\u56fe\u50cf\uff1f<\/strong><br \/>\u8981\u4fdd\u5b58\u7ed8\u5236\u7684\u56fe\u50cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.savefig(&#39;filename.png&#39;)<\/code>\u547d\u4ee4\uff0c\u8fd9\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u6587\u4ef6\uff08\u4f8b\u5982PNG\u3001JPEG\u7b49\uff09\u3002\u786e\u4fdd\u5728\u8c03\u7528<code>plt.savefig<\/code>\u4e4b\u524d\u8c03\u7528<code>plt.show()<\/code>\uff0c\u4ee5\u907f\u514d\u56fe\u50cf\u88ab\u5173\u95ed\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u7ed8\u56fe\u65f6\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff1f<\/strong><br \/>\u82e5\u5e0c\u671b\u5728\u7ed8\u56fe\u65f6\u6682\u505c\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input(&quot;Press Enter to continue...&quot;)<\/code>\u6765\u8981\u6c42\u7528\u6237\u8f93\u5165\u3002\u8fd9\u5c06\u5728\u663e\u793a\u56fe\u50cf\u540e\u6682\u505c\u7a0b\u5e8f\uff0c\u76f4\u5230\u7528\u6237\u6309\u4e0b\u56de\u8f66\u952e\uff0c\u4f7f\u5176\u5177\u6709\u66f4\u597d\u7684\u4ea4\u4e92\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7\u8c03\u6574\u7ed8\u56fe\u7a97\u53e3\u7684\u663e\u793a\u65f6\u95f4\u3001\u4f7f\u7528\u963b\u585e\u51fd\u6570\u3001\u624b\u52a8\u63a7\u5236\u7a97\u53e3\u5173\u95ed\u3001\u5728\u7ed8\u56fe\u4e2d\u52a0\u5165\u4ea4\u4e92\u529f\u80fd\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9Python\u7ed8\u56fe [&hellip;]","protected":false},"author":3,"featured_media":1154159,"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\/1154154"}],"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=1154154"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154154\/revisions"}],"predecessor-version":[{"id":1154162,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154154\/revisions\/1154162"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154159"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}