{"id":1097574,"date":"2025-01-08T15:13:11","date_gmt":"2025-01-08T07:13:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097574.html"},"modified":"2025-01-08T15:13:13","modified_gmt":"2025-01-08T07:13:13","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e7%94%bb%e4%b8%80%e4%b8%aa%e7%ac%91%e8%84%b8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097574.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u753b\u4e00\u4e2a\u7b11\u8138"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212304\/b1f651de-f400-446e-8e3c-73cd2b8991cc.webp\" alt=\"\u5982\u4f55\u5229\u7528python\u753b\u4e00\u4e2a\u7b11\u8138\" \/><\/p>\n<p><p> <strong>\u8981\u5229\u7528Python\u753b\u4e00\u4e2a\u7b11\u8138\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u56fe\u5f62\u5e93\uff0c\u4f8b\u5982matplotlib\u548cturtle\u3002\u901a\u8fc7\u8c03\u7528\u8fd9\u4e9b\u5e93\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7ed8\u5236\u51fa\u5404\u79cd\u56fe\u5f62\u5143\u7d20\u5e76\u7ec4\u5408\u6210\u7b11\u8138\u56fe\u6848\u3002\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u8bbe\u7f6e\u7ed8\u56fe\u7a97\u53e3\u3001\u7ed8\u5236\u7b11\u8138\u7684\u8138\u90e8\u8f6e\u5ed3\u3001\u7ed8\u5236\u773c\u775b\u548c\u5634\u5df4\u3002<\/strong>\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u4e2a\u5e93\u5206\u522b\u5b9e\u73b0\u4e00\u4e2a\u7b11\u8138\u56fe\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Matplotlib\u7ed8\u5236\u7b11\u8138<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u5404\u79cd\u56fe\u8868\u548c\u56fe\u5f62\u3002\u4e0b\u9762\u662f\u4f7f\u7528Matplotlib\u7ed8\u5236\u7b11\u8138\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5bfc\u5165Matplotlib\u5e93\u4e2d\u7684pyplot\u6a21\u5757\u3002<\/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><h4>2\u3001\u8bbe\u7f6e\u7ed8\u56fe\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>plt.figure()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ed8\u56fe\u7a97\u53e3\uff0c\u5e76\u8bbe\u7f6e\u7ed8\u56fe\u7a97\u53e3\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed8\u5236\u8138\u90e8\u8f6e\u5ed3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>plt.Circle()<\/code>\u51fd\u6570\u7ed8\u5236\u4e00\u4e2a\u5706\u5f62\uff0c\u4f5c\u4e3a\u7b11\u8138\u7684\u8138\u90e8\u8f6e\u5ed3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">face = plt.Circle((0.5, 0.5), 0.4, color=&#39;yellow&#39;, ec=&#39;black&#39;)<\/p>\n<p>ax.add_patch(face)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u7ed8\u5236\u773c\u775b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>plt.Circle()<\/code>\u51fd\u6570\u7ed8\u5236\u4e24\u4e2a\u8f83\u5c0f\u7684\u5706\u5f62\uff0c\u5206\u522b\u4f5c\u4e3a\u7b11\u8138\u7684\u773c\u775b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">left_eye = plt.Circle((0.35, 0.65), 0.05, color=&#39;black&#39;)<\/p>\n<p>right_eye = plt.Circle((0.65, 0.65), 0.05, color=&#39;black&#39;)<\/p>\n<p>ax.add_patch(left_eye)<\/p>\n<p>ax.add_patch(right_eye)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u7ed8\u5236\u5634\u5df4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>plt.plot()<\/code>\u51fd\u6570\u7ed8\u5236\u4e00\u6761\u66f2\u7ebf\uff0c\u4f5c\u4e3a\u7b11\u8138\u7684\u5634\u5df4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = np.linspace(0.3, 0.7, 100)<\/p>\n<p>y = 0.4 + 0.1 * np.sin(2 * np.pi * (x - 0.5))<\/p>\n<p>ax.plot(x, y, color=&#39;black&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6\u3001\u663e\u793a\u7b11\u8138\u56fe\u6848<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>plt.show()<\/code>\u51fd\u6570\u663e\u793a\u7ed8\u5236\u7684\u7b11\u8138\u56fe\u6848\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u7b11\u8138\u56fe\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Turtle\u7ed8\u5236\u7b11\u8138<\/h3>\n<\/p>\n<p><p>Turtle\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u7ed8\u56fe\u5e93\uff0c\u7279\u522b\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u7ed8\u56fe\u3002\u4e0b\u9762\u662f\u4f7f\u7528Turtle\u7ed8\u5236\u7b11\u8138\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5bfc\u5165Turtle\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bbe\u7f6e\u7ed8\u56fe\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>turtle.Screen()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ed8\u56fe\u7a97\u53e3\uff0c\u5e76\u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u548c\u80cc\u666f\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">screen = turtle.Screen()<\/p>\n<p>screen.title(&quot;Smile Face&quot;)<\/p>\n<p>screen.bgcolor(&quot;white&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efaTurtle\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>turtle.Turtle()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Turtle\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6eTurtle\u5bf9\u8c61\u7684\u5f62\u72b6\u548c\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">smile = turtle.Turtle()<\/p>\n<p>smile.shape(&quot;turtle&quot;)<\/p>\n<p>smile.color(&quot;black&quot;)<\/p>\n<p>smile.speed(5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u7ed8\u5236\u8138\u90e8\u8f6e\u5ed3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>circle()<\/code>\u51fd\u6570\u7ed8\u5236\u4e00\u4e2a\u5706\u5f62\uff0c\u4f5c\u4e3a\u7b11\u8138\u7684\u8138\u90e8\u8f6e\u5ed3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">smile.penup()<\/p>\n<p>smile.goto(0, -100)<\/p>\n<p>smile.pendown()<\/p>\n<p>smile.begin_fill()<\/p>\n<p>smile.fillcolor(&quot;yellow&quot;)<\/p>\n<p>smile.circle(100)<\/p>\n<p>smile.end_fill()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u7ed8\u5236\u773c\u775b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>circle()<\/code>\u51fd\u6570\u7ed8\u5236\u4e24\u4e2a\u8f83\u5c0f\u7684\u5706\u5f62\uff0c\u5206\u522b\u4f5c\u4e3a\u7b11\u8138\u7684\u773c\u775b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">smile.penup()<\/p>\n<p>smile.goto(-35, 50)<\/p>\n<p>smile.pendown()<\/p>\n<p>smile.begin_fill()<\/p>\n<p>smile.fillcolor(&quot;black&quot;)<\/p>\n<p>smile.circle(10)<\/p>\n<p>smile.end_fill()<\/p>\n<p>smile.penup()<\/p>\n<p>smile.goto(35, 50)<\/p>\n<p>smile.pendown()<\/p>\n<p>smile.begin_fill()<\/p>\n<p>smile.fillcolor(&quot;black&quot;)<\/p>\n<p>smile.circle(10)<\/p>\n<p>smile.end_fill()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6\u3001\u7ed8\u5236\u5634\u5df4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Turtle\u5bf9\u8c61\u7684<code>goto()<\/code>\u51fd\u6570\u79fb\u52a8\u5230\u9002\u5f53\u7684\u4f4d\u7f6e\uff0c\u5e76\u4f7f\u7528<code>circle()<\/code>\u51fd\u6570\u7ed8\u5236\u4e00\u6761\u66f2\u7ebf\uff0c\u4f5c\u4e3a\u7b11\u8138\u7684\u5634\u5df4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">smile.penup()<\/p>\n<p>smile.goto(-40, 0)<\/p>\n<p>smile.setheading(-60)<\/p>\n<p>smile.width(5)<\/p>\n<p>smile.pendown()<\/p>\n<p>smile.circle(50, 120)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7\u3001\u9690\u85cfTurtle\u5bf9\u8c61\u5e76\u663e\u793a\u7b11\u8138\u56fe\u6848<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>hideturtle()<\/code>\u51fd\u6570\u9690\u85cfTurtle\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>done()<\/code>\u51fd\u6570\u4fdd\u6301\u7ed8\u56fe\u7a97\u53e3\u6253\u5f00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">smile.hideturtle()<\/p>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Turtle\u5e93\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u7b11\u8138\u56fe\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684Matplotlib\u548cTurtle\u5e93\u90fd\u53ef\u4ee5\u8f7b\u677e\u5730\u7ed8\u5236\u51fa\u4e00\u4e2a\u7b11\u8138\u56fe\u6848\u3002<strong>Matplotlib\u9002\u5408\u7ed8\u5236\u7cbe\u7ec6\u7684\u56fe\u5f62\u548c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u800cTurtle\u5219\u9002\u5408\u521d\u5b66\u8005\u5b66\u4e60\u7f16\u7a0b\u548c\u7ed8\u56fe\u3002<\/strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0\u56fe\u5f62\u7ed8\u5236\u3002\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5229\u7528Python\u7ed8\u5236\u7b11\u8138\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u7ed8\u5236\u7b11\u8138\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7ed8\u5236\u7b11\u8138\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u9009\u62e9\u4e00\u4e2a\u9002\u5408\u7684\u56fe\u5f62\u5e93\uff0c\u4f8b\u5982Matplotlib\u6216Turtle\u3002\u5bf9\u4e8eMatplotlib\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5706\u5f62\u548c\u7ebf\u6761\u6765\u521b\u5efa\u7b11\u8138\u7684\u8f6e\u5ed3\u3001\u773c\u775b\u548c\u5634\u5df4\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u8bbe\u7f6e\u7ed8\u56fe\u533a\u57df\u3001\u7ed8\u5236\u7b11\u8138\u7684\u5706\u5f62\u5916\u6846\u3001\u6dfb\u52a0\u773c\u775b\u548c\u5fae\u7b11\u7684\u5634\u5df4\uff0c\u518d\u901a\u8fc7\u663e\u793a\u51fd\u6570\u5c06\u56fe\u5f62\u5c55\u73b0\u51fa\u6765\u3002<\/p>\n<p><strong>\u54ea\u4e9bPython\u5e93\u9002\u5408\u7ed8\u5236\u7b80\u5355\u56fe\u5f62\uff0c\u6bd4\u5982\u7b11\u8138\uff1f<\/strong><br \/>Python\u4e2d\u6709\u51e0\u4e2a\u5e93\u975e\u5e38\u9002\u5408\u7ed8\u5236\u7b80\u5355\u56fe\u5f62\uff0c\u4f8b\u5982Matplotlib\u3001Turtle\u548cPygame\u3002Matplotlib\u9002\u5408\u7528\u4e8e\u79d1\u5b66\u7ed8\u56fe\u548c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f46\u4e5f\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u7b80\u5355\u56fe\u5f62\u3002Turtle\u662f\u4e00\u4e2a\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u7684\u5e93\uff0c\u901a\u8fc7\u7b80\u5355\u7684\u547d\u4ee4\u5c31\u53ef\u4ee5\u7ed8\u5236\u590d\u6742\u56fe\u5f62\u3002Pygame\u5219\u66f4\u9002\u5408\u4e8e\u6e38\u620f\u5f00\u53d1\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u56fe\u5f62\u7ed8\u5236\u3002<\/p>\n<p><strong>\u5728\u7ed8\u5236\u7b11\u8138\u65f6\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u989c\u8272\u548c\u5f62\u72b6\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u7b11\u8138\u65f6\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u989c\u8272\u548c\u8c03\u6574\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u5916\u89c2\u3002\u4f7f\u7528Matplotlib\u65f6\uff0c\u53ef\u4ee5\u5728\u7ed8\u5236\u5706\u5f62\u65f6\u6307\u5b9a\u989c\u8272\uff0c\u4f8b\u5982<code>facecolor=&#39;yellow&#39;<\/code>\u6765\u586b\u5145\u7b11\u8138\u7684\u989c\u8272\u3002\u773c\u775b\u548c\u5634\u5df4\u7684\u5f62\u72b6\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u5750\u6807\u548c\u534a\u5f84\u6765\u5b9e\u73b0\u4e0d\u540c\u7684\u8868\u60c5\uff0c\u6bd4\u5982\u5c06\u5634\u5df4\u6539\u4e3a\u4e00\u4e2a\u62f1\u5f62\u4ee5\u8868\u73b0\u4e0d\u540c\u7684\u60c5\u7eea\u3002Turtle\u5e93\u4e5f\u5141\u8bb8\u60a8\u901a\u8fc7<code>fillcolor()<\/code>\u51fd\u6570\u8f7b\u677e\u6539\u53d8\u989c\u8272\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5229\u7528Python\u753b\u4e00\u4e2a\u7b11\u8138\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u56fe\u5f62\u5e93\uff0c\u4f8b\u5982matplotlib\u548cturtle\u3002\u901a\u8fc7\u8c03\u7528\u8fd9 [&hellip;]","protected":false},"author":3,"featured_media":1097583,"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\/1097574"}],"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=1097574"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097574\/revisions"}],"predecessor-version":[{"id":1097585,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097574\/revisions\/1097585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097583"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}