{"id":1149126,"date":"2025-01-13T16:45:44","date_gmt":"2025-01-13T08:45:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149126.html"},"modified":"2025-01-13T16:45:47","modified_gmt":"2025-01-13T08:45:47","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bb%98%e5%88%b6%e5%a4%aa%e6%9e%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149126.html","title":{"rendered":"\u5982\u4f55\u7528python\u7ed8\u5236\u592a\u6781"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25175835\/7349f6c1-c4c1-44a0-9d05-c846f5e7f6d1.webp\" alt=\"\u5982\u4f55\u7528python\u7ed8\u5236\u592a\u6781\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u7ed8\u5236\u592a\u6781\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u3001numpy\u5e93\u6765\u751f\u6210\u592a\u6781\u56fe\u5f62\u3001\u8bbe\u7f6e\u989c\u8272\u4e0e\u6837\u5f0f\u3001\u6dfb\u52a0\u7ec6\u8282\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u7ed8\u5236\u592a\u6781\u56fe\u5f62\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5b9a\u4e49\u57fa\u672c\u53c2\u6570\u3001\u7ed8\u5236\u5927\u5706\u3001\u7ed8\u5236\u5c0f\u5706\u3001\u7ed8\u5236\u9c7c\u773c\u3002\u4e3a\u4e86\u7ed8\u5236\u4e00\u4e2a\u7cbe\u7f8e\u7684\u592a\u6781\u56fe\u5f62\uff0c\u9700\u8981\u5bf9\u8fd9\u4e9b\u6b65\u9aa4\u8fdb\u884c\u8be6\u7ec6\u7684\u5b9e\u73b0\u548c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u4e0e\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u5fc5\u8981\u7684Python\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\u5b83\u4eec\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install numpy matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u4e49\u57fa\u672c\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e9b\u57fa\u672c\u53c2\u6570\u6765\u63a7\u5236\u592a\u6781\u56fe\u7684\u7ed8\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u592a\u6781\u56fe\u7684\u534a\u5f84<\/p>\n<p>radius = 1<\/p>\n<h2><strong>\u5b9a\u4e49\u7ed8\u56fe\u7684\u5206\u8fa8\u7387<\/strong><\/h2>\n<p>resolution = 500<\/p>\n<h2><strong>\u751f\u6210\u89d2\u5ea6\u6570\u7ec4<\/strong><\/h2>\n<p>theta = np.linspace(0, 2 * np.pi, resolution)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u5927\u5706<\/h3>\n<\/p>\n<p><p>\u7ed8\u5236\u592a\u6781\u56fe\u7684\u5916\u5708\u5927\u5706\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u5927\u5706\u7684x\u548cy\u5750\u6807<\/p>\n<p>x_outer = radius * np.cos(theta)<\/p>\n<p>y_outer = radius * np.sin(theta)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.figure(figsize=(6, 6))<\/p>\n<p>plt.plot(x_outer, y_outer, &#39;k&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed8\u5236\u5c0f\u5706\u4e0e\u9c7c\u773c<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u7ed8\u5236\u592a\u6781\u56fe\u4e2d\u7684\u4e24\u4e2a\u5c0f\u5706\u548c\u9c7c\u773c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u5c0f\u5706\u7684\u534a\u5f84<\/p>\n<p>small_radius = radius \/ 2<\/p>\n<h2><strong>\u751f\u6210\u5c0f\u5706\u7684x\u548cy\u5750\u6807<\/strong><\/h2>\n<p>x_small1 = small_radius * np.cos(theta)<\/p>\n<p>y_small1 = small_radius * np.sin(theta) + small_radius<\/p>\n<p>x_small2 = small_radius * np.cos(theta)<\/p>\n<p>y_small2 = small_radius * np.sin(theta) - small_radius<\/p>\n<h2><strong>\u7ed8\u5236\u4e24\u4e2a\u5c0f\u5706<\/strong><\/h2>\n<p>plt.plot(x_small1, y_small1, &#39;k&#39;)<\/p>\n<p>plt.plot(x_small2, y_small2, &#39;k&#39;)<\/p>\n<h2><strong>\u586b\u5145\u5c0f\u5706\u7684\u989c\u8272<\/strong><\/h2>\n<p>plt.fill(x_small1, y_small1, &#39;black&#39;)<\/p>\n<p>plt.fill(x_small2, y_small2, &#39;white&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed8\u5236\u9c7c\u773c<\/h3>\n<\/p>\n<p><p>\u9c7c\u773c\u662f\u592a\u6781\u56fe\u7684\u70b9\u775b\u4e4b\u7b14\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u9c7c\u773c\u7684\u534a\u5f84<\/p>\n<p>eye_radius = radius \/ 10<\/p>\n<h2><strong>\u751f\u6210\u9c7c\u773c\u7684x\u548cy\u5750\u6807<\/strong><\/h2>\n<p>x_eye1 = eye_radius * np.cos(theta)<\/p>\n<p>y_eye1 = eye_radius * np.sin(theta) + small_radius<\/p>\n<p>x_eye2 = eye_radius * np.cos(theta)<\/p>\n<p>y_eye2 = eye_radius * np.sin(theta) - small_radius<\/p>\n<h2><strong>\u7ed8\u5236\u9c7c\u773c<\/strong><\/h2>\n<p>plt.plot(x_eye1, y_eye1, &#39;k&#39;)<\/p>\n<p>plt.plot(x_eye2, y_eye2, &#39;k&#39;)<\/p>\n<h2><strong>\u586b\u5145\u9c7c\u773c\u7684\u989c\u8272<\/strong><\/h2>\n<p>plt.fill(x_eye1, y_eye1, &#39;white&#39;)<\/p>\n<p>plt.fill(x_eye2, y_eye2, &#39;black&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8bbe\u7f6e\u56fe\u5f62\u6837\u5f0f\u4e0e\u663e\u793a<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u8c03\u6574\u56fe\u5f62\u7684\u6837\u5f0f\uff0c\u4f7f\u5176\u66f4\u52a0\u7f8e\u89c2\uff0c\u5e76\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6bd4\u4f8b<\/p>\n<p>plt.gca().set_aspect(&#39;equal&#39;)<\/p>\n<h2><strong>\u53bb\u9664\u5750\u6807\u8f74<\/strong><\/h2>\n<p>plt.axis(&#39;off&#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>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7528Python\u7ed8\u5236\u51fa\u4e00\u4e2a\u7cbe\u7f8e\u7684\u592a\u6781\u56fe\u3002\u5b8c\u6574\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5b9a\u4e49\u592a\u6781\u56fe\u7684\u534a\u5f84<\/strong><\/h2>\n<p>radius = 1<\/p>\n<h2><strong>\u5b9a\u4e49\u7ed8\u56fe\u7684\u5206\u8fa8\u7387<\/strong><\/h2>\n<p>resolution = 500<\/p>\n<h2><strong>\u751f\u6210\u89d2\u5ea6\u6570\u7ec4<\/strong><\/h2>\n<p>theta = np.linspace(0, 2 * np.pi, resolution)<\/p>\n<h2><strong>\u751f\u6210\u5927\u5706\u7684x\u548cy\u5750\u6807<\/strong><\/h2>\n<p>x_outer = radius * np.cos(theta)<\/p>\n<p>y_outer = radius * np.sin(theta)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.figure(figsize=(6, 6))<\/p>\n<p>plt.plot(x_outer, y_outer, &#39;k&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u5c0f\u5706\u7684\u534a\u5f84<\/strong><\/h2>\n<p>small_radius = radius \/ 2<\/p>\n<h2><strong>\u751f\u6210\u5c0f\u5706\u7684x\u548cy\u5750\u6807<\/strong><\/h2>\n<p>x_small1 = small_radius * np.cos(theta)<\/p>\n<p>y_small1 = small_radius * np.sin(theta) + small_radius<\/p>\n<p>x_small2 = small_radius * np.cos(theta)<\/p>\n<p>y_small2 = small_radius * np.sin(theta) - small_radius<\/p>\n<h2><strong>\u7ed8\u5236\u4e24\u4e2a\u5c0f\u5706<\/strong><\/h2>\n<p>plt.plot(x_small1, y_small1, &#39;k&#39;)<\/p>\n<p>plt.plot(x_small2, y_small2, &#39;k&#39;)<\/p>\n<h2><strong>\u586b\u5145\u5c0f\u5706\u7684\u989c\u8272<\/strong><\/h2>\n<p>plt.fill(x_small1, y_small1, &#39;black&#39;)<\/p>\n<p>plt.fill(x_small2, y_small2, &#39;white&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u9c7c\u773c\u7684\u534a\u5f84<\/strong><\/h2>\n<p>eye_radius = radius \/ 10<\/p>\n<h2><strong>\u751f\u6210\u9c7c\u773c\u7684x\u548cy\u5750\u6807<\/strong><\/h2>\n<p>x_eye1 = eye_radius * np.cos(theta)<\/p>\n<p>y_eye1 = eye_radius * np.sin(theta) + small_radius<\/p>\n<p>x_eye2 = eye_radius * np.cos(theta)<\/p>\n<p>y_eye2 = eye_radius * np.sin(theta) - small_radius<\/p>\n<h2><strong>\u7ed8\u5236\u9c7c\u773c<\/strong><\/h2>\n<p>plt.plot(x_eye1, y_eye1, &#39;k&#39;)<\/p>\n<p>plt.plot(x_eye2, y_eye2, &#39;k&#39;)<\/p>\n<h2><strong>\u586b\u5145\u9c7c\u773c\u7684\u989c\u8272<\/strong><\/h2>\n<p>plt.fill(x_eye1, y_eye1, &#39;white&#39;)<\/p>\n<p>plt.fill(x_eye2, y_eye2, &#39;black&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6bd4\u4f8b<\/strong><\/h2>\n<p>plt.gca().set_aspect(&#39;equal&#39;)<\/p>\n<h2><strong>\u53bb\u9664\u5750\u6807\u8f74<\/strong><\/h2>\n<p>plt.axis(&#39;off&#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>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u7528Python\u7ed8\u5236\u51fa\u4e00\u4e2a\u6807\u51c6\u7684\u592a\u6781\u56fe\u3002\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u548c\u6837\u5f0f\uff0c\u4f60\u8fd8\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u4e0d\u540c\u98ce\u683c\u7684\u592a\u6781\u56fe\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u592a\u6781\u56fe\u6240\u9700\u7684\u5e93\u548c\u73af\u5883\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u7ed8\u5236\u592a\u6781\u56fe\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u4e00\u4e9b\u56fe\u5f62\u7ed8\u5236\u5e93\uff0c\u5982Matplotlib\u548cNumPy\u3002Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u548c\u56fe\u5f62\u3002NumPy\u5219\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u8ba1\u7b97\u529f\u80fd\uff0c\u9002\u5408\u4e8e\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u3002\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4 <code>pip install matplotlib numpy<\/code> \u6765\u5b89\u88c5\u3002<\/p>\n<p><strong>2. \u7ed8\u5236\u592a\u6781\u56fe\u65f6\uff0c\u5982\u4f55\u8c03\u6574\u989c\u8272\u548c\u6837\u5f0f\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u592a\u6781\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u586b\u5145\u989c\u8272\u548c\u8fb9\u6846\u6837\u5f0f\u6765\u6539\u53d8\u56fe\u5f62\u7684\u5916\u89c2\u3002\u4f7f\u7528 <code>ax.fill<\/code> \u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a\u989c\u8272\uff0c\u53c2\u6570\u5982 <code>color=&#39;black&#39;<\/code> \u548c <code>color=&#39;white&#39;<\/code> \u53ef\u4ee5\u5206\u522b\u8bbe\u7f6e\u9ed1\u8272\u548c\u767d\u8272\u7684\u90e8\u5206\u3002\u6b64\u5916\uff0c\u5229\u7528 <code>edgecolor<\/code> \u548c <code>linewidth<\/code> \u53c2\u6570\u53ef\u4ee5\u8c03\u6574\u8fb9\u6846\u7684\u989c\u8272\u548c\u7ebf\u5bbd\uff0c\u8fd9\u6837\u53ef\u4ee5\u8ba9\u56fe\u5f62\u66f4\u5177\u4e2a\u6027\u5316\u3002<\/p>\n<p><strong>3. \u5982\u679c\u60f3\u8981\u5c06\u7ed8\u5236\u7684\u592a\u6781\u56fe\u4fdd\u5b58\u4e3a\u56fe\u7247\uff0c\u5e94\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>plt.savefig(&#39;filename.png&#39;)<\/code> \u65b9\u6cd5\u6765\u5c06\u7ed8\u5236\u7684\u592a\u6781\u56fe\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u6bd4\u5982 PNG\u3001JPEG \u6216 SVG\u3002\u786e\u4fdd\u5728\u8c03\u7528 <code>savefig<\/code> \u65b9\u6cd5\u4e4b\u524d\u8c03\u7528 <code>plt.show()<\/code>\uff0c\u4ee5\u786e\u4fdd\u56fe\u5f62\u6b63\u786e\u751f\u6210\u5e76\u663e\u793a\u51fa\u6765\u3002\u8c03\u6574 <code>dpi<\/code> \u53c2\u6570\u4e5f\u53ef\u4ee5\u63a7\u5236\u56fe\u7247\u7684\u5206\u8fa8\u7387\uff0c\u4ece\u800c\u63d0\u9ad8\u56fe\u50cf\u8d28\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u7ed8\u5236\u592a\u6781\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u3001numpy\u5e93\u6765\u751f\u6210\u592a\u6781\u56fe\u5f62\u3001\u8bbe\u7f6e\u989c\u8272\u4e0e\u6837\u5f0f\u3001\u6dfb\u52a0\u7ec6 [&hellip;]","protected":false},"author":3,"featured_media":1149130,"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\/1149126"}],"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=1149126"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149126\/revisions"}],"predecessor-version":[{"id":1149131,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149126\/revisions\/1149131"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149130"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}