{"id":1090709,"date":"2025-01-08T14:04:58","date_gmt":"2025-01-08T06:04:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1090709.html"},"modified":"2025-01-08T14:05:00","modified_gmt":"2025-01-08T06:05:00","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e7%94%bb%e4%b8%80%e4%b8%aa%e5%9c%86-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1090709.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u753b\u4e00\u4e2a\u5706"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203242\/f33cdc75-896c-4b89-a38b-40f3f3cec819.webp\" alt=\"\u5982\u4f55\u5229\u7528python\u753b\u4e00\u4e2a\u5706\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5229\u7528matplotlib\u3001turtle\u3001pygame\u7b49\u5e93\u53ef\u4ee5\u753b\u4e00\u4e2a\u5706\u3002<\/strong> \u5176\u4e2d\uff0cmatplotlib\u5e93\u662f\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5177\u6709\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\u548c\u7b80\u5355\u7684API\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528\u8fd9\u51e0\u4e2a\u5e93\u5728Python\u4e2d\u753b\u4e00\u4e2a\u5706\uff0c\u5e76\u7ed9\u51fa\u6bcf\u79cd\u65b9\u6cd5\u7684\u8be6\u7ec6\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u5706<\/strong><\/h2>\n<p><p>matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u4e00\u4e2a\u5706\u3002<\/p>\n<\/p>\n<p><h2>1.1 \u5b89\u88c5matplotlib\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528matplotlib\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8be5\u5e93\u3002\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><h2>1.2 \u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u5706<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u5706\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165matplotlib\u5e93\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u548c\u4e00\u4e2a\u5b50\u56fe\u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528<code>Circle<\/code>\u7c7b\u521b\u5efa\u4e00\u4e2a\u5706\u5f62\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5b50\u56fe\u5bf9\u8c61\u4e2d\u3002<\/li>\n<li>\u8bbe\u7f6e\u5b50\u56fe\u5bf9\u8c61\u7684\u5750\u6807\u8f74\u6bd4\u4f8b\u3002<\/li>\n<li>\u663e\u793a\u56fe\u5f62\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\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<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61\u548c\u5b50\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u521b\u5efa\u5706\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>circle = plt.Circle((0, 0), 1, color=&#39;b&#39;, fill=False)<\/p>\n<h2><strong>\u5c06\u5706\u5f62\u5bf9\u8c61\u6dfb\u52a0\u5230\u5b50\u56fe\u5bf9\u8c61\u4e2d<\/strong><\/h2>\n<p>ax.add_artist(circle)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b50\u56fe\u5bf9\u8c61\u7684\u5750\u6807\u8f74\u6bd4\u4f8b<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#39;, &#39;box&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.xlim(-1.5, 1.5)<\/p>\n<p>plt.ylim(-1.5, 1.5)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.title(&quot;Circle using Matplotlib&quot;)<\/p>\n<p>plt.xlabel(&quot;X-axis&quot;)<\/p>\n<p>plt.ylabel(&quot;Y-axis&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528turtle\u5e93\u7ed8\u5236\u5706<\/strong><\/h2>\n<p><p>turtle\u5e93\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u521d\u5b66\u8005\u4f7f\u7528\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528turtle\u5e93\u7ed8\u5236\u4e00\u4e2a\u5706\u3002<\/p>\n<\/p>\n<p><h2>2.1 \u4f7f\u7528turtle\u5e93\u7ed8\u5236\u5706<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528turtle\u5e93\u7ed8\u5236\u5706\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165turtle\u5e93\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u6d77\u9f9f\u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528<code>circle<\/code>\u65b9\u6cd5\u7ed8\u5236\u4e00\u4e2a\u5706\u3002<\/li>\n<li>\u5b8c\u6210\u7ed8\u56fe\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efa\u6d77\u9f9f\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<h2><strong>\u7ed8\u5236\u5706<\/strong><\/h2>\n<p>t.circle(100)<\/p>\n<h2><strong>\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528pygame\u5e93\u7ed8\u5236\u5706<\/strong><\/h2>\n<p><p>pygame\u662f\u4e00\u4e2a\u7528\u4e8e\u5f00\u53d12D\u6e38\u620f\u7684Python\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7ed8\u56fe\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528pygame\u5e93\u7ed8\u5236\u4e00\u4e2a\u5706\u3002<\/p>\n<\/p>\n<p><h2>3.1 \u5b89\u88c5pygame\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528pygame\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8be5\u5e93\u3002\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 pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.2 \u4f7f\u7528pygame\u5e93\u7ed8\u5236\u5706<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528pygame\u5e93\u7ed8\u5236\u5706\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165pygame\u5e93\u3002<\/li>\n<li>\u521d\u59cb\u5316pygame\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3\u5bf9\u8c61\u3002<\/li>\n<li>\u8bbe\u7f6e\u7a97\u53e3\u7684\u80cc\u666f\u989c\u8272\u3002<\/li>\n<li>\u4f7f\u7528<code>draw.circle<\/code>\u65b9\u6cd5\u7ed8\u5236\u4e00\u4e2a\u5706\u3002<\/li>\n<li>\u66f4\u65b0\u7a97\u53e3\u3002<\/li>\n<li>\u5904\u7406\u4e8b\u4ef6\u5faa\u73af\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<h2><strong>\u521d\u59cb\u5316pygame<\/strong><\/h2>\n<p>pygame.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>size = (500, 500)<\/p>\n<p>screen = pygame.display.set_mode(size)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898<\/strong><\/h2>\n<p>pygame.display.set_caption(&quot;Circle using Pygame&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u80cc\u666f\u989c\u8272<\/strong><\/h2>\n<p>white = (255, 255, 255)<\/p>\n<p>screen.fill(white)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5706\u7684\u989c\u8272\u548c\u4f4d\u7f6e<\/strong><\/h2>\n<p>blue = (0, 0, 255)<\/p>\n<p>center = (250, 250)<\/p>\n<p>radius = 100<\/p>\n<h2><strong>\u7ed8\u5236\u5706<\/strong><\/h2>\n<p>pygame.draw.circle(screen, blue, center, radius)<\/p>\n<h2><strong>\u66f4\u65b0\u7a97\u53e3<\/strong><\/h2>\n<p>pygame.display.flip()<\/p>\n<h2><strong>\u5904\u7406\u4e8b\u4ef6\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<h2><strong>\u9000\u51fapygame<\/strong><\/h2>\n<p>pygame.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528Python\u4e2d\u7684matplotlib\u3001turtle\u3001pygame\u7b49\u5e93\u7ed8\u5236\u4e00\u4e2a\u5706\u3002<strong>matplotlib\u5e93\u9002\u5408\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u4f7f\u7528\u7b80\u5355<\/strong>\uff0c<strong>turtle\u5e93\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u548c\u4f7f\u7528<\/strong>\uff0c<strong>pygame\u5e93\u5219\u66f4\u9002\u5408\u7528\u4e8e\u5f00\u53d12D\u6e38\u620f<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Python\u4e2d\u7ed8\u5236\u5706\u5f62\u56fe\u5f62\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u5feb\u901f\u638c\u63e1\u5728Python\u4e2d\u7ed8\u5236\u5706\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u4e0d\u540c\u5927\u5c0f\u7684\u5706\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u4e0d\u540c\u5927\u5c0f\u7684\u5706\u3002\u901a\u8fc7\u8c03\u6574\u5706\u5fc3\u7684\u5750\u6807\u548c\u534a\u5f84\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u5c3a\u5bf8\u7684\u5706\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Circle<\/code>\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u7279\u5b9a\u534a\u5f84\u7684\u5706\uff0c\u5e76\u901a\u8fc7<code>add_patch<\/code>\u65b9\u6cd5\u5c06\u5176\u6dfb\u52a0\u5230\u7ed8\u56fe\u533a\u57df\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nfig, ax = plt.subplots()\ncircle = plt.Circle((0.5, 0.5), 0.3, color=&#39;blue&#39;, fill=False)\nax.add_patch(circle)\nax.set_xlim(0, 1)\nax.set_ylim(0, 1)\nplt.gca().set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u7ed8\u5236\u5706\u65f6\u5982\u4f55\u9009\u62e9\u989c\u8272\u548c\u586b\u5145\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>matplotlib<\/code>\u7ed8\u5236\u5706\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>color<\/code>\u53c2\u6570\u8bbe\u7f6e\u5706\u7684\u989c\u8272\uff0c\u4f7f\u7528<code>fill<\/code>\u53c2\u6570\u63a7\u5236\u662f\u5426\u586b\u5145\u989c\u8272\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e<code>fill=True<\/code>\u5c06\u5706\u586b\u5145\u4e3a\u6307\u5b9a\u989c\u8272\uff0c\u8bbe\u7f6e<code>fill=False<\/code>\u5219\u53ea\u7ed8\u5236\u5706\u7684\u8fb9\u6846\u3002\u53ef\u4ee5\u4f7f\u7528\u5e38\u89c1\u7684\u989c\u8272\u540d\u79f0\u6216RGB\u503c\u6765\u5b9a\u4e49\u989c\u8272\uff0c\u63d0\u5347\u56fe\u5f62\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u591a\u4e2a\u5706\u5e76\u63a7\u5236\u5b83\u4eec\u7684\u5e03\u5c40\uff1f<\/strong><br \/>\u7ed8\u5236\u591a\u4e2a\u5706\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u521b\u5efa\u4e0d\u540c\u4f4d\u7f6e\u548c\u5927\u5c0f\u7684\u5706\u3002\u4f7f\u7528\u5750\u6807\u8f74\u7684<code>add_patch<\/code>\u65b9\u6cd5\u5c06\u6bcf\u4e2a\u5706\u6dfb\u52a0\u5230\u7ed8\u56fe\u4e2d\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u6bcf\u4e2a\u5706\u7684\u5750\u6807\u548c\u534a\u5f84\uff0c\u4ee5\u5b9e\u73b0\u6240\u9700\u7684\u5e03\u5c40\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u540c\u4e00\u5f20\u56fe\u4e2d\u7ed8\u5236\u591a\u4e2a\u5706\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nfig, ax = plt.subplots()\nfor i in range(1, 4):\n    circle = plt.Circle((i * 0.3, 0.5), 0.1 * i, color=&#39;red&#39;, fill=True)\n    ax.add_patch(circle)\n\nax.set_xlim(0, 1.5)\nax.set_ylim(0, 1)\nplt.gca().set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)\nplt.show()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5229\u7528matplotlib\u3001turtle\u3001pygame\u7b49\u5e93\u53ef\u4ee5\u753b\u4e00\u4e2a\u5706\u3002 \u5176\u4e2d\uff0cmatplot [&hellip;]","protected":false},"author":3,"featured_media":1090719,"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\/1090709"}],"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=1090709"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090709\/revisions"}],"predecessor-version":[{"id":1090721,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090709\/revisions\/1090721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1090719"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1090709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1090709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1090709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}