{"id":1035038,"date":"2024-12-31T11:55:23","date_gmt":"2024-12-31T03:55:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1035038.html"},"modified":"2024-12-31T11:55:26","modified_gmt":"2024-12-31T03:55:26","slug":"python%e5%a6%82%e4%bd%95%e7%bb%98%e5%88%b6%e5%9b%9b%e8%be%b9%e5%bd%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1035038.html","title":{"rendered":"python\u5982\u4f55\u7ed8\u5236\u56db\u8fb9\u5f62"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/bf7acc78-d458-439a-af33-bfb77c1746dc.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u7ed8\u5236\u56db\u8fb9\u5f62\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7ed8\u5236\u56db\u8fb9\u5f62\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u5229\u7528\u591a\u79cd\u5e93\u548c\u5de5\u5177\uff0c\u5982Matplotlib\u3001Turtle\u548cPygame\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1aMatplotlib\u3001Turtle\u3001Pygame\u3002\u4ee5\u4e0b\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528Matplotlib\u7ed8\u5236\u56db\u8fb9\u5f62\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684Python\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u548c\u56fe\u5f62\u3002\u5b83\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684API\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u590d\u6742\u7684\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u7ed8\u5236\u56db\u8fb9\u5f62\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u5e76\u5bfc\u5165Matplotlib<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u5236\u56db\u8fb9\u5f62\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\u3002\u5982\u679c\u5c1a\u672a\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-python\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165Matplotlib\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<p><p>\u4e8c\u3001\u5b9a\u4e49\u56db\u8fb9\u5f62\u7684\u9876\u70b9<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u5b9a\u4e49\u56db\u8fb9\u5f62\u7684\u9876\u70b9\u3002\u5047\u8bbe\u6211\u4eec\u8981\u7ed8\u5236\u4e00\u4e2a\u4efb\u610f\u7684\u56db\u8fb9\u5f62\uff0c\u53ef\u4ee5\u5b9a\u4e49\u56db\u4e2a\u9876\u70b9\u7684\u5750\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u56db\u8fb9\u5f62\u9876\u70b9\u7684\u5750\u6807<\/p>\n<p>vertices = np.array([[1, 2], [4, 2], [3, 5], [1, 4]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u521b\u5efa\u56db\u8fb9\u5f62\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684Path\u7c7b\u521b\u5efa\u56db\u8fb9\u5f62\u7684\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from matplotlib.path import Path<\/p>\n<p>import matplotlib.patches as patches<\/p>\n<h2><strong>\u521b\u5efa\u8def\u5f84<\/strong><\/h2>\n<p>path_data = [<\/p>\n<p>    (Path.MOVETO, vertices[0]),<\/p>\n<p>    (Path.LINETO, vertices[1]),<\/p>\n<p>    (Path.LINETO, vertices[2]),<\/p>\n<p>    (Path.LINETO, vertices[3]),<\/p>\n<p>    (Path.CLOSEPOLY, vertices[0])<\/p>\n<p>]<\/p>\n<p>codes, verts = zip(*path_data)<\/p>\n<p>path = Path(verts, codes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u7ed8\u5236\u56db\u8fb9\u5f62<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684Patch\u7c7b\u7ed8\u5236\u56db\u8fb9\u5f62\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7ed8\u56fe\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u56db\u8fb9\u5f62\u8865\u4e01<\/p>\n<p>patch = patches.PathPatch(path, facecolor=&#39;orange&#39;, lw=2)<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.add_patch(patch)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim(0, 5)<\/p>\n<p>ax.set_ylim(0, 6)<\/p>\n<h2><strong>\u663e\u793a\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u6210\u529f\u5730\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e86\u4e00\u4e2a\u56db\u8fb9\u5f62\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u8be6\u7ec6\u4ecb\u7ecdMatplotlib\u7684\u4f7f\u7528\uff0c\u5305\u62ec\u5982\u4f55\u81ea\u5b9a\u4e49\u56db\u8fb9\u5f62\u7684\u989c\u8272\u3001\u6837\u5f0f\u4ee5\u53ca\u6dfb\u52a0\u5176\u4ed6\u56fe\u5f62\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Matplotlib\u7684\u5b89\u88c5\u548c\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\u4e4b\u4e00\uff0c\u652f\u6301\u591a\u79cd\u56fe\u8868\u7c7b\u578b\u3002\u4ee5\u4e0b\u662f\u5b89\u88c5\u548c\u5bfc\u5165Matplotlib\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Matplotlib<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5Matplotlib\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5bfc\u5165Matplotlib<\/h4>\n<\/p>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165Matplotlib\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<p><h3>\u4e8c\u3001\u5b9a\u4e49\u56db\u8fb9\u5f62\u7684\u9876\u70b9<\/h3>\n<\/p>\n<p><p>\u7ed8\u5236\u56db\u8fb9\u5f62\u9700\u8981\u5b9a\u4e49\u5176\u9876\u70b9\u7684\u5750\u6807\u3002\u5047\u8bbe\u6211\u4eec\u8981\u7ed8\u5236\u4e00\u4e2a\u4efb\u610f\u7684\u56db\u8fb9\u5f62\uff0c\u53ef\u4ee5\u5b9a\u4e49\u5982\u4e0b\u9876\u70b9\u5750\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u56db\u8fb9\u5f62\u9876\u70b9\u7684\u5750\u6807<\/p>\n<p>vertices = np.array([[1, 2], [4, 2], [3, 5], [1, 4]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u5750\u6807\u8868\u793a\u56db\u8fb9\u5f62\u7684\u56db\u4e2a\u9876\u70b9\uff0c\u5206\u522b\u4e3a(1, 2)\u3001(4, 2)\u3001(3, 5)\u548c(1, 4)\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u56db\u8fb9\u5f62\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684Path\u7c7b\u521b\u5efa\u56db\u8fb9\u5f62\u7684\u8def\u5f84\u3002Path\u7c7b\u7528\u4e8e\u5b9a\u4e49\u8def\u5f84\uff0c\u8def\u5f84\u7531\u4e00\u7cfb\u5217\u9876\u70b9\u548c\u76f8\u5e94\u7684\u7ed8\u5236\u547d\u4ee4\u7ec4\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from matplotlib.path import Path<\/p>\n<p>import matplotlib.patches as patches<\/p>\n<h2><strong>\u521b\u5efa\u8def\u5f84<\/strong><\/h2>\n<p>path_data = [<\/p>\n<p>    (Path.MOVETO, vertices[0]),<\/p>\n<p>    (Path.LINETO, vertices[1]),<\/p>\n<p>    (Path.LINETO, vertices[2]),<\/p>\n<p>    (Path.LINETO, vertices[3]),<\/p>\n<p>    (Path.CLOSEPOLY, vertices[0])<\/p>\n<p>]<\/p>\n<p>codes, verts = zip(*path_data)<\/p>\n<p>path = Path(verts, codes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u6761\u8def\u5f84\uff0c\u5305\u542b\u56db\u4e2a\u9876\u70b9\u548c\u76f8\u5e94\u7684\u7ed8\u5236\u547d\u4ee4\u3002Path.MOVETO\u8868\u793a\u79fb\u52a8\u5230\u7b2c\u4e00\u4e2a\u9876\u70b9\uff0cPath.LINETO\u8868\u793a\u8fde\u63a5\u5230\u4e0b\u4e00\u4e2a\u9876\u70b9\uff0cPath.CLOSEPOLY\u8868\u793a\u95ed\u5408\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed8\u5236\u56db\u8fb9\u5f62<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684Patch\u7c7b\u7ed8\u5236\u56db\u8fb9\u5f62\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7ed8\u56fe\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u56db\u8fb9\u5f62\u8865\u4e01<\/p>\n<p>patch = patches.PathPatch(path, facecolor=&#39;orange&#39;, lw=2)<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.add_patch(patch)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim(0, 5)<\/p>\n<p>ax.set_ylim(0, 6)<\/p>\n<h2><strong>\u663e\u793a\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aPathPatch\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u7ed8\u56fe\u5bf9\u8c61\u4e2d\u3002\u7136\u540e\uff0c\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4\u5e76\u663e\u793a\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u81ea\u5b9a\u4e49\u56db\u8fb9\u5f62\u7684\u989c\u8272\u548c\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574PathPatch\u5bf9\u8c61\u7684\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u56db\u8fb9\u5f62\u7684\u989c\u8272\u548c\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u66f4\u6539facecolor\u53c2\u6570\u6765\u8bbe\u7f6e\u586b\u5145\u989c\u8272\uff0c\u4f7f\u7528edgecolor\u53c2\u6570\u6765\u8bbe\u7f6e\u8fb9\u6846\u989c\u8272\uff0c\u4f7f\u7528lw\u53c2\u6570\u6765\u8bbe\u7f6e\u8fb9\u6846\u5bbd\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u56db\u8fb9\u5f62\u8865\u4e01<\/p>\n<p>patch = patches.PathPatch(path, facecolor=&#39;blue&#39;, edgecolor=&#39;red&#39;, lw=3, linestyle=&#39;--&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.add_patch(patch)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim(0, 5)<\/p>\n<p>ax.set_ylim(0, 6)<\/p>\n<h2><strong>\u663e\u793a\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u586b\u5145\u989c\u8272\u8bbe\u7f6e\u4e3a\u84dd\u8272\uff0c\u8fb9\u6846\u989c\u8272\u8bbe\u7f6e\u4e3a\u7ea2\u8272\uff0c\u8fb9\u6846\u5bbd\u5ea6\u8bbe\u7f6e\u4e3a3\uff0c\u5e76\u4f7f\u7528\u865a\u7ebf\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6dfb\u52a0\u5176\u4ed6\u56fe\u5f62\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u7ed8\u5236\u56db\u8fb9\u5f62\u5916\uff0c\u8fd8\u53ef\u4ee5\u5728\u540c\u4e00\u56fe\u5f62\u4e2d\u6dfb\u52a0\u5176\u4ed6\u56fe\u5f62\u5143\u7d20\uff0c\u4f8b\u5982\u70b9\u3001\u7ebf\u548c\u6587\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u9876\u70b9\u5750\u6807<\/p>\n<p>vertices = np.array([[1, 2], [4, 2], [3, 5], [1, 4]])<\/p>\n<h2><strong>\u521b\u5efa\u8def\u5f84<\/strong><\/h2>\n<p>path_data = [<\/p>\n<p>    (Path.MOVETO, vertices[0]),<\/p>\n<p>    (Path.LINETO, vertices[1]),<\/p>\n<p>    (Path.LINETO, vertices[2]),<\/p>\n<p>    (Path.LINETO, vertices[3]),<\/p>\n<p>    (Path.CLOSEPOLY, vertices[0])<\/p>\n<p>]<\/p>\n<p>codes, verts = zip(*path_data)<\/p>\n<p>path = Path(verts, codes)<\/p>\n<h2><strong>\u521b\u5efa\u56db\u8fb9\u5f62\u8865\u4e01<\/strong><\/h2>\n<p>patch = patches.PathPatch(path, facecolor=&#39;green&#39;, lw=2)<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.add_patch(patch)<\/p>\n<h2><strong>\u6dfb\u52a0\u9876\u70b9<\/strong><\/h2>\n<p>ax.plot(vertices[:, 0], vertices[:, 1], &#39;ro&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u7ebf\u6761<\/strong><\/h2>\n<p>ax.plot([1, 4], [2, 2], &#39;b--&#39;)<\/p>\n<p>ax.plot([3, 1], [5, 4], &#39;g-.&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u672c<\/strong><\/h2>\n<p>ax.text(2.5, 3.5, &#39;Quadrilateral&#39;, fontsize=12, ha=&#39;center&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim(0, 5)<\/p>\n<p>ax.set_ylim(0, 6)<\/p>\n<h2><strong>\u663e\u793a\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9664\u4e86\u7ed8\u5236\u56db\u8fb9\u5f62\u5916\uff0c\u8fd8\u6dfb\u52a0\u4e86\u9876\u70b9\uff08\u7ea2\u8272\u5706\u70b9\uff09\u3001\u7ebf\u6761\uff08\u865a\u7ebf\u548c\u70b9\u7ebf\uff09\u548c\u6587\u672c\uff08\u6807\u7b7e\uff09\uff0c\u4f7f\u5f97\u56fe\u5f62\u66f4\u52a0\u4e30\u5bcc\u548c\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4fdd\u5b58\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u5b8c\u6210\u7ed8\u56fe\u540e\uff0c\u53ef\u4ee5\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002Matplotlib\u63d0\u4f9b\u4e86savefig\u65b9\u6cd5\u6765\u4fdd\u5b58\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>fig.savefig(&#39;quadrilateral.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u683c\u5f0f\uff0c\u5982PNG\u3001JPEG\u3001SVG\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528Turtle\u7ed8\u5236\u56db\u8fb9\u5f62<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Turtle\u5e93\u7ed8\u5236\u56db\u8fb9\u5f62\u3002Turtle\u662fPython\u5185\u7f6e\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u56fe\u5f62\u7ed8\u5236\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Turtle\u7ed8\u5236\u56db\u8fb9\u5f62\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3<\/strong><\/h2>\n<p>window = turtle.Screen()<\/p>\n<p>window.bgcolor(&quot;white&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6d77\u9f9f\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<p>t.pensize(3)<\/p>\n<p>t.color(&quot;blue&quot;)<\/p>\n<h2><strong>\u7ed8\u5236\u56db\u8fb9\u5f62<\/strong><\/h2>\n<p>t.penup()<\/p>\n<p>t.goto(1, 2)<\/p>\n<p>t.pendown()<\/p>\n<p>t.goto(4, 2)<\/p>\n<p>t.goto(3, 5)<\/p>\n<p>t.goto(1, 4)<\/p>\n<p>t.goto(1, 2)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Turtle\u5e93\u521b\u5efa\u4e00\u4e2a\u6d77\u9f9f\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6e\u5176\u5c5e\u6027\u3002\u7136\u540e\uff0c\u901a\u8fc7\u79fb\u52a8\u548c\u7ed8\u5236\u64cd\u4f5c\u6765\u7ed8\u5236\u56db\u8fb9\u5f62\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528Pygame\u7ed8\u5236\u56db\u8fb9\u5f62<\/h3>\n<\/p>\n<p><p>Pygame\u662f\u4e00\u4e2a\u7528\u4e8e\u5f00\u53d12D\u6e38\u620f\u7684Python\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pygame\u7ed8\u5236\u56db\u8fb9\u5f62\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import sys<\/p>\n<h2><strong>\u521d\u59cb\u5316Pygame<\/strong><\/h2>\n<p>pygame.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3<\/strong><\/h2>\n<p>size = (500, 500)<\/p>\n<p>screen = pygame.display.set_mode(size)<\/p>\n<p>pygame.display.set_caption(&quot;Draw Quadrilateral&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u989c\u8272<\/strong><\/h2>\n<p>black = (0, 0, 0)<\/p>\n<p>blue = (0, 0, 255)<\/p>\n<h2><strong>\u5b9a\u4e49\u9876\u70b9\u5750\u6807<\/strong><\/h2>\n<p>vertices = [(100, 200), (400, 200), (300, 500), (100, 400)]<\/p>\n<h2><strong>\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>running = True<\/p>\n<p>while running:<\/p>\n<p>    for event in pygame.event.get():<\/p>\n<p>        if event.type == pygame.QUIT:<\/p>\n<p>            running = False<\/p>\n<p>    # \u7ed8\u5236\u56db\u8fb9\u5f62<\/p>\n<p>    screen.fill(black)<\/p>\n<p>    pygame.draw.polygon(screen, blue, vertices, 2)<\/p>\n<p>    # \u66f4\u65b0\u663e\u793a<\/p>\n<p>    pygame.display.flip()<\/p>\n<h2><strong>\u9000\u51faPygame<\/strong><\/h2>\n<p>pygame.quit()<\/p>\n<p>sys.exit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pygame\u5e93\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3\uff0c\u5e76\u5b9a\u4e49\u56db\u8fb9\u5f62\u7684\u9876\u70b9\u5750\u6807\u3002\u901a\u8fc7\u4e3b\u5faa\u73af\u4e0d\u65ad\u7ed8\u5236\u56db\u8fb9\u5f62\uff0c\u5e76\u66f4\u65b0\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u4e86\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u56db\u8fb9\u5f62\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u3001Turtle\u548cPygame\u3002Matplotlib\u9002\u7528\u4e8e\u79d1\u5b66\u7ed8\u56fe\u548c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u7684API\u3002Turtle\u9002\u7528\u4e8e\u7b80\u5355\u56fe\u5f62\u7ed8\u5236\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002Pygame\u9002\u7528\u4e8e2D\u6e38\u620f\u5f00\u53d1\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u590d\u6742\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u7ed8\u5236\u56db\u8fb9\u5f62\u7684\u65b9\u6cd5\uff0c\u5e76\u5e94\u7528\u4e8e\u5b9e\u9645\u9879\u76ee\u4e2d\u3002\u5982\u679c\u60a8\u5bf9\u5176\u4ed6\u56fe\u5f62\u7ed8\u5236\u6709\u5174\u8da3\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63a2\u7d22\u8fd9\u4e9b\u5e93\u7684\u66f4\u591a\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u56db\u8fb9\u5f62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u56db\u8fb9\u5f62\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Matplotlib\u5e93\u3002\u63a5\u7740\uff0c\u5b9a\u4e49\u56db\u8fb9\u5f62\u7684\u56db\u4e2a\u9876\u70b9\u5750\u6807\uff0c\u5e76\u5229\u7528<code>plt.fill()<\/code>\u51fd\u6570\u586b\u5145\u56db\u8fb9\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u5b9a\u4e49\u56db\u8fb9\u5f62\u7684\u9876\u70b9\u5750\u6807\nx = [1, 2, 3, 4]\ny = [1, 3, 2, 1]\n\n# \u7ed8\u5236\u56db\u8fb9\u5f62\nplt.fill(x, y, color=&#39;lightblue&#39;)\nplt.xlim(0, 5)\nplt.ylim(0, 4)\nplt.title(&quot;Simple Quadrilateral&quot;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u7ed8\u5236\u56db\u8fb9\u5f62\uff1f<\/strong><br \/>\u5f53\u7136\uff0c\u9664\u4e86Matplotlib\uff0cPython\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u56fe\u5f62\u5e93\u6765\u7ed8\u5236\u56db\u8fb9\u5f62\u3002\u4f8b\u5982\uff0cPygame\u548cTurtle\u90fd\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002Pygame\u9002\u5408\u4e8e\u6e38\u620f\u5f00\u53d1\uff0c\u800cTurtle\u5219\u9002\u5408\u4e8e\u6559\u80b2\u548c\u7b80\u5355\u7684\u56fe\u5f62\u7ed8\u5236\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\u56db\u8fb9\u5f62\u7684\u7ed8\u5236\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u81ea\u5b9a\u4e49\u56db\u8fb9\u5f62\u7684\u989c\u8272\u548c\u6837\u5f0f\u5417\uff1f<\/strong><br \/>\u5b8c\u5168\u53ef\u4ee5\u3002\u5728Matplotlib\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>plt.fill()<\/code>\u51fd\u6570\u7684\u53c2\u6570\u81ea\u5b9a\u4e49\u989c\u8272\uff0c\u4f8b\u5982<code>color=&#39;red&#39;<\/code>\u6216\u4f7f\u7528RGBA\u503c\u6765\u8bbe\u7f6e\u900f\u660e\u5ea6\u3002\u6b64\u5916\uff0c\u60a8\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7ebf\u6761\u6837\u5f0f\u3001\u8fb9\u6846\u5bbd\u5ea6\u7b49\u53c2\u6570\u6765\u8fdb\u4e00\u6b65\u7f8e\u5316\u56db\u8fb9\u5f62\uff0c\u4f7f\u7528<code>plt.plot()<\/code>\u6765\u7ed8\u5236\u8fb9\u6846\uff0c\u8bbe\u7f6e\u4e0d\u540c\u7684\u7ebf\u578b\u548c\u989c\u8272\uff0c\u4f7f\u5176\u4e0e\u586b\u5145\u989c\u8272\u5f62\u6210\u5bf9\u6bd4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7ed8\u5236\u56db\u8fb9\u5f62\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u5229\u7528\u591a\u79cd\u5e93\u548c\u5de5\u5177\uff0c\u5982Matplotlib\u3001Turtle\u548cPygam [&hellip;]","protected":false},"author":3,"featured_media":1035046,"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\/1035038"}],"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=1035038"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035038\/revisions"}],"predecessor-version":[{"id":1035047,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035038\/revisions\/1035047"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1035046"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1035038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1035038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1035038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}