{"id":1112153,"date":"2025-01-08T17:35:51","date_gmt":"2025-01-08T09:35:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112153.html"},"modified":"2025-01-08T17:35:53","modified_gmt":"2025-01-08T09:35:53","slug":"python-%e5%a6%82%e4%bd%95%e7%94%bb%e4%b8%80%e4%b8%aa%e8%93%9d%e8%89%b2%e7%9a%84%e5%9c%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112153.html","title":{"rendered":"python \u5982\u4f55\u753b\u4e00\u4e2a\u84dd\u8272\u7684\u5706"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074319\/659340d3-6841-42fc-bb44-56f8b851d98c.webp\" alt=\"python \u5982\u4f55\u753b\u4e00\u4e2a\u84dd\u8272\u7684\u5706\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u753b\u4e00\u4e2a\u84dd\u8272\u7684\u5706\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528matplotlib\u3001Pygame\u3001Turtle\u7b49\u5e93\u3002<\/strong> \u5176\u4e2d\uff0cmatplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u7ed8\u5236\u9ad8\u8d28\u91cf\u7684\u56fe\u5f62\uff0c\u800cPygame\u5219\u9002\u5408\u7528\u4e8e\u5236\u4f5c\u7b80\u5355\u7684\u6e38\u620f\u548c\u52a8\u753b\u3002Turtle\u5e93\u5219\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u7ed8\u56fe\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528matplotlib\u7ed8\u5236\u84dd\u8272\u5706\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528matplotlib\u7ed8\u5236\u84dd\u8272\u5706<\/h3>\n<\/p>\n<p><p>matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u76842D\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8ePython\u811a\u672c\u3001Jupyter Notebook\u548cWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528matplotlib\u7ed8\u5236\u84dd\u8272\u5706\u975e\u5e38\u7b80\u5355\uff0c\u4e0b\u9762\u662f\u8be6\u7ec6\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5matplotlib<\/h4>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\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><h4>2\u3001\u7ed8\u5236\u84dd\u8272\u5706<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4f7f\u7528matplotlib\u7ed8\u5236\u4e00\u4e2a\u84dd\u8272\u7684\u5706\u3002\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<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u84dd\u8272\u7684\u5706<\/strong><\/h2>\n<p>circle = plt.Circle((0.5, 0.5), 0.3, color=&#39;blue&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u5706\u5230\u56fe\u5f62\u4e2d<\/strong><\/h2>\n<p>ax.add_artist(circle)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim(0, 1)<\/p>\n<p>ax.set_ylim(0, 1)<\/p>\n<h2><strong>\u4fdd\u6301\u957f\u5bbd\u6bd4\u76f8\u540c<\/strong><\/h2>\n<p>ax.set_aspect(&#39;equal&#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>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86matplotlib.pyplot\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u548c\u5750\u6807\u8f74\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>plt.Circle<\/code>\u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u84dd\u8272\u7684\u5706\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u56fe\u5f62\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u5750\u6807\u8f74\u7684\u8303\u56f4\u548c\u957f\u5bbd\u6bd4\uff0c\u786e\u4fdd\u5706\u5f62\u4e0d\u4f1a\u88ab\u626d\u66f2\uff0c\u6700\u540e\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pygame\u7ed8\u5236\u84dd\u8272\u5706<\/h3>\n<\/p>\n<p><p>Pygame\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684Python\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u5f00\u53d1\u89c6\u9891\u6e38\u620f\u3002\u5b83\u5305\u542b\u8ba1\u7b97\u673a\u56fe\u5f62\u548c\u58f0\u97f3\u5e93\u3002\u4f7f\u7528Pygame\u7ed8\u5236\u84dd\u8272\u5706\u9002\u5408\u7528\u4e8e\u5236\u4f5c\u7b80\u5355\u7684\u6e38\u620f\u548c\u52a8\u753b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Pygame<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Pygame\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\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><h4>2\u3001\u7ed8\u5236\u84dd\u8272\u5706<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Pygame\u7ed8\u5236\u84dd\u8272\u5706\u7684\u4ee3\u7801\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\u5927\u5c0f<\/strong><\/h2>\n<p>size = (400, 400)<\/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;Blue Circle&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u989c\u8272<\/strong><\/h2>\n<p>blue = (0, 0, 255)<\/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>    # \u586b\u5145\u80cc\u666f\u989c\u8272<\/p>\n<p>    screen.fill((255, 255, 255))<\/p>\n<p>    # \u7ed8\u5236\u84dd\u8272\u5706<\/p>\n<p>    pygame.draw.circle(screen, blue, (200, 200), 100)<\/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\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86pygame\u548csys\u6a21\u5757\uff0c\u7136\u540e\u521d\u59cb\u5316Pygame\u5e76\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\u548c\u6807\u9898\u3002\u63a5\u7740\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u84dd\u8272\u7684RGB\u503c\uff0c\u5e76\u5728\u4e3b\u5faa\u73af\u4e2d\u5904\u7406\u4e8b\u4ef6\u3002\u5982\u679c\u7528\u6237\u5173\u95ed\u7a97\u53e3\uff0c\u7a0b\u5e8f\u5c06\u9000\u51fa\u3002\u5728\u4e3b\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u586b\u5145\u80cc\u666f\u989c\u8272\uff0c\u5e76\u4f7f\u7528<code>pygame.draw.circle<\/code>\u65b9\u6cd5\u7ed8\u5236\u84dd\u8272\u7684\u5706\uff0c\u6700\u540e\u66f4\u65b0\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Turtle\u7ed8\u5236\u84dd\u8272\u5706<\/h3>\n<\/p>\n<p><p>Turtle\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u7ed8\u56fe\u3002\u4f7f\u7528Turtle\u7ed8\u5236\u84dd\u8272\u5706\u975e\u5e38\u76f4\u89c2\uff0c\u4e0b\u9762\u662f\u8be6\u7ec6\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Turtle<\/h4>\n<\/p>\n<p><p>Turtle\u5e93\u662fPython\u5185\u7f6e\u7684\u5e93\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed8\u5236\u84dd\u8272\u5706<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Turtle\u7ed8\u5236\u84dd\u8272\u5706\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3<\/strong><\/h2>\n<p>window = turtle.Screen()<\/p>\n<p>window.title(&quot;Blue Circle&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>pen = turtle.Turtle()<\/p>\n<h2><strong>\u8bbe\u7f6e\u989c\u8272<\/strong><\/h2>\n<p>pen.color(&quot;blue&quot;)<\/p>\n<h2><strong>\u7ed8\u5236\u5706\u5f62<\/strong><\/h2>\n<p>pen.circle(100)<\/p>\n<h2><strong>\u9690\u85cfTurtle<\/strong><\/h2>\n<p>pen.hideturtle()<\/p>\n<h2><strong>\u4fdd\u6301\u7a97\u53e3\u6253\u5f00<\/strong><\/h2>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86turtle\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u7a97\u53e3\u5e76\u8bbe\u7f6e\u6807\u9898\u3002\u63a5\u7740\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aTurtle\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6e\u989c\u8272\u4e3a\u84dd\u8272\u3002\u4f7f\u7528<code>pen.circle<\/code>\u65b9\u6cd5\u7ed8\u5236\u4e00\u4e2a\u534a\u5f84\u4e3a100\u7684\u5706\u5f62\uff0c\u6700\u540e\u9690\u85cfTurtle\u5e76\u4fdd\u6301\u7a97\u53e3\u6253\u5f00\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528Python\u7ed8\u5236\u4e00\u4e2a\u84dd\u8272\u7684\u5706\u3002<strong>\u4f7f\u7528matplotlib\u7ed8\u5236\u9ad8\u8d28\u91cf\u56fe\u5f62\u3001\u4f7f\u7528Pygame\u5236\u4f5c\u7b80\u5355\u7684\u6e38\u620f\u548c\u52a8\u753b\u3001\u4f7f\u7528Turtle\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u7ed8\u56fe<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7ed8\u56fe\u8fc7\u7a0b\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff01<\/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\u5706\u5f62\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u7ed8\u5236\u4e00\u4e2a\u5706\u5f62\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\uff0c\u5982Matplotlib\u3002\u901a\u8fc7\u7b80\u5355\u7684\u51e0\u884c\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u84dd\u8272\u7684\u5706\u5f62\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>plt.Circle<\/code>\u65b9\u6cd5\uff0c\u8bbe\u7f6e\u5706\u5fc3\u548c\u534a\u5f84\uff0c\u5e76\u6307\u5b9a\u989c\u8272\u4e3a\u84dd\u8272\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>add_artist<\/code>\u65b9\u6cd5\u5c06\u5706\u5f62\u6dfb\u52a0\u5230\u56fe\u5f62\u4e2d\uff0c\u7136\u540e\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<p><strong>\u4f7f\u7528Matplotlib\u7ed8\u5236\u84dd\u8272\u5706\u5f62\u9700\u8981\u54ea\u4e9b\u6b65\u9aa4\uff1f<\/strong><br \/>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\uff0c\u5e76\u5bfc\u5165\u76f8\u5173\u6a21\u5757\u3002\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u548c\u5750\u6807\u8f74\uff0c\u5b9a\u4e49\u5706\u7684\u4e2d\u5fc3\u548c\u534a\u5f84\u3002\u901a\u8fc7<code>plt.Circle<\/code>\u521b\u5efa\u5706\u5f62\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6e<code>color<\/code>\u53c2\u6570\u4e3a&#39;blue&#39;\u6765\u6307\u5b9a\u989c\u8272\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>plt.xlim<\/code>\u548c<code>plt.ylim<\/code>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4\uff0c\u786e\u4fdd\u5706\u5f62\u5b8c\u5168\u663e\u793a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u7ed8\u5236\u5706\u5f62\u65f6\u5982\u4f55\u63a7\u5236\u5706\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574<code>plt.Circle<\/code>\u4e2d\u7684<code>radius<\/code>\u53c2\u6570\u6765\u63a7\u5236\u5706\u7684\u5927\u5c0f\uff0c<code>center<\/code>\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\u5706\u5fc3\u7684\u4f4d\u7f6e\u3002\u5706\u5fc3\u7684\u5750\u6807\u901a\u5e38\u4ee5\u5143\u7ec4\u5f62\u5f0f\u8f93\u5165\uff0c\u4f8b\u5982<code>(x, y)<\/code>\u3002\u901a\u8fc7\u8c03\u6574\u8fd9\u4e9b\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u7ed8\u5236\u51fa\u4e0d\u540c\u5927\u5c0f\u548c\u4f4d\u7f6e\u7684\u5706\u5f62\uff0c\u4ee5\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u753b\u4e00\u4e2a\u84dd\u8272\u7684\u5706\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528matplotlib\u3001Pygame\u3001Turtle [&hellip;]","protected":false},"author":3,"featured_media":1112156,"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\/1112153"}],"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=1112153"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112153\/revisions"}],"predecessor-version":[{"id":1112158,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112153\/revisions\/1112158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112156"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}