{"id":1053779,"date":"2024-12-31T14:36:50","date_gmt":"2024-12-31T06:36:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1053779.html"},"modified":"2024-12-31T14:36:52","modified_gmt":"2024-12-31T06:36:52","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e5%9b%9b%e4%b8%aa%e5%9c%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1053779.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u56db\u4e2a\u5706"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/0d1db83a-674d-4d81-9d14-507e4b8d2cd3.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u753b\u56db\u4e2a\u5706\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7ed8\u5236\u56db\u4e2a\u5706\u7684\u6b65\u9aa4<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7ed8\u5236\u56fe\u5f62\u901a\u5e38\u4f7f\u7528\u7684\u5e93\u662fMatplotlib\uff0c\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5404\u79cd\u56fe\u5f62\u548c\u56fe\u8868\u3002\u8981\u7ed8\u5236\u56db\u4e2a\u5706\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>pyplot<\/code>\u6a21\u5757\u3002<strong>\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\u3001\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61\u3001\u4f7f\u7528\u5706\u5f62\u7ed8\u5236\u51fd\u6570\u3001\u8bbe\u7f6e\u7ed8\u56fe\u5c5e\u6027<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u6bcf\u4e2a\u6b65\u9aa4\u5e76\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\u4e2d\u7684<code>pyplot<\/code>\u6a21\u5757\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2aMATLAB\u98ce\u683c\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u5404\u79cd\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61\u5e76\u7ed8\u5236\u5706<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>Circle<\/code>\u7c7b\u6765\u7ed8\u5236\u5706\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>add_patch<\/code>\u65b9\u6cd5\u5c06\u5706\u6dfb\u52a0\u5230\u7ed8\u56fe\u5bf9\u8c61\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<h2><strong>\u521b\u5efa\u56db\u4e2a\u5706<\/strong><\/h2>\n<p>circle1 = plt.Circle((0.2, 0.5), 0.1, color=&#39;blue&#39;, fill=False)<\/p>\n<p>circle2 = plt.Circle((0.4, 0.5), 0.1, color=&#39;green&#39;, fill=False)<\/p>\n<p>circle3 = plt.Circle((0.6, 0.5), 0.1, color=&#39;red&#39;, fill=False)<\/p>\n<p>circle4 = plt.Circle((0.8, 0.5), 0.1, color=&#39;yellow&#39;, fill=False)<\/p>\n<h2><strong>\u6dfb\u52a0\u5706\u5230\u7ed8\u56fe\u5bf9\u8c61\u4e2d<\/strong><\/h2>\n<p>ax.add_patch(circle1)<\/p>\n<p>ax.add_patch(circle2)<\/p>\n<p>ax.add_patch(circle3)<\/p>\n<p>ax.add_patch(circle4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u7f6e\u7ed8\u56fe\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u7ed8\u56fe\u66f4\u52a0\u7f8e\u89c2\u548c\u7b26\u5408\u8981\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e9b\u7ed8\u56fe\u5c5e\u6027\uff0c\u4f8b\u5982\u5750\u6807\u8f74\u8303\u56f4\u3001\u6bd4\u4f8b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/p>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6bd4\u4f8b\u4e3a1:1<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4ee3\u7801\u5b8c\u6574\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5728\u4e00\u8d77\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u521b\u5efa\u56db\u4e2a\u5706<\/strong><\/h2>\n<p>circle1 = plt.Circle((0.2, 0.5), 0.1, color=&#39;blue&#39;, fill=False)<\/p>\n<p>circle2 = plt.Circle((0.4, 0.5), 0.1, color=&#39;green&#39;, fill=False)<\/p>\n<p>circle3 = plt.Circle((0.6, 0.5), 0.1, color=&#39;red&#39;, fill=False)<\/p>\n<p>circle4 = plt.Circle((0.8, 0.5), 0.1, color=&#39;yellow&#39;, fill=False)<\/p>\n<h2><strong>\u6dfb\u52a0\u5706\u5230\u7ed8\u56fe\u5bf9\u8c61\u4e2d<\/strong><\/h2>\n<p>ax.add_patch(circle1)<\/p>\n<p>ax.add_patch(circle2)<\/p>\n<p>ax.add_patch(circle3)<\/p>\n<p>ax.add_patch(circle4)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6bd4\u4f8b\u4e3a1:1<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6df1\u5165\u7406\u89e3\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u5706\u7684\u586b\u5145\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>fill=False<\/code>\u53c2\u6570\u6765\u521b\u5efa\u4e0d\u586b\u5145\u7684\u5706\u3002\u5982\u679c\u5e0c\u671b\u586b\u5145\u5706\uff0c\u53ef\u4ee5\u5c06<code>fill<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>True<\/code>\uff0c\u5e76\u6307\u5b9a<code>facecolor<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u586b\u5145\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">circle1 = plt.Circle((0.2, 0.5), 0.1, color=&#39;blue&#39;, fill=True, facecolor=&#39;lightblue&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7ed8\u5236\u66f4\u591a\u590d\u6742\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u7ed8\u5236\u7b80\u5355\u7684\u5706\u5f62\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7ed8\u5236\u66f4\u52a0\u590d\u6742\u7684\u56fe\u5f62\uff0c\u4f8b\u5982\u692d\u5706\u3001\u77e9\u5f62\u3001\u591a\u8fb9\u5f62\u7b49\u3002\u901a\u8fc7\u7ec4\u5408\u4f7f\u7528\u4e0d\u540c\u7684\u7ed8\u5236\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u51fa\u4e30\u5bcc\u591a\u5f69\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u52a8\u6001\u4ea4\u4e92\u548c\u52a8\u753b<\/h4>\n<\/p>\n<p><p>Matplotlib\u8fd8\u652f\u6301\u521b\u5efa\u52a8\u6001\u4ea4\u4e92\u548c\u52a8\u753b\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>FuncAnimation<\/code>\u7c7b\u6765\u521b\u5efa\u52a8\u753b\uff0c\u4f7f\u56fe\u5f62\u968f\u65f6\u95f4\u53d8\u5316\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u7684\u53d8\u5316\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.animation as animation<\/p>\n<p>def update(frame):<\/p>\n<p>    circle1.set_radius(0.1 + 0.01 * frame)<\/p>\n<p>    return circle1,<\/p>\n<p>ani = animation.FuncAnimation(fig, update, frames=range(100), blit=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528Python\u548cMatplotlib\u5e93\u6765\u7ed8\u5236\u56db\u4e2a\u5706\u3002<strong>\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u65b9\u6cd5\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u6269\u5c55\u548c\u4fee\u6539\uff0c\u521b\u5efa\u66f4\u52a0\u590d\u6742\u548c\u7f8e\u89c2\u7684\u56fe\u5f62<\/strong>\u3002Matplotlib\u5e93\u7684\u5f3a\u5927\u529f\u80fd\u4f7f\u5f97\u5b83\u6210\u4e3a\u6570\u636e\u53ef\u89c6\u5316\u548c\u56fe\u5f62\u7ed8\u5236\u7684\u9996\u9009\u5de5\u5177\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u7ed8\u5236\u591a\u4e2a\u5706\u5f62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u7ed8\u5236\u5706\u5f62\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fMatplotlib\u3002\u901a\u8fc7\u4f7f\u7528Matplotlib\u7684<code>patches<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u7ed8\u5236\u591a\u4e2a\u5706\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\nimport matplotlib.patches as patches\n\nfig, ax = plt.subplots()\n# \u521b\u5efa\u56db\u4e2a\u5706\u5f62\ncircle1 = patches.Circle((1, 1), 0.5, color=&#39;blue&#39;, alpha=0.5)\ncircle2 = patches.Circle((2, 2), 0.5, color=&#39;red&#39;, alpha=0.5)\ncircle3 = patches.Circle((3, 1), 0.5, color=&#39;green&#39;, alpha=0.5)\ncircle4 = patches.Circle((2, 0), 0.5, color=&#39;yellow&#39;, alpha=0.5)\n\n# \u6dfb\u52a0\u5706\u5f62\u5230\u7ed8\u56fe\u533a\u57df\nax.add_patch(circle1)\nax.add_patch(circle2)\nax.add_patch(circle3)\nax.add_patch(circle4)\n\n# \u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4\nax.set_xlim(0, 4)\nax.set_ylim(0, 3)\nax.set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u7ed8\u5236\u5706\u5f62\u65f6\u9700\u8981\u8003\u8651\u54ea\u4e9b\u53c2\u6570\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u5706\u5f62\u65f6\uff0c\u4e3b\u8981\u9700\u8981\u5173\u6ce8\u5706\u5fc3\u7684\u5750\u6807\u3001\u534a\u5f84\u3001\u989c\u8272\u548c\u900f\u660e\u5ea6\u7b49\u53c2\u6570\u3002\u5706\u5fc3\u7684\u5750\u6807\u51b3\u5b9a\u4e86\u5706\u7684\u4f4d\u7f6e\uff0c\u534a\u5f84\u5219\u5f71\u54cd\u5706\u7684\u5927\u5c0f\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u989c\u8272\u548c\u900f\u660e\u5ea6\u6765\u589e\u5f3a\u56fe\u5f62\u7684\u89c6\u89c9\u6548\u679c\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u8bbe\u7f6e\u5408\u9002\u7684\u5750\u6807\u8f74\u8303\u56f4\uff0c\u4ee5\u4fbf\u5706\u5f62\u80fd\u591f\u6e05\u6670\u53ef\u89c1\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7ed8\u5236\u5706\u5f62\u7684\u5176\u4ed6\u5e93\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\u5916\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u5706\u5f62\uff0c\u4f8b\u5982Pygame\u3001Turtle\u548cPillow\u7b49\u3002Pygame\u66f4\u9002\u5408\u4e8e\u6e38\u620f\u5f00\u53d1\uff0c\u80fd\u591f\u5904\u7406\u52a8\u753b\u548c\u4ea4\u4e92\uff1bTurtle\u9002\u5408\u6559\u80b2\u76ee\u7684\uff0c\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1bPillow\u5219\u4e3b\u8981\u7528\u4e8e\u56fe\u50cf\u5904\u7406\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7ed8\u5236\u57fa\u672c\u7684\u56fe\u5f62\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7ed8\u5236\u7684\u5706\u5f62\u4e2d\u6dfb\u52a0\u6587\u672c\u6216\u6807\u7b7e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Matplotlib\u4e2d\u7684<code>text<\/code>\u65b9\u6cd5\u5728\u5706\u5f62\u9644\u8fd1\u6dfb\u52a0\u6587\u672c\u6216\u6807\u7b7e\u3002\u4f8b\u5982\uff0c\u5728\u7ed8\u5236\u5706\u5f62\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5728\u7279\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\uff1a<\/p>\n<pre><code class=\"language-python\">ax.text(1, 1, &#39;Circle 1&#39;, fontsize=12, ha=&#39;center&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8c03\u6574\u6587\u672c\u7684\u4f4d\u7f6e\u3001\u5b57\u4f53\u5927\u5c0f\u548c\u5bf9\u9f50\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u597d\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7ed8\u5236\u56db\u4e2a\u5706\u7684\u6b65\u9aa4 \u5728Python\u4e2d\u7ed8\u5236\u56fe\u5f62\u901a\u5e38\u4f7f\u7528\u7684\u5e93\u662fMatplotlib\uff0c\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927 [&hellip;]","protected":false},"author":3,"featured_media":1053785,"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\/1053779"}],"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=1053779"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053779\/revisions"}],"predecessor-version":[{"id":1053788,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053779\/revisions\/1053788"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1053785"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1053779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1053779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1053779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}