{"id":1144090,"date":"2025-01-08T22:56:39","date_gmt":"2025-01-08T14:56:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144090.html"},"modified":"2025-01-08T22:56:46","modified_gmt":"2025-01-08T14:56:46","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e9%92%9d%e8%a7%92%e4%b8%89%e8%a7%92%e5%bd%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1144090.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u949d\u89d2\u4e09\u89d2\u5f62"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181354\/629df6d7-7015-4485-baea-3c70ef8813d5.webp\" alt=\"\u5982\u4f55\u7528python\u753b\u949d\u89d2\u4e09\u89d2\u5f62\" \/><\/p>\n<p><p> <strong>\u7528Python\u753b\u949d\u89d2\u4e09\u89d2\u5f62\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u3001\u4f7f\u7528turtle\u5e93\u7ed8\u5236\u3001\u4ee5\u53ca\u7ed3\u5408PIL\u5e93\u8fdb\u884c\u56fe\u50cf\u7ed8\u5236\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5373\u901a\u8fc7matplotlib\u5e93\u6765\u5b9e\u73b0\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165matplotlib\u5e93\uff0c\u8fd9\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u7ed8\u56fe\u5de5\u5177\u3002\u901a\u8fc7\u8fd9\u4e2a\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5305\u62ec\u4e09\u89d2\u5f62\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528matplotlib\u6765\u7ed8\u5236\u4e00\u4e2a\u949d\u89d2\u4e09\u89d2\u5f62\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/strong><\/h2>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u5236\u4e4b\u524d\uff0c\u6211\u4eec\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>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u8fd9\u4e2a\u5e93\uff1a<\/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><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u5b9a\u4e49\u4e09\u89d2\u5f62\u7684\u9876\u70b9<\/strong><\/h2>\n<p><p>\u4e00\u4e2a\u4e09\u89d2\u5f62\u53ef\u4ee5\u901a\u8fc7\u4e09\u4e2a\u9876\u70b9\u6765\u5b9a\u4e49\u3002\u4e3a\u4e86\u786e\u4fdd\u6211\u4eec\u7ed8\u5236\u7684\u662f\u4e00\u4e2a\u949d\u89d2\u4e09\u89d2\u5f62\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5176\u4e2d\u4e00\u4e2a\u89d2\u5927\u4e8e90\u5ea6\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4ee5\u4e0b\u9876\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e09\u89d2\u5f62\u7684\u9876\u70b9\u5750\u6807<\/p>\n<p>x = np.array([0, 1, 0.5])<\/p>\n<p>y = np.array([0, 0, 2])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9876\u70b9 (0, 0), (1, 0) \u548c (0.5, 2) \u5f62\u6210\u4e86\u4e00\u4e2a\u949d\u89d2\u4e09\u89d2\u5f62\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u7ed8\u5236\u4e09\u89d2\u5f62<\/strong><\/h2>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528matplotlib\u7684plot\u51fd\u6570\u6765\u7ed8\u5236\u8fd9\u4e2a\u4e09\u89d2\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y, &#39;b-&#39;)  # \u4f7f\u7528\u84dd\u8272\u7684\u7ebf\u6761\u7ed8\u5236\u4e09\u89d2\u5f62<\/p>\n<p>plt.fill(x, y, &#39;b&#39;, alpha=0.3)  # \u586b\u5145\u4e09\u89d2\u5f62\u7684\u5185\u90e8\uff0c\u8bbe\u7f6e\u900f\u660e\u5ea6<\/p>\n<p>plt.xlim(-1, 2)  # \u8bbe\u7f6ex\u8f74\u7684\u8303\u56f4<\/p>\n<p>plt.ylim(-1, 3)  # \u8bbe\u7f6ey\u8f74\u7684\u8303\u56f4<\/p>\n<p>plt.gca().set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)  # \u8bbe\u7f6e\u5750\u6807\u8f74\u6bd4\u4f8b\u76f8\u7b49<\/p>\n<p>plt.title(&#39;\u949d\u89d2\u4e09\u89d2\u5f62&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u4f18\u5316\u7ed8\u56fe<\/strong><\/h2>\n<p><p>\u4e3a\u4e86\u8ba9\u7ed8\u5236\u7684\u4e09\u89d2\u5f62\u66f4\u52a0\u7f8e\u89c2\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u7ed8\u56fe\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u5750\u6807\u8f74\u6807\u7b7e\u3001\u7f51\u683c\u7ebf\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y, &#39;b-&#39;, linewidth=2)  # \u4f7f\u7528\u66f4\u7c97\u7684\u7ebf\u6761<\/p>\n<p>plt.fill(x, y, &#39;b&#39;, alpha=0.3)  # \u586b\u5145\u4e09\u89d2\u5f62\u7684\u5185\u90e8\uff0c\u8bbe\u7f6e\u900f\u660e\u5ea6<\/p>\n<p>plt.xlim(-1, 2)  # \u8bbe\u7f6ex\u8f74\u7684\u8303\u56f4<\/p>\n<p>plt.ylim(-1, 3)  # \u8bbe\u7f6ey\u8f74\u7684\u8303\u56f4<\/p>\n<p>plt.xlabel(&#39;X \u8f74&#39;)  # \u6dfb\u52a0x\u8f74\u6807\u7b7e<\/p>\n<p>plt.ylabel(&#39;Y \u8f74&#39;)  # \u6dfb\u52a0y\u8f74\u6807\u7b7e<\/p>\n<p>plt.grid(True)  # \u663e\u793a\u7f51\u683c\u7ebf<\/p>\n<p>plt.gca().set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)  # \u8bbe\u7f6e\u5750\u6807\u8f74\u6bd4\u4f8b\u76f8\u7b49<\/p>\n<p>plt.title(&#39;\u949d\u89d2\u4e09\u89d2\u5f62&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u8f7b\u677e\u5730\u7ed8\u5236\u4e00\u4e2a\u949d\u89d2\u4e09\u89d2\u5f62\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u4e09\u89d2\u5f62\u7684\u9876\u70b9\u5750\u6807\u3001\u7ebf\u6761\u989c\u8272\u3001\u586b\u5145\u989c\u8272\u7b49\u53c2\u6570\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u89c6\u89c9\u6548\u679c\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u60a8\u80fd\u591f\u638c\u63e1\u4f7f\u7528Python\u7ed8\u5236\u949d\u89d2\u4e09\u89d2\u5f62\u7684\u65b9\u6cd5\uff0c\u5e76\u5e94\u7528\u5230\u5b9e\u9645\u7684\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u7ed8\u5236\u949d\u89d2\u4e09\u89d2\u5f62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u5305\u62ecMatplotlib\u548cPygame\u3002Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u9002\u5408\u4e8e\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\uff0c\u5305\u62ec\u949d\u89d2\u4e09\u89d2\u5f62\u3002\u4f7f\u7528Matplotlib\u65f6\uff0c\u4f60\u53ea\u9700\u5b9a\u4e49\u4e09\u89d2\u5f62\u7684\u4e09\u4e2a\u9876\u70b9\u5750\u6807\uff0c\u7136\u540e\u5229\u7528<code>plt.fill()<\/code>\u51fd\u6570\u586b\u5145\u989c\u8272\u3002\u6b64\u5916\uff0cPygame\u5219\u66f4\u9002\u5408\u4e8e\u6e38\u620f\u5f00\u53d1\uff0c\u80fd\u591f\u63d0\u4f9b\u66f4\u4e3a\u52a8\u6001\u7684\u56fe\u5f62\u7ed8\u5236\u4f53\u9a8c\u3002\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u5b9a\u949d\u89d2\u4e09\u89d2\u5f62\u7684\u9876\u70b9\u5750\u6807\uff1f<\/strong><br \/>\u949d\u89d2\u4e09\u89d2\u5f62\u7684\u7279\u5f81\u5728\u4e8e\u4e00\u4e2a\u89d2\u5927\u4e8e90\u5ea6\u3002\u5728\u9009\u62e9\u9876\u70b9\u5750\u6807\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u5750\u6807\u8ba1\u7b97\u6765\u786e\u4fdd\u4e00\u4e2a\u89d2\u5927\u4e8e90\u5ea6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u9009\u5b9a\u4e24\u4e2a\u70b9\u5728x\u8f74\u7684\u540c\u4e00\u4fa7\uff0c\u800c\u7b2c\u4e09\u4e2a\u70b9\u5728\u8fd9\u4e9b\u70b9\u7684\u76f8\u5bf9\u8fdc\u79bb\u7684\u4e00\u4fa7\u3002\u786e\u4fdd\u8fd9\u4e9b\u5750\u6807\u6ee1\u8db3\u4e09\u89d2\u5f62\u4e0d\u7b49\u5f0f\u5b9a\u7406\uff0c\u4ece\u800c\u5f62\u6210\u4e00\u4e2a\u6709\u6548\u7684\u949d\u89d2\u4e09\u89d2\u5f62\u3002<\/p>\n<p><strong>\u5728Python\u7ed8\u5236\u949d\u89d2\u4e09\u89d2\u5f62\u65f6\u5982\u4f55\u8bbe\u7f6e\u989c\u8272\u548c\u6837\u5f0f\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u949d\u89d2\u4e09\u89d2\u5f62\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>fill()<\/code>\u51fd\u6570\u7684\u53c2\u6570\u8bbe\u7f6e\u989c\u8272\u548c\u900f\u660e\u5ea6\u3002\u4f8b\u5982\uff0c<code>plt.fill(x, y, color=&#39;blue&#39;, alpha=0.5)<\/code>\u5c06\u4e09\u89d2\u5f62\u586b\u5145\u4e3a\u84dd\u8272\uff0c\u5e76\u8bbe\u7f6e50%\u7684\u900f\u660e\u5ea6\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>plt.plot()<\/code>\u53ef\u4ee5\u7ed8\u5236\u4e09\u89d2\u5f62\u7684\u8fb9\u754c\u7ebf\uff0c\u5e76\u901a\u8fc7\u6307\u5b9a\u7ebf\u6761\u6837\u5f0f\u3001\u5bbd\u5ea6\u7b49\u53c2\u6570\u6765\u7f8e\u5316\u56fe\u5f62\u3002\u901a\u8fc7\u8fd9\u4e9b\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u589e\u5f3a\u56fe\u5f62\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u753b\u949d\u89d2\u4e09\u89d2\u5f62\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u3001\u4f7f\u7528turtle\u5e93\u7ed8\u5236\u3001\u4ee5\u53ca [&hellip;]","protected":false},"author":3,"featured_media":1144108,"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\/1144090"}],"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=1144090"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144090\/revisions"}],"predecessor-version":[{"id":1144111,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144090\/revisions\/1144111"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144108"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}