{"id":1045620,"date":"2024-12-31T13:25:12","date_gmt":"2024-12-31T05:25:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1045620.html"},"modified":"2024-12-31T13:25:16","modified_gmt":"2024-12-31T05:25:16","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e5%a4%9a%e8%be%b9%e5%bd%a2%e5%a1%ab%e5%85%85%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1045620.html","title":{"rendered":"Python\u5982\u4f55\u753b\u591a\u8fb9\u5f62\u586b\u5145\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/500d6f43-dd20-4307-b3c9-991974e68eca.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u753b\u591a\u8fb9\u5f62\u586b\u5145\u56fe\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7ed8\u5236\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u4f7f\u7528turtle\u5e93\u3001\u4f7f\u7528PIL\u5e93\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u591a\u8fb9\u5f62\u56fe\u5f62\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8fdb\u884c\u5b9a\u5236\u5316\u3002matplotlib\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u80fd\u591f\u751f\u6210\u9ad8\u8d28\u91cf\u76842D\u56fe\u5f62\u3002\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u5b89\u88c5matplotlib\u5e93\u3001\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u3001\u5982\u4f55\u8fdb\u884c\u5b9a\u5236\u5316\u8bbe\u7f6e\u4ee5\u53ca\u5982\u4f55\u4fdd\u5b58\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5matplotlib\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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>\u4e8c\u3001\u7ed8\u5236\u7b80\u5355\u7684\u591a\u8fb9\u5f62\u586b\u5145\u56fe<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u5c06\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u3002\u6211\u4eec\u5c06\u4f7f\u7528matplotlib\u4e2d\u7684<code>pyplot<\/code>\u6a21\u5757\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\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<h2><strong>\u5b9a\u4e49\u591a\u8fb9\u5f62\u7684\u9876\u70b9\u5750\u6807<\/strong><\/h2>\n<p>polygon = np.array([[2, 3], [4, 7], [6, 3], [5, 2], [3, 2]])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u591a\u8fb9\u5f62\u5e76\u586b\u5145\u989c\u8272<\/strong><\/h2>\n<p>plt.fill(polygon[:, 0], polygon[:, 1], color=&#39;blue&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 8)<\/p>\n<p>plt.ylim(0, 8)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5b9a\u5236\u5316\u591a\u8fb9\u5f62\u586b\u5145\u56fe<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9a\u5236\u5316\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u3002\u53ef\u4ee5\u66f4\u6539\u586b\u5145\u989c\u8272\u3001\u8bbe\u7f6e\u8fb9\u6846\u989c\u8272\u548c\u6837\u5f0f\u3001\u6dfb\u52a0\u56fe\u4f8b\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u66f4\u6539\u586b\u5145\u989c\u8272\u548c\u900f\u660e\u5ea6<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">plt.fill(polygon[:, 0], polygon[:, 1], color=&#39;green&#39;, alpha=0.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u586b\u5145\u989c\u8272\u66f4\u6539\u4e3a\u7eff\u8272\uff0c\u5e76\u8bbe\u7f6e\u900f\u660e\u5ea6\u4e3a0.5\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8bbe\u7f6e\u8fb9\u6846\u989c\u8272\u548c\u6837\u5f0f<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">plt.plot(polygon[:, 0], polygon[:, 1], color=&#39;red&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u8fb9\u6846\u989c\u8272\u8bbe\u7f6e\u4e3a\u7ea2\u8272\uff0c\u8fb9\u6846\u6837\u5f0f\u8bbe\u7f6e\u4e3a\u865a\u7ebf\uff0c\u8fb9\u6846\u5bbd\u5ea6\u8bbe\u7f6e\u4e3a2\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u6dfb\u52a0\u56fe\u4f8b<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">plt.fill(polygon[:, 0], polygon[:, 1], color=&#39;blue&#39;, label=&#39;Polygon&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4fdd\u5b58\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>savefig<\/code>\u51fd\u6570\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.savefig(&#39;polygon.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5b8c\u6574\u793a\u4f8b<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5b9a\u5236\u5316\u7684\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\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<h2><strong>\u5b9a\u4e49\u591a\u8fb9\u5f62\u7684\u9876\u70b9\u5750\u6807<\/strong><\/h2>\n<p>polygon = np.array([[2, 3], [4, 7], [6, 3], [5, 2], [3, 2]])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u591a\u8fb9\u5f62\u5e76\u586b\u5145\u989c\u8272<\/strong><\/h2>\n<p>plt.fill(polygon[:, 0], polygon[:, 1], color=&#39;green&#39;, alpha=0.5, label=&#39;Polygon&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8fb9\u6846\u989c\u8272\u548c\u6837\u5f0f<\/strong><\/h2>\n<p>plt.plot(polygon[:, 0], polygon[:, 1], color=&#39;red&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 8)<\/p>\n<p>plt.ylim(0, 8)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;polygon.png&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u591a\u4e2a\u591a\u8fb9\u5f62<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u56fe\u4e2d\u7ed8\u5236\u591a\u4e2a\u591a\u8fb9\u5f62\uff0c\u6bcf\u4e2a\u591a\u8fb9\u5f62\u90fd\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u989c\u8272\u548c\u6837\u5f0f\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<h2><strong>\u5b9a\u4e49\u591a\u4e2a\u591a\u8fb9\u5f62\u7684\u9876\u70b9\u5750\u6807<\/strong><\/h2>\n<p>polygon1 = np.array([[2, 3], [4, 7], [6, 3], [5, 2], [3, 2]])<\/p>\n<p>polygon2 = np.array([[1, 1], [2, 4], [3, 1]])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u4e2a\u591a\u8fb9\u5f62\u5e76\u586b\u5145\u989c\u8272<\/strong><\/h2>\n<p>plt.fill(polygon1[:, 0], polygon1[:, 1], color=&#39;blue&#39;, alpha=0.5, label=&#39;Polygon 1&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2a\u591a\u8fb9\u5f62\u5e76\u586b\u5145\u989c\u8272<\/strong><\/h2>\n<p>plt.fill(polygon2[:, 0], polygon2[:, 1], color=&#39;orange&#39;, alpha=0.5, label=&#39;Polygon 2&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8fb9\u6846\u989c\u8272\u548c\u6837\u5f0f<\/strong><\/h2>\n<p>plt.plot(polygon1[:, 0], polygon1[:, 1], color=&#39;red&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/p>\n<p>plt.plot(polygon2[:, 0], polygon2[:, 1], color=&#39;green&#39;, linestyle=&#39;-&#39;, linewidth=2)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 8)<\/p>\n<p>plt.ylim(0, 8)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u3002\u6211\u4eec\u9996\u5148\u5c55\u793a\u4e86\u5982\u4f55\u5b89\u88c5matplotlib\u5e93\uff0c\u7136\u540e\u4ecb\u7ecd\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u3002\u63a5\u7740\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u5236\u5316\u591a\u8fb9\u5f62\u586b\u5145\u56fe\uff0c\u5305\u62ec\u66f4\u6539\u586b\u5145\u989c\u8272\u548c\u900f\u660e\u5ea6\u3001\u8bbe\u7f6e\u8fb9\u6846\u989c\u8272\u548c\u6837\u5f0f\u3001\u6dfb\u52a0\u56fe\u4f8b\u7b49\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u5728\u540c\u4e00\u4e2a\u56fe\u4e2d\u7ed8\u5236\u591a\u4e2a\u591a\u8fb9\u5f62\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u8ba9\u60a8\u80fd\u591f\u8f7b\u677e\u5730\u4f7f\u7528Python\u7ed8\u5236\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u591a\u8fb9\u5f62\u586b\u5145\u56fe\uff1f<\/strong><br \/>\u8981\u7ed8\u5236\u591a\u8fb9\u5f62\u586b\u5145\u56fe\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u591a\u8fb9\u5f62\u7684\u9876\u70b9\u5750\u6807\u5e76\u4f7f\u7528<code>fill<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u586b\u5145\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u5b9a\u4e49\u591a\u8fb9\u5f62\u7684\u9876\u70b9\nx = [1, 2, 3, 4, 5]\ny = [1, 3, 2, 5, 4]\n\n# \u7ed8\u5236\u586b\u5145\u56fe\nplt.fill(x, y, color=&#39;skyblue&#39;, alpha=0.5)\nplt.title(&#39;Polygon Fill Example&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u591a\u8fb9\u5f62\u7684\u989c\u8272\u548c\u900f\u660e\u5ea6\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>fill<\/code>\u51fd\u6570\u7684<code>color<\/code>\u548c<code>alpha<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u591a\u8fb9\u5f62\u7684\u989c\u8272\u548c\u900f\u660e\u5ea6\u3002<code>color<\/code>\u53c2\u6570\u53ef\u4ee5\u63a5\u53d7\u989c\u8272\u540d\u79f0\u3001\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u6216RGB\u5143\u7ec4\uff0c\u800c<code>alpha<\/code>\u53c2\u6570\u5219\u63a7\u5236\u900f\u660e\u5ea6\uff08\u8303\u56f4\u4ece0\u52301\uff09\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>plt.fill(x, y, color=&#39;red&#39;, alpha=0.3)<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u7ea2\u8272\u3001\u900f\u660e\u5ea6\u4e3a0.3\u7684\u591a\u8fb9\u5f62\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u4e2d\u6dfb\u52a0\u8fb9\u6846\u7ebf\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u4e2d\u6dfb\u52a0\u8fb9\u6846\u7ebf\u3002\u4f7f\u7528<code>fill<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>edgecolor<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u8fb9\u6846\u7684\u989c\u8272\uff0c<code>linewidth<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u8fb9\u6846\u7684\u5bbd\u5ea6\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">plt.fill(x, y, color=&#39;skyblue&#39;, edgecolor=&#39;black&#39;, linewidth=2)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u9ed1\u8272\u8fb9\u6846\u7684\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7ed8\u5236\u591a\u8fb9\u5f62\u586b\u5145\u56fe\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u4f7f\u7528turtle\u5e93\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1045645,"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\/1045620"}],"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=1045620"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045620\/revisions"}],"predecessor-version":[{"id":1045649,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045620\/revisions\/1045649"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1045645"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1045620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1045620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1045620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}