{"id":1128439,"date":"2025-01-08T20:20:30","date_gmt":"2025-01-08T12:20:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128439.html"},"modified":"2025-01-08T20:20:32","modified_gmt":"2025-01-08T12:20:32","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%a1%a8%e7%a4%ba%e4%b8%80%e5%bc%a0%e5%9b%be%e7%9a%84%e5%9d%90%e6%a0%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1128439.html","title":{"rendered":"\u5982\u4f55\u7528python\u8868\u793a\u4e00\u5f20\u56fe\u7684\u5750\u6807"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095148\/dd9fa3da-6477-4e33-8f42-384202bd070d.webp\" alt=\"\u5982\u4f55\u7528python\u8868\u793a\u4e00\u5f20\u56fe\u7684\u5750\u6807\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u8868\u793a\u4e00\u5f20\u56fe\u7684\u5750\u6807\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\uff0c\u5982matplotlib\u3001PIL\u3001opencv\u7b49\u3002<\/strong>\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c<strong>matplotlib<\/strong>\u662f\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<strong>matplotlib\u7684plt.scatter()\u51fd\u6570<\/strong>\u6765\u7ed8\u5236\u5750\u6807\u70b9\uff0c\u5e76\u4f7f\u7528<strong>plt.annotate()\u51fd\u6570<\/strong>\u6765\u6807\u6ce8\u8fd9\u4e9b\u70b9\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u7528Python\u8868\u793a\u4e00\u5f20\u56fe\u7684\u5750\u6807\uff0c\u5e76\u5bf9\u5176\u4e2d\u7684<strong>plt.scatter()\u51fd\u6570<\/strong>\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528matplotlib\u7ed8\u5236\u5750\u6807\u70b9<\/strong><\/h2>\n<p><h2>1.1\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5e93<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\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><p>\u7136\u540e\uff0c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>1.2\u3001\u5b9a\u4e49\u5750\u6807\u70b9<\/h2>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u7ec4\u5750\u6807\u70b9\uff0c\u9700\u8981\u5728\u56fe\u4e0a\u8868\u793a\u8fd9\u4e9b\u70b9\u3002\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u5750\u6807\u70b9\u5b9a\u4e49\u4e3a\u5217\u8868\u6216\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>1.3\u3001\u4f7f\u7528plt.scatter()\u7ed8\u5236\u70b9<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528plt.scatter()\u51fd\u6570\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7ed8\u5236\u70b9\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.scatter(x, y, color=&#39;blue&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>plt.scatter()\u51fd\u6570<\/strong>\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u53c2\u6570\u6765\u8c03\u6574\u70b9\u7684\u989c\u8272\u3001\u5927\u5c0f\u3001\u5f62\u72b6\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u53c2\u6570\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>x, y<\/code>\uff1a\u8868\u793a\u70b9\u7684\u5750\u6807\u3002<\/li>\n<li><code>color<\/code>\uff1a\u8bbe\u7f6e\u70b9\u7684\u989c\u8272\u3002<\/li>\n<li><code>s<\/code>\uff1a\u8bbe\u7f6e\u70b9\u7684\u5927\u5c0f\u3002<\/li>\n<li><code>marker<\/code>\uff1a\u8bbe\u7f6e\u70b9\u7684\u5f62\u72b6\u3002<\/li>\n<\/ul>\n<p><h2>1.4\u3001\u6807\u6ce8\u5750\u6807\u70b9<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528plt.annotate()\u51fd\u6570\u53ef\u4ee5\u5728\u70b9\u65c1\u8fb9\u6dfb\u52a0\u6ce8\u91ca\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(len(x)):<\/p>\n<p>    plt.annotate(f&#39;({x[i]}, {y[i]})&#39;, (x[i], y[i]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>1.5\u3001\u663e\u793a\u56fe\u5f62<\/h2>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528plt.show()\u51fd\u6570\u6765\u663e\u793a\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528PIL\u7ed8\u5236\u5750\u6807\u70b9<\/strong><\/h2>\n<p><h2>2.1\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5e93<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PIL\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\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><p>\u7136\u540e\uff0c\u5bfc\u5165\u5fc5\u8981\u7684\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<p><h2>2.2\u3001\u521b\u5efa\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a7a\u767d\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">img = Image.new(&#39;RGB&#39;, (400, 400), color=&#39;white&#39;)<\/p>\n<p>draw = ImageDraw.Draw(img)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2.3\u3001\u7ed8\u5236\u70b9\u548c\u6807\u6ce8<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528draw.point()\u51fd\u6570\u6765\u7ed8\u5236\u70b9\uff0c\u4f7f\u7528draw.text()\u51fd\u6570\u6765\u6807\u6ce8\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">points = [(50, 50), (100, 100), (150, 150)]<\/p>\n<p>for point in points:<\/p>\n<p>    draw.point(point, fill=&#39;blue&#39;)<\/p>\n<p>    draw.text((point[0]+5, point[1]-5), f&#39;{point}&#39;, fill=&#39;black&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2.4\u3001\u663e\u793a\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>\u663e\u793a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">img.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528OpenCV\u7ed8\u5236\u5750\u6807\u70b9<\/strong><\/h2>\n<p><h2>3.1\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5e93<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86OpenCV\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\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 opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.2\u3001\u521b\u5efa\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a7a\u767d\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">img = np.ones((400, 400, 3), dtype=np.uint8) * 255<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.3\u3001\u7ed8\u5236\u70b9\u548c\u6807\u6ce8<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528cv2.circle()\u51fd\u6570\u6765\u7ed8\u5236\u70b9\uff0c\u4f7f\u7528cv2.putText()\u51fd\u6570\u6765\u6807\u6ce8\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">points = [(50, 50), (100, 100), (150, 150)]<\/p>\n<p>for point in points:<\/p>\n<p>    cv2.circle(img, point, 5, (255, 0, 0), -1)<\/p>\n<p>    cv2.putText(img, f&#39;{point}&#39;, (point[0]+5, point[1]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.4\u3001\u663e\u793a\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>\u663e\u793a\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cv2.imshow(&#39;Image&#39;, img)<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u7528Python\u8868\u793a\u4e00\u5f20\u56fe\u7684\u5750\u6807\uff0c\u4e3b\u8981\u4f7f\u7528\u4e86matplotlib\u3001PIL\u3001OpenCV\u4e09\u79cd\u4e0d\u540c\u7684\u5e93\u3002<strong>matplotlib<\/strong>\u662f\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\uff0c<strong>plt.scatter()\u51fd\u6570<\/strong>\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7ed8\u5236\u70b9\u56fe\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u53c2\u6570\u6765\u8c03\u6574\u70b9\u7684\u989c\u8272\u3001\u5927\u5c0f\u3001\u5f62\u72b6\u7b49\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u7ed8\u5236\u56fe\u5f62\u548c\u8868\u793a\u5750\u6807\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u56fe\u50cf\u7684\u5750\u6807\u4fe1\u606f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u50cf\u5904\u7406\u5e93\u5982OpenCV\u6216PIL\uff08Pillow\uff09\u6765\u83b7\u53d6\u56fe\u50cf\u7684\u5750\u6807\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528OpenCV\u7684<code>cv2.imread()<\/code>\u51fd\u6570\u8bfb\u53d6\u56fe\u50cf\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7<code>numpy<\/code>\u6570\u7ec4\u7d22\u5f15\u6765\u8bbf\u95ee\u7279\u5b9a\u50cf\u7d20\u7684\u5750\u6807\u3002PIL\u5e93\u4e5f\u63d0\u4f9b\u4e86<code>Image.open()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7<code>image.getpixel((x, y))<\/code>\u6765\u83b7\u53d6\u7279\u5b9a\u5750\u6807\u7684\u50cf\u7d20\u503c\u3002<\/p>\n<p><strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u5904\u7406\u56fe\u50cf\u5750\u6807\uff1f<\/strong><br \/>\u6709\u591a\u79cd\u5e93\u53ef\u7528\u4e8e\u5904\u7406\u56fe\u50cf\u5750\u6807\uff0c\u6700\u5e38\u7528\u7684\u662fOpenCV\u548cPIL\uff08Pillow\uff09\u3002OpenCV\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u5305\u62ec\u8bfb\u53d6\u3001\u4fee\u6539\u548c\u663e\u793a\u56fe\u50cf\u3002PIL\uff08Pillow\uff09\u5219\u66f4\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u8fdb\u884c\u57fa\u672c\u7684\u56fe\u50cf\u64cd\u4f5c\u3002Matplotlib\u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u5c24\u5176\u662f\u5728\u53ef\u89c6\u5316\u56fe\u50cf\u6570\u636e\u65f6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u56fe\u50cf\u5750\u6807\u70b9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u56fe\u50cf\u53ca\u5176\u5750\u6807\u70b9\u3002\u901a\u8fc7<code>plt.imshow()<\/code>\u51fd\u6570\u53ef\u4ee5\u663e\u793a\u56fe\u50cf\uff0c\u63a5\u7740\u4f7f\u7528<code>plt.plot(x, y, &#39;ro&#39;)<\/code>\u6765\u6807\u8bb0\u7279\u5b9a\u5750\u6807\u70b9\uff0c\u5176\u4e2d<code>&#39;ro&#39;<\/code>\u8868\u793a\u7ea2\u8272\u5706\u70b9\u3002\u8fd9\u6837\u53ef\u4ee5\u76f4\u89c2\u5730\u5728\u56fe\u50cf\u4e0a\u663e\u793a\u51fa\u7279\u5b9a\u7684\u5750\u6807\u4f4d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u8868\u793a\u4e00\u5f20\u56fe\u7684\u5750\u6807\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\uff0c\u5982matplotlib\u3001PIL\u3001opencv\u7b49\u3002\u5728\u5b9e\u9645\u64cd\u4f5c [&hellip;]","protected":false},"author":3,"featured_media":1128444,"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\/1128439"}],"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=1128439"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128439\/revisions"}],"predecessor-version":[{"id":1128446,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128439\/revisions\/1128446"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1128444"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1128439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1128439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1128439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}