{"id":1098262,"date":"2025-01-08T15:19:31","date_gmt":"2025-01-08T07:19:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098262.html"},"modified":"2025-01-08T15:19:33","modified_gmt":"2025-01-08T07:19:33","slug":"python%e7%bb%98%e7%94%bb%e5%a6%82%e4%bd%95%e5%a1%ab%e5%85%85%e4%b8%a4%e7%a7%8d%e9%a2%9c%e8%89%b2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098262.html","title":{"rendered":"python\u7ed8\u753b\u5982\u4f55\u586b\u5145\u4e24\u79cd\u989c\u8272"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212651\/5bdd058b-21db-48d4-b293-dfbc06f78326.webp\" alt=\"python\u7ed8\u753b\u5982\u4f55\u586b\u5145\u4e24\u79cd\u989c\u8272\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7ed8\u753b\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u6765\u5b9e\u73b0\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u6548\u679c\uff0c\u5982Pillow\uff08PIL\uff09\u3001Matplotlib\u3001Turtle\u7b49\u3002<\/strong> \u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528Pillow\u5e93\u6765\u5904\u7406\u56fe\u50cf\u548c\u7ed8\u753b\u4efb\u52a1\u3002Pillow\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u6548\u679c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Pillow\u5e93\u6765\u586b\u5145\u4e24\u79cd\u989c\u8272\uff0c\u5e76\u4ecb\u7ecd\u4e00\u4e9b\u5176\u4ed6\u5e38\u89c1\u7684\u5e93\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Pillow\u5e93\u5b9e\u73b0\u586b\u5145\u4e24\u79cd\u989c\u8272<\/h3>\n<\/p>\n<p><p>Pillow\u5e93\u662fPython Imaging Library\uff08PIL\uff09\u7684\u4e00\u4e2a\u5206\u652f\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u521b\u5efa\u56fe\u50cf\uff0c\u5e76\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u548c\u586b\u5145\u4e0d\u540c\u7684\u5f62\u72b6\u548c\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Pillow\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Pillow\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 pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u56fe\u50cf\u5e76\u586b\u5145\u4e24\u79cd\u989c\u8272<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Pillow\u5e93\u521b\u5efa\u56fe\u50cf\u5e76\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<h2><strong>\u521b\u5efa\u7a7a\u767d\u56fe\u50cf<\/strong><\/h2>\n<p>width, height = 400, 400<\/p>\n<p>image = Image.new(&quot;RGB&quot;, (width, height), &quot;white&quot;)<\/p>\n<p>draw = ImageDraw.Draw(image)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u79cd\u989c\u8272<\/strong><\/h2>\n<p>color1 = (255, 0, 0)  # \u7ea2\u8272<\/p>\n<p>color2 = (0, 0, 255)  # \u84dd\u8272<\/p>\n<h2><strong>\u7ed8\u5236\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u77e9\u5f62<\/strong><\/h2>\n<p>draw.rectangle([0, 0, width\/\/2, height], fill=color1)<\/p>\n<p>draw.rectangle([width\/\/2, 0, width, height], fill=color2)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u50cf<\/strong><\/h2>\n<p>image.save(&quot;two_color_fill.png&quot;)<\/p>\n<p>image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a400&#215;400\u50cf\u7d20\u7684\u7a7a\u767d\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528<code>ImageDraw<\/code>\u5bf9\u8c61\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u4e24\u4e2a\u586b\u5145\u4e0d\u540c\u989c\u8272\u7684\u77e9\u5f62\u3002\u6700\u7ec8\uff0c\u6211\u4eec\u5c06\u56fe\u50cf\u4fdd\u5b58\u5e76\u663e\u793a\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001Matplotlib\u5e93\u5b9e\u73b0\u586b\u5145\u4e24\u79cd\u989c\u8272<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u56fe\u8868\u548c\u7ed8\u56fe\u4efb\u52a1\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6765\u5b9e\u73b0\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Matplotlib\u5e93<\/h4>\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><h4>2\u3001\u7ed8\u5236\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u793a\u4f8b\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<h2><strong>\u521b\u5efa\u753b\u5e03<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u79cd\u989c\u8272<\/strong><\/h2>\n<p>color1 = &#39;red&#39;<\/p>\n<p>color2 = &#39;blue&#39;<\/p>\n<h2><strong>\u7ed8\u5236\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u77e9\u5f62<\/strong><\/h2>\n<p>rect1 = plt.Rectangle((0, 0), 0.5, 1, color=color1)<\/p>\n<p>rect2 = plt.Rectangle((0.5, 0), 0.5, 1, color=color2)<\/p>\n<p>ax.add_patch(rect1)<\/p>\n<p>ax.add_patch(rect2)<\/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>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Matplotlib\u521b\u5efa\u4e00\u4e2a\u753b\u5e03\uff0c\u5e76\u7ed8\u5236\u4e24\u4e2a\u586b\u5145\u4e0d\u540c\u989c\u8272\u7684\u77e9\u5f62\u3002\u6700\u7ec8\uff0c\u6211\u4eec\u5c06\u56fe\u50cf\u663e\u793a\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001Turtle\u5e93\u5b9e\u73b0\u586b\u5145\u4e24\u79cd\u989c\u8272<\/h3>\n<\/p>\n<p><p>Turtle\u5e93\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u7ed8\u56fe\u4efb\u52a1\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528Turtle\u5e93\u6765\u5b9e\u73b0\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165Turtle\u5e93<\/h4>\n<\/p>\n<p><p>Turtle\u5e93\u662fPython\u5185\u7f6e\u7684\u5e93\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002\u53ef\u4ee5\u76f4\u63a5\u5bfc\u5165\u5e76\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed8\u5236\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Turtle\u5e93\u7ed8\u5236\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efa\u753b\u5e03<\/strong><\/h2>\n<p>screen = turtle.Screen()<\/p>\n<p>screen.setup(width=400, height=400)<\/p>\n<h2><strong>\u521b\u5efaTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<h2><strong>\u7ed8\u5236\u586b\u5145\u7ea2\u8272\u7684\u77e9\u5f62<\/strong><\/h2>\n<p>t.penup()<\/p>\n<p>t.goto(-200, 200)<\/p>\n<p>t.pendown()<\/p>\n<p>t.color(&quot;red&quot;)<\/p>\n<p>t.begin_fill()<\/p>\n<p>for _ in range(2):<\/p>\n<p>    t.forward(200)<\/p>\n<p>    t.right(90)<\/p>\n<p>    t.forward(400)<\/p>\n<p>    t.right(90)<\/p>\n<p>t.end_fill()<\/p>\n<h2><strong>\u7ed8\u5236\u586b\u5145\u84dd\u8272\u7684\u77e9\u5f62<\/strong><\/h2>\n<p>t.penup()<\/p>\n<p>t.goto(0, 200)<\/p>\n<p>t.pendown()<\/p>\n<p>t.color(&quot;blue&quot;)<\/p>\n<p>t.begin_fill()<\/p>\n<p>for _ in range(2):<\/p>\n<p>    t.forward(200)<\/p>\n<p>    t.right(90)<\/p>\n<p>    t.forward(400)<\/p>\n<p>    t.right(90)<\/p>\n<p>t.end_fill()<\/p>\n<h2><strong>\u9690\u85cfTurtle\u5bf9\u8c61\u5e76\u5b8c\u6210\u7ed8\u56fe<\/strong><\/h2>\n<p>t.hideturtle()<\/p>\n<p>turtle.done()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Turtle\u5e93\u521b\u5efa\u4e00\u4e2a\u753b\u5e03\uff0c\u5e76\u7ed8\u5236\u4e24\u4e2a\u586b\u5145\u4e0d\u540c\u989c\u8272\u7684\u77e9\u5f62\u3002\u6700\u7ec8\uff0c\u6211\u4eec\u5b8c\u6210\u7ed8\u56fe\u5e76\u663e\u793a\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u6765\u5b9e\u73b0\u7ed8\u753b\u548c\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u6548\u679c\u3002<strong>Pillow\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u56fe\u50cf\u521b\u5efa\u548c\u5904\u7406\u4efb\u52a1<\/strong>\u3002<strong>Matplotlib\u5e93\u9002\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u56fe\u8868\u548c\u7ed8\u56fe\u4efb\u52a1<\/strong>\u3002<strong>Turtle\u5e93\u9002\u7528\u4e8e\u7b80\u5355\u7684\u7ed8\u56fe\u4efb\u52a1<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\u7ed8\u56fe\u548c\u989c\u8272\u586b\u5145\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u5904\u7406\u56fe\u50cf\u3001\u521b\u5efa\u56fe\u8868\uff0c\u8fd8\u662f\u7ed8\u5236\u7b80\u5355\u56fe\u5f62\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5e93\u548c\u5de5\u5177\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728Python\u7ed8\u753b\u548c\u989c\u8272\u586b\u5145\u65b9\u9762\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u753b\u5e93\u5b9e\u73b0\u53cc\u8272\u586b\u5145\u6548\u679c\uff1f<\/strong><br \/>\u8981\u5728Python\u7ed8\u753b\u4e2d\u5b9e\u73b0\u53cc\u8272\u586b\u5145\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u50cfMatplotlib\u3001Pygame\u6216Turtle\u7b49\u5e93\u3002\u4ee5Matplotlib\u4e3a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u533a\u57df\u5e76\u4e3a\u6bcf\u4e2a\u533a\u57df\u6307\u5b9a\u4e0d\u540c\u7684\u989c\u8272\u6765\u5b9e\u73b0\u53cc\u8272\u586b\u5145\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>fill<\/code>\u51fd\u6570\u6765\u5b9a\u4e49\u591a\u8fb9\u5f62\u7684\u8fb9\u754c\uff0c\u5e76\u4e3a\u5176\u6307\u5b9a\u989c\u8272\u3002<\/p>\n<p><strong>\u5728Pygame\u4e2d\u5982\u4f55\u5b9e\u73b0\u53cc\u8272\u586b\u5145\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u5728Pygame\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u8868\u9762\u6216\u4f7f\u7528<code>pygame.draw<\/code>\u6a21\u5757\u7684\u4e0d\u540c\u7ed8\u56fe\u51fd\u6570\u6765\u5b9e\u73b0\u53cc\u8272\u586b\u5145\u3002\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u989c\u8272\uff0c\u7136\u540e\u4f7f\u7528<code>pygame.draw.polygon()<\/code>\u7ed8\u5236\u591a\u8fb9\u5f62\uff0c\u5c06\u4e0d\u540c\u7684\u533a\u57df\u586b\u5145\u4e0d\u540c\u7684\u989c\u8272\u3002\u786e\u4fdd\u5728\u7ed8\u5236\u524d\u8bbe\u7f6e\u597d\u5c4f\u5e55\u80cc\u666f\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5c55\u793a\u53cc\u8272\u6548\u679c\u3002<\/p>\n<p><strong>Turtle\u7ed8\u56fe\u5982\u4f55\u5b9e\u73b0\u53cc\u8272\u586b\u5145\uff1f<\/strong><br \/>\u4f7f\u7528Turtle\u5e93\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u591a\u4e2a\u5f62\u72b6\u5e76\u4e3a\u6bcf\u4e2a\u5f62\u72b6\u8bbe\u7f6e\u4e0d\u540c\u7684\u586b\u5145\u989c\u8272\u6765\u5b9e\u73b0\u53cc\u8272\u586b\u5145\u3002\u53ef\u4ee5\u4f7f\u7528<code>turtle.begin_fill()<\/code>\u548c<code>turtle.end_fill()<\/code>\u6765\u5b9a\u4e49\u586b\u5145\u533a\u57df\uff0c\u5e76\u5728\u7ed8\u5236\u5f62\u72b6\u524d\u540e\u8bbe\u7f6e\u4e0d\u540c\u7684\u586b\u5145\u989c\u8272\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5177\u6709\u53cc\u8272\u586b\u5145\u7684\u590d\u6742\u56fe\u5f62\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed8\u753b\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u6765\u5b9e\u73b0\u586b\u5145\u4e24\u79cd\u989c\u8272\u7684\u6548\u679c\uff0c\u5982Pillow\uff08PIL\uff09\u3001Matplot [&hellip;]","protected":false},"author":3,"featured_media":1098267,"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\/1098262"}],"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=1098262"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098262\/revisions"}],"predecessor-version":[{"id":1098268,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098262\/revisions\/1098268"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098267"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}