{"id":1063191,"date":"2024-12-31T15:58:32","date_gmt":"2024-12-31T07:58:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1063191.html"},"modified":"2024-12-31T15:58:35","modified_gmt":"2024-12-31T07:58:35","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e4%b8%89%e4%b8%aa%e5%9c%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1063191.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u4e09\u4e2a\u5706"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/83e2bcb9-e689-42cb-bb7e-2cc54f299ec8.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u753b\u4e09\u4e2a\u5706\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7ed8\u5236\u4e09\u4e2a\u5706\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528matplotlib\u3001pygame\u3001turtle\u7b49\u5e93\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528matplotlib\u6765\u7ed8\u5236\u4e09\u4e2a\u5706\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u5404\u79cd\u5de5\u5177\u548c\u529f\u80fd\u3002\u5728Python\u4e2d\u4f7f\u7528matplotlib\u7ed8\u5236\u5706\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u51e0\u884c\u4ee3\u7801\u5373\u53ef\u5b9e\u73b0\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u5236\u5706\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3002\u9996\u5148\u662fmatplotlib\u5e93\uff0c\u5b83\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u5f3a\u5927\u5de5\u5177\u3002\u6211\u4eec\u8fd8\u9700\u8981numpy\u5e93\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u5bfc\u5165\u8fd9\u4e9b\u5e93\u7684\u4ee3\u7801\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\u5706\u7684\u53c2\u6570<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u5706\u7684\u53c2\u6570\u3002\u5706\u7684\u53c2\u6570\u5305\u62ec\u5706\u5fc3\u7684\u5750\u6807\u548c\u534a\u5f84\u3002\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4eec\u5b9a\u4e49\u4e09\u4e2a\u5706\u7684\u5706\u5fc3\u5206\u522b\u5728(0, 0)\u3001(1, 1)\u548c(2, 2)\uff0c\u534a\u5f84\u5206\u522b\u4e3a1\u30010.5\u548c0.75\u3002\u4ee5\u4e0b\u662f\u5b9a\u4e49\u8fd9\u4e9b\u53c2\u6570\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">circle1 = {&#39;center&#39;: (0, 0), &#39;radius&#39;: 1}<\/p>\n<p>circle2 = {&#39;center&#39;: (1, 1), &#39;radius&#39;: 0.5}<\/p>\n<p>circle3 = {&#39;center&#39;: (2, 2), &#39;radius&#39;: 0.75}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61\u3002matplotlib\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u51fd\u6570<code>plt.figure()<\/code>\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u7ed8\u5236\u5706<\/p>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u5b9a\u4e49\u4e86\u5706\u7684\u53c2\u6570\uff0c\u5e76\u521b\u5efa\u4e86\u7ed8\u56fe\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u7ed8\u5236\u5706\u4e86\u3002matplotlib\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u7c7b<code>plt.Circle<\/code>\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7ed8\u5236\u5706\u3002\u4ee5\u4e0b\u662f\u7ed8\u5236\u4e09\u4e2a\u5706\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">circle1_plot = plt.Circle(circle1[&#39;center&#39;], circle1[&#39;radius&#39;], color=&#39;blue&#39;, fill=False)<\/p>\n<p>circle2_plot = plt.Circle(circle2[&#39;center&#39;], circle2[&#39;radius&#39;], color=&#39;green&#39;, fill=False)<\/p>\n<p>circle3_plot = plt.Circle(circle3[&#39;center&#39;], circle3[&#39;radius&#39;], color=&#39;red&#39;, fill=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6dfb\u52a0\u5706\u5230\u7ed8\u56fe\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u7ed8\u5236\u5b8c\u5706\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230\u7ed8\u56fe\u5bf9\u8c61\u4e2d\u3002\u4ee5\u4e0b\u662f\u5c06\u4e09\u4e2a\u5706\u6dfb\u52a0\u5230\u7ed8\u56fe\u5bf9\u8c61\u4e2d\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.add_patch(circle1_plot)<\/p>\n<p>ax.add_patch(circle2_plot)<\/p>\n<p>ax.add_patch(circle3_plot)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u8bbe\u7f6e\u7ed8\u56fe\u5bf9\u8c61\u7684\u5c5e\u6027<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u7ed8\u56fe\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u5305\u62ec\u5750\u6807\u8f74\u7684\u8303\u56f4\u3001\u6807\u9898\u7b49\u3002\u4ee5\u4e0b\u662f\u8bbe\u7f6e\u8fd9\u4e9b\u5c5e\u6027\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_xlim(-1, 3)<\/p>\n<p>ax.set_ylim(-1, 3)<\/p>\n<p>ax.set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)<\/p>\n<p>plt.title(&#39;Three Circles&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u663e\u793a\u7ed8\u56fe<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u663e\u793a\u7ed8\u56fe\u3002matplotlib\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u51fd\u6570<code>plt.show()<\/code>\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u663e\u793a\u7ed8\u56fe\u3002\u4ee5\u4e0b\u662f\u663e\u793a\u7ed8\u56fe\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u81f3\u6b64\uff0c\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86\u4f7f\u7528matplotlib\u7ed8\u5236\u4e09\u4e2a\u5706\u7684\u6240\u6709\u6b65\u9aa4\u3002\u5b8c\u6574\u7684\u4ee3\u7801\u5982\u4e0b\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\u5706\u7684\u53c2\u6570<\/strong><\/h2>\n<p>circle1 = {&#39;center&#39;: (0, 0), &#39;radius&#39;: 1}<\/p>\n<p>circle2 = {&#39;center&#39;: (1, 1), &#39;radius&#39;: 0.5}<\/p>\n<p>circle3 = {&#39;center&#39;: (2, 2), &#39;radius&#39;: 0.75}<\/p>\n<h2><strong>\u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u5706<\/strong><\/h2>\n<p>circle1_plot = plt.Circle(circle1[&#39;center&#39;], circle1[&#39;radius&#39;], color=&#39;blue&#39;, fill=False)<\/p>\n<p>circle2_plot = plt.Circle(circle2[&#39;center&#39;], circle2[&#39;radius&#39;], color=&#39;green&#39;, fill=False)<\/p>\n<p>circle3_plot = plt.Circle(circle3[&#39;center&#39;], circle3[&#39;radius&#39;], color=&#39;red&#39;, fill=False)<\/p>\n<h2><strong>\u6dfb\u52a0\u5706\u5230\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>ax.add_patch(circle1_plot)<\/p>\n<p>ax.add_patch(circle2_plot)<\/p>\n<p>ax.add_patch(circle3_plot)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7ed8\u56fe\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/h2>\n<p>ax.set_xlim(-1, 3)<\/p>\n<p>ax.set_ylim(-1, 3)<\/p>\n<p>ax.set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)<\/p>\n<p>plt.title(&#39;Three Circles&#39;)<\/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\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u548cmatplotlib\u5e93\u8f7b\u677e\u7ed8\u5236\u4e09\u4e2a\u5706\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u5408\u521d\u5b66\u8005\u548c\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u591a\u4e2a\u5706\u5f62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u7ed8\u5236\u5706\u5f62\uff0c\u5982Matplotlib\u548cPygame\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u3002\u5982\u679c\u9700\u8981\u7b80\u5355\u7684\u56fe\u5f62\u5c55\u793a\uff0cMatplotlib\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e09\u4e2a\u5706\u5f62\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62\nfig, ax = plt.subplots()\n\n# \u7ed8\u5236\u4e09\u4e2a\u5706\u5f62\ncircle1 = plt.Circle((0.2, 0.5), 0.1, color=&#39;blue&#39;, alpha=0.5)\ncircle2 = plt.Circle((0.5, 0.5), 0.1, color=&#39;red&#39;, alpha=0.5)\ncircle3 = plt.Circle((0.8, 0.5), 0.1, color=&#39;green&#39;, alpha=0.5)\n\n# \u6dfb\u52a0\u5706\u5f62\u5230\u56fe\u5f62\nax.add_artist(circle1)\nax.add_artist(circle2)\nax.add_artist(circle3)\n\n# \u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4\nax.set_xlim(0, 1)\nax.set_ylim(0, 1)\n\n# \u663e\u793a\u56fe\u5f62\nplt.gca().set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u7ed8\u56fe\u65f6\u53ef\u4ee5\u9009\u62e9\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>Python\u6709\u8bb8\u591a\u5e93\u53ef\u4f9b\u7ed8\u56fe\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ecMatplotlib\u3001Seaborn\u548cPygame\u3002Matplotlib\u4ee5\u5176\u7b80\u5355\u6613\u7528\u800c\u53d7\u5230\u5e7f\u6cdb\u6b22\u8fce\uff0c\u7279\u522b\u9002\u5408\u6570\u636e\u53ef\u89c6\u5316\uff1bSeaborn\u5219\u5728\u7edf\u8ba1\u56fe\u5f62\u65b9\u9762\u8868\u73b0\u51fa\u8272\uff1b\u800cPygame\u9002\u5408\u6e38\u620f\u5f00\u53d1\u53ca\u66f4\u590d\u6742\u7684\u56fe\u5f62\u5e94\u7528\u3002\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u80fd\u591f\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p><strong>\u7ed8\u5236\u5706\u5f62\u65f6\u5982\u4f55\u8bbe\u7f6e\u989c\u8272\u548c\u900f\u660e\u5ea6\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>color<\/code>\u53c2\u6570\u8bbe\u7f6e\u5706\u7684\u989c\u8272\uff0c\u4f7f\u7528<code>alpha<\/code>\u53c2\u6570\u6765\u8c03\u6574\u900f\u660e\u5ea6\uff0c\u8303\u56f4\u4ece0\uff08\u5b8c\u5168\u900f\u660e\uff09\u52301\uff08\u5b8c\u5168\u4e0d\u900f\u660e\uff09\u3002\u4f8b\u5982\uff0c\u5728\u7ed8\u5236\u5706\u65f6\uff0c\u53ef\u4ee5\u8fd9\u6837\u8bbe\u7f6e\uff1a<code>plt.Circle((x, y), radius, color=&#39;blue&#39;, alpha=0.5)<\/code>\uff0c\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u84dd\u8272\u4e14\u534a\u900f\u660e\u7684\u5706\u5f62\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7ed8\u5236\u4e09\u4e2a\u5706\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528matplotlib\u3001pygame\u3001turtle\u7b49\u5e93\u3002\u5728\u672c\u6587\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1063199,"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\/1063191"}],"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=1063191"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063191\/revisions"}],"predecessor-version":[{"id":1063202,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063191\/revisions\/1063202"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1063199"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1063191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1063191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1063191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}