{"id":942164,"date":"2024-12-26T21:54:08","date_gmt":"2024-12-26T13:54:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/942164.html"},"modified":"2024-12-26T21:54:10","modified_gmt":"2024-12-26T13:54:10","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e6%a8%b1%e8%8a%b1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/942164.html","title":{"rendered":"python\u5982\u4f55\u753b\u6a31\u82b1"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080100\/5975e7d7-911a-4fb0-b234-aca10c6f4f38.webp\" alt=\"python\u5982\u4f55\u753b\u6a31\u82b1\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u753b\u6a31\u82b1\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u3001Turtle\u5e93\u3001PIL\u5e93\u7b49\u5b9e\u73b0\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u521b\u5efa\u968f\u673a\u5206\u5e03\u7684\u6a31\u82b1\u70b9\u3001Turtle\u5e93\u53ef\u4ee5\u521b\u5efa\u6a31\u82b1\u5f62\u72b6\u3001PIL\u5e93\u7528\u4e8e\u521b\u5efa\u66f4\u52a0\u590d\u6742\u548c\u827a\u672f\u5316\u7684\u6a31\u82b1\u56fe\u50cf\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5728Python\u4e2d\u7ed8\u5236\u6a31\u82b1\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5229\u7528Matplotlib\u5e93\u7ed8\u5236\u6a31\u82b1<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u751f\u6210\u56fe\u8868\u548c\u4e8c\u7ef4\u56fe\u5f62\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u7ed8\u5236\u7b80\u5355\u7684\u6a31\u82b1\u56fe\u6848\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u7b80\u5355\u6a31\u82b1\u56fe\u6848<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u6563\u70b9\u56fe\u6765\u6a21\u62df\u6a31\u82b1\u7684\u6837\u5f0f\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\u3002\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><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u751f\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u6a31\u82b1\u6563\u70b9\u56fe\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>\u968f\u673a\u751f\u6210\u6570\u636e\u70b9<\/strong><\/h2>\n<p>x = np.random.rand(100)<\/p>\n<p>y = np.random.rand(100)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x, y, c=&#39;pink&#39;, s=100, alpha=0.5)<\/p>\n<p>plt.title(&#39;Cherry Blossom&#39;)<\/p>\n<p>plt.axis(&#39;off&#39;)  # \u9690\u85cf\u5750\u6807\u8f74<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>np.random.rand<\/code>\u751f\u6210\u4e86100\u4e2a\u968f\u673a\u70b9\uff0c\u5e76\u4f7f\u7528<code>plt.scatter<\/code>\u6765\u7ed8\u5236\u8fd9\u4e9b\u70b9\uff0c\u8bbe\u7f6e\u70b9\u7684\u989c\u8272\u4e3a\u7c89\u8272\uff08\u4ee3\u8868\u6a31\u82b1\uff09\uff0c\u900f\u660e\u5ea6\u4e3a0.5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6539\u8fdb\u6a31\u82b1\u56fe\u6848<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u6a31\u82b1\u56fe\u6848\u66f4\u52a0\u903c\u771f\uff0c\u53ef\u4ee5\u8c03\u6574\u70b9\u7684\u5927\u5c0f\u3001\u5f62\u72b6\u3001\u989c\u8272\u7b49\u5c5e\u6027\uff0c\u751a\u81f3\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u591a\u4e2a\u4e0d\u540c\u989c\u8272\u548c\u5927\u5c0f\u7684\u6563\u70b9\u6765\u6a21\u62df\u6a31\u82b1\u6811\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u5229\u7528Turtle\u5e93\u7ed8\u5236\u6a31\u82b1<\/p>\n<\/p>\n<p><p>Turtle\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u7b80\u5355\u56fe\u5f62\u7ed8\u5236\u6a21\u5757\uff0c\u9002\u7528\u4e8e\u7ed8\u5236\u7b80\u5355\u7684\u56fe\u5f62\u548c\u52a8\u753b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Turtle\u5e93\u7ed8\u5236\u6a31\u82b1\u5f62\u72b6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ed8\u5236\u7b80\u5355\u6a31\u82b1\u5f62\u72b6<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Turtle\u5e93\uff0c\u7136\u540e\u8bbe\u7f6e\u753b\u5e03\u548c\u753b\u7b14\u5c5e\u6027\uff0c\u63a5\u7740\u7f16\u5199\u51fd\u6570\u6765\u7ed8\u5236\u6a31\u82b1\u5f62\u72b6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import turtle<\/p>\n<p>def draw_petals(t, radius):<\/p>\n<p>    for _ in range(6):<\/p>\n<p>        t.circle(radius, 60)<\/p>\n<p>        t.left(120)<\/p>\n<p>        t.circle(radius, 60)<\/p>\n<p>        t.left(60)<\/p>\n<p>def draw_flower():<\/p>\n<p>    window = turtle.Screen()<\/p>\n<p>    window.bgcolor(&quot;white&quot;)<\/p>\n<p>    t = turtle.Turtle()<\/p>\n<p>    t.shape(&quot;turtle&quot;)<\/p>\n<p>    t.speed(5)<\/p>\n<p>    t.color(&quot;pink&quot;)<\/p>\n<p>    # \u7ed8\u5236\u6a31\u82b1<\/p>\n<p>    draw_petals(t, 100)<\/p>\n<p>    t.hideturtle()<\/p>\n<p>    window.exitonclick()<\/p>\n<p>draw_flower()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>draw_petals<\/code>\uff0c\u7528\u4e8e\u7ed8\u5236\u82b1\u74e3\u7684\u5f62\u72b6\u3002\u901a\u8fc7\u5faa\u73af\u548c\u753b\u5706\u6307\u4ee4\uff0c\u6a21\u62df\u4e86\u6a31\u82b1\u7684\u5f62\u72b6\u3002\u7136\u540e\u5728<code>draw_flower<\/code>\u51fd\u6570\u4e2d\u8c03\u7528\u8be5\u51fd\u6570\u4ee5\u5b8c\u6210\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u590d\u6742\u6a31\u82b1\u56fe\u6848<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u589e\u52a0\u6a31\u82b1\u7684\u590d\u6742\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u4e0d\u540c\u7684\u82b1\u74e3\u7ed8\u5236\u51fd\u6570\uff0c\u8c03\u6574\u82b1\u74e3\u7684\u5927\u5c0f\u548c\u989c\u8272\uff0c\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u6a31\u82b1\u56fe\u6848\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5229\u7528PIL\u5e93\u7ed8\u5236\u6a31\u82b1<\/p>\n<\/p>\n<p><p>Python Imaging Library\uff08PIL\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5c\u56fe\u50cf\u3002\u5229\u7528PIL\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u66f4\u52a0\u590d\u6742\u548c\u827a\u672f\u5316\u7684\u6a31\u82b1\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u5bfc\u5165PIL\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5PIL\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5bfc\u5165PIL\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image, ImageDraw<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u6a31\u82b1\u56fe\u50cf<\/strong><\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528PIL\u521b\u5efa\u7b80\u5355\u6a31\u82b1\u56fe\u50cf\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_cherry_blossom():<\/p>\n<p>    # \u521b\u5efa\u767d\u8272\u80cc\u666f\u56fe\u50cf<\/p>\n<p>    img = Image.new(&#39;RGB&#39;, (500, 500), &#39;white&#39;)<\/p>\n<p>    draw = ImageDraw.Draw(img)<\/p>\n<p>    # \u7ed8\u5236\u6a31\u82b1\u82b1\u74e3<\/p>\n<p>    for _ in range(50):<\/p>\n<p>        x0, y0 = (np.random.randint(0, 500), np.random.randint(0, 500))<\/p>\n<p>        x1, y1 = (x0 + np.random.randint(10, 50), y0 + np.random.randint(10, 50))<\/p>\n<p>        draw.ellipse([x0, y0, x1, y1], fill=&#39;pink&#39;)<\/p>\n<p>    # \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>    img.save(&#39;cherry_blossom.png&#39;)<\/p>\n<p>create_cherry_blossom()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a500&#215;500\u7684\u767d\u8272\u80cc\u666f\u56fe\u50cf\uff0c\u5e76\u4f7f\u7528<code>draw.ellipse<\/code>\u65b9\u6cd5\u968f\u673a\u7ed8\u5236\u7c89\u8272\u692d\u5706\uff0c\u6a21\u62df\u6a31\u82b1\u7684\u82b1\u74e3\u3002\u6700\u540e\u5c06\u751f\u6210\u7684\u56fe\u50cf\u4fdd\u5b58\u4e3a<code>cherry_blossom.png<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u6574\u56fe\u50cf\u7ec6\u8282<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u82b1\u74e3\u7684\u5927\u5c0f\u3001\u6570\u91cf\u3001\u989c\u8272\u7b49\u53c2\u6570\uff0c\u751a\u81f3\u53ef\u4ee5\u6dfb\u52a0\u5176\u4ed6\u5143\u7d20\uff08\u5982\u6811\u5e72\u3001\u80cc\u666f\u8272\uff09\u6765\u589e\u5f3a\u56fe\u50cf\u7684\u827a\u672f\u6548\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\uff0c\u5229\u7528Python\u7684\u5404\u7c7b\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u7ed8\u5236\u6a31\u82b1\u56fe\u6848\u3002Matplotlib\u9002\u5408\u5feb\u901f\u751f\u6210\u7b80\u5355\u7684\u56fe\u6848\uff0cTurtle\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u52a8\u753b\u548c\u7b80\u5355\u5f62\u72b6\uff0cPIL\u5219\u7528\u4e8e\u521b\u5efa\u590d\u6742\u548c\u827a\u672f\u5316\u7684\u56fe\u50cf\u3002\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u548c\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u6837\u5316\u7684\u6a31\u82b1\u56fe\u6848\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u6a31\u82b1\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u7ed8\u5236\u6a31\u82b1\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u56fe\u5f62\u5e93\u5982Matplotlib\u6216Turtle\u3002\u9996\u5148\uff0c\u9009\u62e9\u9002\u5408\u7684\u5e93\u5e76\u5b89\u88c5\u3002\u63a5\u7740\uff0c\u5b9a\u4e49\u6a31\u82b1\u7684\u57fa\u672c\u5f62\u72b6\uff0c\u5305\u62ec\u82b1\u74e3\u548c\u82b1\u5fc3\u3002\u901a\u8fc7\u8c03\u6574\u989c\u8272\u3001\u5f62\u72b6\u548c\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u521b\u5efa\u51fa\u7f8e\u4e3d\u7684\u6a31\u82b1\u56fe\u6848\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u73b0\u6210\u7684Python\u5e93\u53ef\u4ee5\u7528\u6765\u753b\u6a31\u82b1\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Turtle\u5e93\u6765\u753b\u6a31\u82b1\u3002Turtle\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7Matplotlib\u7ed8\u5236\u66f4\u590d\u6742\u7684\u6a31\u82b1\u56fe\u5f62\uff0c\u7ed3\u5408\u5176\u4ed6\u5e93\u5982NumPy\u8fdb\u884c\u66f4\u591a\u6570\u5b66\u8ba1\u7b97\u548c\u56fe\u5f62\u5904\u7406\u3002<\/p>\n<p><strong>\u7ed8\u5236\u6a31\u82b1\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u989c\u8272\u548c\u5f62\u72b6\uff1f<\/strong><br \/>\u6a31\u82b1\u7684\u989c\u8272\u901a\u5e38\u662f\u6de1\u7c89\u8272\u6216\u767d\u8272\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528RGB\u503c\u6765\u5b9a\u4e49\u8fd9\u4e9b\u989c\u8272\u3002\u5728\u8bbe\u8ba1\u82b1\u74e3\u65f6\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4e0d\u540c\u7684\u5f62\u72b6\uff0c\u6bd4\u5982\u5706\u5f62\u6216\u692d\u5706\u5f62\uff0c\u4ee5\u8fbe\u5230\u66f4\u81ea\u7136\u7684\u6548\u679c\u3002\u4e0d\u65ad\u8c03\u6574\u53c2\u6570\uff0c\u76f4\u5230\u83b7\u5f97\u7406\u60f3\u7684\u89c6\u89c9\u6548\u679c\u3002\u540c\u65f6\uff0c\u53c2\u8003\u771f\u5b9e\u6a31\u82b1\u7684\u56fe\u7247\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u628a\u63e1\u8272\u5f69\u548c\u5f62\u72b6\u7684\u642d\u914d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u753b\u6a31\u82b1\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u3001Turtle\u5e93\u3001PIL\u5e93\u7b49\u5b9e\u73b0\uff0c\u4f7f\u7528Matplotl [&hellip;]","protected":false},"author":3,"featured_media":942165,"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\/942164"}],"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=942164"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/942164\/revisions"}],"predecessor-version":[{"id":942168,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/942164\/revisions\/942168"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/942165"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=942164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=942164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=942164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}