{"id":1053500,"date":"2024-12-31T14:34:25","date_gmt":"2024-12-31T06:34:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1053500.html"},"modified":"2024-12-31T14:34:25","modified_gmt":"2024-12-31T06:34:25","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","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1053500.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u753b\u4e00\u4e2a\u5706"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3ec5aaaf-557f-46e0-88fa-8c9f0189d8d7.webp\" alt=\"\u5982\u4f55\u5229\u7528python\u753b\u4e00\u4e2a\u5706\" \/><\/p>\n<p><p> <strong>\u5229\u7528Python\u753b\u4e00\u4e2a\u5706\uff0c\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528Matplotlib\u5e93\u3001\u4f7f\u7528Turtle\u5e93\u3001\u4f7f\u7528Pygame\u5e93\u3001\u4f7f\u7528OpenCV\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c<strong>Matplotlib\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\uff0c\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u5927\u90e8\u5206\u7ed8\u56fe\u9700\u6c42\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u7528Matplotlib\u5e93\u753b\u4e00\u4e2a\u5706\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001MATPLOTLIB\u5e93\u7ed8\u5236\u5706<\/strong><\/h2>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u7ed8\u5236\u5404\u79cd2D\u56fe\u5f62\u3002\u5b83\u80fd\u751f\u6210\u9ad8\u8d28\u91cf\u7684\u56fe\u5f62\uff0c\u9002\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u4e0b\u9762\u662f\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u5706\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h2>\u5b89\u88c5Matplotlib<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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>\u5bfc\u5165\u5e93\u5e76\u7ed8\u5236\u5706<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5bfc\u5165Matplotlib\u5e93\u5e76\u4f7f\u7528\u5b83\u7ed8\u5236\u4e00\u4e2a\u5706\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\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u5706\u7684\u53c2\u6570<\/strong><\/h2>\n<p>radius = 1<\/p>\n<p>center = (0, 0)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u751f\u6210\u5706\u5468\u4e0a\u7684\u70b9<\/strong><\/h2>\n<p>theta = np.linspace(0, 2*np.pi, 100)<\/p>\n<p>x = center[0] + radius * np.cos(theta)<\/p>\n<p>y = center[1] + radius * np.sin(theta)<\/p>\n<h2><strong>\u7ed8\u5236\u5706<\/strong><\/h2>\n<p>ax.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u5f62\u7684\u6bd4\u4f8b<\/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\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>numpy<\/code>\u5e93\u7528\u4e8e\u751f\u6210\u5706\u5468\u4e0a\u7684\u70b9\uff0c<code>matplotlib<\/code>\u5e93\u7528\u4e8e\u7ed8\u5236\u5706\u3002<code>set_aspect(&#39;equal&#39;)<\/code>\u786e\u4fdd\u5706\u7684\u6bd4\u4f8b\u6b63\u786e\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001TURTLE\u5e93\u7ed8\u5236\u5706<\/strong><\/h2>\n<p><p>Turtle\u5e93\u662fPython\u5185\u7f6e\u7684\u56fe\u5f62\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u521d\u5b66\u8005\u548c\u6559\u80b2\u7528\u9014\u3002\u5b83\u901a\u8fc7\u6a21\u62df\u4e4c\u9f9f\u5728\u5c4f\u5e55\u4e0a\u79fb\u52a8\u6765\u7ed8\u5236\u56fe\u5f62\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Turtle\u5e93\u7ed8\u5236\u4e00\u4e2a\u5706\uff1a<\/p>\n<\/p>\n<p><h2>\u5b89\u88c5Turtle\u5e93<\/h2>\n<\/p>\n<p><p>Turtle\u5e93\u662fPython\u5185\u7f6e\u5e93\uff0c\u65e0\u9700\u5b89\u88c5\u3002\u76f4\u63a5\u5bfc\u5165\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u5bfc\u5165\u5e93\u5e76\u7ed8\u5236\u5706<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aTurtle\u5bf9\u8c61<\/strong><\/h2>\n<p>t = turtle.Turtle()<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e4c\u9f9f\u7684\u901f\u5ea6<\/strong><\/h2>\n<p>t.speed(1)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u5706<\/strong><\/h2>\n<p>t.circle(100)<\/p>\n<h2><strong>\u9690\u85cf\u4e4c\u9f9f<\/strong><\/h2>\n<p>t.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\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>t.circle(100)<\/code>\u7ed8\u5236\u4e86\u4e00\u4e2a\u534a\u5f84\u4e3a100\u7684\u5706\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001PYGAME\u5e93\u7ed8\u5236\u5706<\/strong><\/h2>\n<p><p>Pygame\u662f\u4e00\u4e2a\u7528\u4e8e\u5f00\u53d1\u6e38\u620f\u7684Python\u5e93\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u56fe\u5f62\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Pygame\u5e93\u7ed8\u5236\u4e00\u4e2a\u5706\uff1a<\/p>\n<\/p>\n<p><h2>\u5b89\u88c5Pygame<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u5b89\u88c5Pygame\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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>\u5bfc\u5165\u5e93\u5e76\u7ed8\u5236\u5706<\/h2>\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 = (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;Draw Circle&quot;)<\/p>\n<h2><strong>\u5b9a\u4e49\u989c\u8272<\/strong><\/h2>\n<p>WHITE = (255, 255, 255)<\/p>\n<p>BLACK = (0, 0, 0)<\/p>\n<h2><strong>\u586b\u5145\u80cc\u666f\u8272<\/strong><\/h2>\n<p>screen.fill(WHITE)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u5706<\/strong><\/h2>\n<p>pygame.draw.circle(screen, BLACK, (200, 200), 100)<\/p>\n<h2><strong>\u66f4\u65b0\u663e\u793a<\/strong><\/h2>\n<p>pygame.display.flip()<\/p>\n<h2><strong>\u7b49\u5f85\u9000\u51fa<\/strong><\/h2>\n<p>done = False<\/p>\n<p>while not done:<\/p>\n<p>    for event in pygame.event.get():<\/p>\n<p>        if event.type == pygame.QUIT:<\/p>\n<p>            done = True<\/p>\n<h2><strong>\u9000\u51faPygame<\/strong><\/h2>\n<p>pygame.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>pygame.draw.circle(screen, BLACK, (200, 200), 100)<\/code>\u7ed8\u5236\u4e86\u4e00\u4e2a\u5706\u5fc3\u5728(200, 200)\uff0c\u534a\u5f84\u4e3a100\u7684\u5706\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001OPENCV\u5e93\u7ed8\u5236\u5706<\/strong><\/h2>\n<p><p>OpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u56fe\u50cf\u548c\u89c6\u9891\uff0c\u8fd8\u53ef\u4ee5\u7ed8\u5236\u56fe\u5f62\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528OpenCV\u5e93\u7ed8\u5236\u4e00\u4e2a\u5706\uff1a<\/p>\n<\/p>\n<p><h2>\u5b89\u88c5OpenCV<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u5b89\u88c5OpenCV\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5bfc\u5165\u5e93\u5e76\u7ed8\u5236\u5706<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u767d\u56fe\u50cf<\/strong><\/h2>\n<p>image = np.zeros((400, 400, 3), dtype=np.uint8)<\/p>\n<h2><strong>\u5b9a\u4e49\u989c\u8272<\/strong><\/h2>\n<p>WHITE = (255, 255, 255)<\/p>\n<h2><strong>\u7ed8\u5236\u4e00\u4e2a\u5706<\/strong><\/h2>\n<p>cv2.circle(image, (200, 200), 100, WHITE, -1)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&quot;Circle&quot;, image)<\/p>\n<h2><strong>\u7b49\u5f85\u7528\u6237\u6309\u952e<\/strong><\/h2>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<h2><strong>\u5173\u95ed\u7a97\u53e3<\/strong><\/h2>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>cv2.circle(image, (200, 200), 100, WHITE, -1)<\/code>\u7ed8\u5236\u4e86\u4e00\u4e2a\u5706\u5fc3\u5728(200, 200)\uff0c\u534a\u5f84\u4e3a100\u7684\u5706\uff0c\u989c\u8272\u4e3a\u767d\u8272\u3002<\/p>\n<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u4f7f\u7528Matplotlib\u5e93\u3001Turtle\u5e93\u3001Pygame\u5e93\u548cOpenCV\u5e93<\/strong>\u90fd\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u7ed8\u5236\u5706\u3002<strong>Matplotlib\u5e93<\/strong>\u9002\u5408\u6570\u636e\u53ef\u89c6\u5316\uff0c<strong>Turtle\u5e93<\/strong>\u9002\u5408\u6559\u5b66\u548c\u521d\u5b66\u8005\uff0c<strong>Pygame\u5e93<\/strong>\u9002\u5408\u6e38\u620f\u5f00\u53d1\uff0c<strong>OpenCV\u5e93<\/strong>\u9002\u5408\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5b8c\u6210\u7ed8\u56fe\u4efb\u52a1\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\u989c\u8272\u7684\u5706\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u8f7b\u677e\u7ed8\u5236\u5e26\u6709\u4e0d\u540c\u989c\u8272\u7684\u5706\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86<code>matplotlib<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip install matplotlib<\/code>\u547d\u4ee4\u5b89\u88c5\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>plt.Circle<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u5706\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6e<code>facecolor<\/code>\u53c2\u6570\u6765\u5b9a\u4e49\u989c\u8272\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u7ed8\u5236\u4e00\u4e2a\u7ea2\u8272\u7684\u5706\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.4, facecolor=&#39;red&#39;)\nax.add_artist(circle)\nax.set_xlim(0, 1)\nax.set_ylim(0, 1)\nplt.gca().set_aspect(&#39;equal&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528Python\u7ed8\u5236\u591a\u4e2a\u5706\u5417\uff1f\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u8981\u7ed8\u5236\u591a\u4e2a\u5706\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u8c03\u7528\u7ed8\u5236\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u591a\u4e2a\u4e0d\u540c\u4f4d\u7f6e\u548c\u5927\u5c0f\u7684\u5706\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nfig, ax = plt.subplots()\ncircles = [(0.2, 0.2, 0.1), (0.5, 0.5, 0.2), (0.8, 0.3, 0.15)]  # (x, y, radius)\n\nfor (x, y, radius) in circles:\n    circle = plt.Circle((x, y), radius, facecolor=&#39;blue&#39;, alpha=0.5)\n    ax.add_artist(circle)\n\nax.set_xlim(0, 1)\nax.set_ylim(0, 1)\nplt.gca().set_aspect(&#39;equal&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u7ed8\u5236\u7684\u5706\u4e0a\u6dfb\u52a0\u6807\u7b7e\u6216\u6587\u5b57\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>ax.text<\/code>\u65b9\u6cd5\u5728\u5706\u7684\u4e2d\u5fc3\u6dfb\u52a0\u6807\u7b7e\u6216\u6587\u5b57\u3002\u5728\u7ed8\u5236\u5706\u7684\u4ee3\u7801\u540e\uff0c\u4f7f\u7528\u8be5\u65b9\u6cd5\u6765\u653e\u7f6e\u6587\u672c\u3002\u4f8b\u5982\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, facecolor=&#39;green&#39;, alpha=0.5)\nax.add_artist(circle)\nax.text(0.5, 0.5, &#39;\u4e2d\u5fc3&#39;, fontsize=12, ha=&#39;center&#39;, va=&#39;center&#39;)\n\nax.set_xlim(0, 1)\nax.set_ylim(0, 1)\nplt.gca().set_aspect(&#39;equal&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5728Python\u4e2d\u7ed8\u5236\u5706\u5e76\u8fdb\u884c\u4e2a\u6027\u5316\u8bbe\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5229\u7528Python\u753b\u4e00\u4e2a\u5706\uff0c\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528Matplotlib\u5e93\u3001\u4f7f\u7528Turtle\u5e93\u3001\u4f7f\u7528Pygame\u5e93\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053500"}],"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=1053500"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053500\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1053500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1053500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1053500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}