{"id":1094105,"date":"2025-01-08T14:37:50","date_gmt":"2025-01-08T06:37:50","guid":{"rendered":""},"modified":"2025-01-08T14:37:53","modified_gmt":"2025-01-08T06:37:53","slug":"python%e5%a6%82%e4%bd%95%e5%88%b6%e4%bd%9c%e4%ba%94%e6%98%9f%e7%ba%a2%e6%97%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094105.html","title":{"rendered":"Python\u5982\u4f55\u5236\u4f5c\u4e94\u661f\u7ea2\u65d7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210353\/e444704d-df59-48b5-8a57-2bd8154086f2.webp\" alt=\"Python\u5982\u4f55\u5236\u4f5c\u4e94\u661f\u7ea2\u65d7\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u5236\u4f5c\u4e94\u661f\u7ea2\u65d7<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u5236\u4f5c\u4e94\u661f\u7ea2\u65d7\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u70b9\u6765\u5b9e\u73b0\uff1a\u4f7f\u7528matplotlib\u5e93\u7ed8\u56fe\u3001\u5b9a\u4e49\u7ea2\u65d7\u80cc\u666f\u548c\u4e94\u89d2\u661f\u7684\u989c\u8272\u3001\u7ed8\u5236\u4e94\u89d2\u661f\u7684\u51fd\u6570\u3001\u786e\u5b9a\u4e94\u89d2\u661f\u7684\u5750\u6807\u4f4d\u7f6e\u3001\u7ed8\u5236\u5e76\u663e\u793a\u6574\u4e2a\u56fe\u50cf\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5176\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\u2014\u2014\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\u5e76\u8be6\u7ec6\u63cf\u8ff0\u5404\u4e2a\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528matplotlib\u5e93\u7ed8\u56fe<\/strong><\/p>\n<\/p>\n<p><p>matplotlib\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684Python\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u5404\u79cd\u56fe\u8868\u548c\u56fe\u5f62\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u5b83\u6765\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5matplotlib\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\u5b83\uff0c\u53ef\u4ee5\u901a\u8fc7\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>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5bfc\u5165matplotlib\u5e76\u5f00\u59cb\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u5b9a\u4e49\u7ea2\u65d7\u80cc\u666f\u548c\u4e94\u89d2\u661f\u7684\u989c\u8272<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u661f\u7ea2\u65d7\u7684\u80cc\u666f\u989c\u8272\u4e3a\u7ea2\u8272\uff0c\u4e94\u89d2\u661f\u7684\u989c\u8272\u4e3a\u9ec4\u8272\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u989c\u8272\u4ee3\u7801\u6765\u5b9a\u4e49\u8fd9\u4e24\u79cd\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">background_color = &#39;#FF0000&#39;  # \u7ea2\u8272<\/p>\n<p>star_color = &#39;#FFFF00&#39;  # \u9ec4\u8272<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e09\u3001\u7ed8\u5236\u4e94\u89d2\u661f\u7684\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u7ed8\u5236\u4e94\u89d2\u661f\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u751f\u6210\u4e94\u89d2\u661f\u7684\u9876\u70b9\u5750\u6807\u3002\u4e94\u89d2\u661f\u6709\u4e94\u4e2a\u9876\u70b9\uff0c\u5206\u522b\u8fde\u63a5\u5f62\u6210\u4e00\u4e2a\u4e94\u89d2\u661f\u5f62\u72b6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6781\u5750\u6807\u7cfb\u6765\u8ba1\u7b97\u4e94\u89d2\u661f\u7684\u9876\u70b9\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def draw_star(center, radius, color):<\/p>\n<p>    theta = np.linspace(0, 2 * np.pi, 6)<\/p>\n<p>    x = center[0] + radius * np.cos(theta)<\/p>\n<p>    y = center[1] + radius * np.sin(theta)<\/p>\n<p>    xy = np.column_stack([x, y])<\/p>\n<p>    star = plt.Polygon(xy, color=color)<\/p>\n<p>    plt.gca().add_patch(star)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u786e\u5b9a\u4e94\u89d2\u661f\u7684\u5750\u6807\u4f4d\u7f6e<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u661f\u7ea2\u65d7\u4e0a\u6709\u4e00\u4e2a\u5927\u4e94\u89d2\u661f\u548c\u56db\u4e2a\u5c0f\u4e94\u89d2\u661f\u3002\u5927\u4e94\u89d2\u661f\u4f4d\u4e8e\u65d7\u5e1c\u7684\u5de6\u4e0a\u89d2\uff0c\u5c0f\u4e94\u89d2\u661f\u56f4\u7ed5\u5927\u4e94\u89d2\u661f\u6392\u5217\u3002\u6211\u4eec\u9700\u8981\u786e\u5b9a\u8fd9\u4e9b\u4e94\u89d2\u661f\u7684\u5750\u6807\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5927\u4e94\u89d2\u661f\u7684\u4e2d\u5fc3\u5750\u6807\u548c\u534a\u5f84<\/p>\n<p>big_star_center = (0.25, 0.75)<\/p>\n<p>big_star_radius = 0.1<\/p>\n<h2><strong>\u5c0f\u4e94\u89d2\u661f\u7684\u4e2d\u5fc3\u5750\u6807\u548c\u534a\u5f84<\/strong><\/h2>\n<p>small_star_centers = [<\/p>\n<p>    (0.35, 0.85),<\/p>\n<p>    (0.45, 0.75),<\/p>\n<p>    (0.45, 0.60),<\/p>\n<p>    (0.35, 0.50)<\/p>\n<p>]<\/p>\n<p>small_star_radius = 0.05<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e94\u3001\u7ed8\u5236\u5e76\u663e\u793a\u6574\u4e2a\u56fe\u50cf<\/strong><\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b9a\u4e49\u597d\u7684\u51fd\u6570\u548c\u5750\u6807\u4f4d\u7f6e\u6765\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\uff0c\u5e76\u663e\u793a\u6574\u4e2a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u56fe\u50cf\u80cc\u666f\u989c\u8272<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.set_facecolor(background_color)<\/p>\n<h2><strong>\u7ed8\u5236\u5927\u4e94\u89d2\u661f<\/strong><\/h2>\n<p>draw_star(big_star_center, big_star_radius, star_color)<\/p>\n<h2><strong>\u7ed8\u5236\u5c0f\u4e94\u89d2\u661f<\/strong><\/h2>\n<p>for center in small_star_centers:<\/p>\n<p>    draw_star(center, small_star_radius, star_color)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u50cf\u5927\u5c0f\u548c\u9690\u85cf\u5750\u6807\u8f74<\/strong><\/h2>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u548cmatplotlib\u5e93\u6210\u529f\u5730\u7ed8\u5236\u51fa\u4e94\u661f\u7ea2\u65d7\u3002<\/strong>\u8fd9\u4e0d\u4ec5\u5c55\u793a\u4e86Python\u5728\u56fe\u5f62\u7ed8\u5236\u65b9\u9762\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u51e0\u4f55\u56fe\u5f62\u7684\u7ed8\u5236\u539f\u7406\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u6df1\u5165\u63a2\u8ba8\u6bcf\u4e2a\u6b65\u9aa4\u7684\u7ec6\u8282\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528matplotlib\u5e93\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>matplotlib\u662f\u4e00\u4e2aPython\u76842D\u7ed8\u56fe\u5e93\uff0c\u5b83\u80fd\u591f\u751f\u6210\u5404\u79cd\u683c\u5f0f\u7684\u56fe\u8868\u548c\u56fe\u5f62\u3002\u5b83\u7684\u8bbe\u8ba1\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u751f\u6210\u9ad8\u8d28\u91cf\u7684\u56fe\u5f62\u3002\u4f7f\u7528matplotlib\u5e93\u8fdb\u884c\u7ed8\u56fe\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5bfc\u5165<code>pyplot<\/code>\u6a21\u5757\uff0c\u5e76\u4f7f\u7528\u5b83\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u7ed8\u56fe\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\u65f6\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6e\u56fe\u50cf\u7684\u80cc\u666f\u989c\u8272\u3002\u901a\u8fc7<code>plt.subplots()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u56fe\u5f62\u548c\u5750\u6807\u8f74\u7684\u7ed8\u56fe\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>set_facecolor()<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u56fe\u50cf\u7684\u80cc\u666f\u989c\u8272\u3002<\/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\u7ed8\u56fe\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u50cf\u80cc\u666f\u989c\u8272<\/strong><\/h2>\n<p>ax.set_facecolor(&#39;#FF0000&#39;)  # \u7ea2\u8272<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u4e49\u7ea2\u65d7\u80cc\u666f\u548c\u4e94\u89d2\u661f\u7684\u989c\u8272<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u7ea2\u65d7\u7684\u80cc\u666f\u989c\u8272\u548c\u4e94\u89d2\u661f\u7684\u989c\u8272\u3002\u4e94\u661f\u7ea2\u65d7\u7684\u80cc\u666f\u989c\u8272\u4e3a\u7ea2\u8272\uff0c\u4e94\u89d2\u661f\u7684\u989c\u8272\u4e3a\u9ec4\u8272\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u989c\u8272\u4ee3\u7801\u6765\u5b9a\u4e49\u8fd9\u4e24\u79cd\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">background_color = &#39;#FF0000&#39;  # \u7ea2\u8272<\/p>\n<p>star_color = &#39;#FFFF00&#39;  # \u9ec4\u8272<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u989c\u8272\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u7ed8\u5236\u51fa\u7684\u56fe\u50cf\u989c\u8272\u51c6\u786e\u65e0\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u4e94\u89d2\u661f\u7684\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u7ed8\u5236\u4e94\u89d2\u661f\u662f\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\u7684\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00\u3002\u4e94\u89d2\u661f\u6709\u4e94\u4e2a\u9876\u70b9\uff0c\u5206\u522b\u8fde\u63a5\u5f62\u6210\u4e00\u4e2a\u4e94\u89d2\u661f\u5f62\u72b6\u3002\u4e3a\u4e86\u7ed8\u5236\u4e94\u89d2\u661f\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u751f\u6210\u4e94\u89d2\u661f\u7684\u9876\u70b9\u5750\u6807\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u89d2\u661f\u7684\u9876\u70b9\u5750\u6807\u53ef\u4ee5\u901a\u8fc7\u6781\u5750\u6807\u7cfb\u6765\u8ba1\u7b97\u3002\u6781\u5750\u6807\u7cfb\u4e2d\u7684\u6bcf\u4e2a\u9876\u70b9\u53ef\u4ee5\u8868\u793a\u4e3a\u89d2\u5ea6\u548c\u534a\u5f84\u7684\u7ec4\u5408\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u4e2d\u7684<code>linspace<\/code>\u51fd\u6570\u6765\u751f\u6210\u4e94\u89d2\u661f\u7684\u9876\u70b9\u89d2\u5ea6\uff0c\u7136\u540e\u4f7f\u7528<code>cos<\/code>\u548c<code>sin<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u9876\u70b9\u7684x\u548cy\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def draw_star(center, radius, color):<\/p>\n<p>    theta = np.linspace(0, 2 * np.pi, 6)<\/p>\n<p>    x = center[0] + radius * np.cos(theta)<\/p>\n<p>    y = center[1] + radius * np.sin(theta)<\/p>\n<p>    xy = np.column_stack([x, y])<\/p>\n<p>    star = plt.Polygon(xy, color=color)<\/p>\n<p>    plt.gca().add_patch(star)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u751f\u6210\u4e94\u89d2\u661f\u7684\u9876\u70b9\u89d2\u5ea6\uff0c\u7136\u540e\u8ba1\u7b97\u9876\u70b9\u7684x\u548cy\u5750\u6807\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8fd9\u4e9b\u9876\u70b9\u5750\u6807\u7ec4\u5408\u6210\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528<code>plt.Polygon<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u591a\u8fb9\u5f62\u5bf9\u8c61\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2a\u591a\u8fb9\u5f62\u5bf9\u8c61\u6dfb\u52a0\u5230\u5f53\u524d\u5750\u6807\u8f74\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u786e\u5b9a\u4e94\u89d2\u661f\u7684\u5750\u6807\u4f4d\u7f6e<\/h3>\n<\/p>\n<p><p>\u4e94\u661f\u7ea2\u65d7\u4e0a\u6709\u4e00\u4e2a\u5927\u4e94\u89d2\u661f\u548c\u56db\u4e2a\u5c0f\u4e94\u89d2\u661f\u3002\u5927\u4e94\u89d2\u661f\u4f4d\u4e8e\u65d7\u5e1c\u7684\u5de6\u4e0a\u89d2\uff0c\u5c0f\u4e94\u89d2\u661f\u56f4\u7ed5\u5927\u4e94\u89d2\u661f\u6392\u5217\u3002\u6211\u4eec\u9700\u8981\u786e\u5b9a\u8fd9\u4e9b\u4e94\u89d2\u661f\u7684\u5750\u6807\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><p>\u5927\u4e94\u89d2\u661f\u7684\u4e2d\u5fc3\u5750\u6807\u53ef\u4ee5\u8bbe\u7f6e\u4e3a(0.25, 0.75)\uff0c\u534a\u5f84\u4e3a0.1\u3002\u5c0f\u4e94\u89d2\u661f\u7684\u4e2d\u5fc3\u5750\u6807\u5206\u522b\u8bbe\u7f6e\u4e3a(0.35, 0.85)\u3001(0.45, 0.75)\u3001(0.45, 0.60)\u548c(0.35, 0.50)\uff0c\u534a\u5f84\u4e3a0.05\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5927\u4e94\u89d2\u661f\u7684\u4e2d\u5fc3\u5750\u6807\u548c\u534a\u5f84<\/p>\n<p>big_star_center = (0.25, 0.75)<\/p>\n<p>big_star_radius = 0.1<\/p>\n<h2><strong>\u5c0f\u4e94\u89d2\u661f\u7684\u4e2d\u5fc3\u5750\u6807\u548c\u534a\u5f84<\/strong><\/h2>\n<p>small_star_centers = [<\/p>\n<p>    (0.35, 0.85),<\/p>\n<p>    (0.45, 0.75),<\/p>\n<p>    (0.45, 0.60),<\/p>\n<p>    (0.35, 0.50)<\/p>\n<p>]<\/p>\n<p>small_star_radius = 0.05<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u8fd9\u4e9b\u5750\u6807\u548c\u5927\u5c0f\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u4e94\u89d2\u661f\u7684\u4f4d\u7f6e\u548c\u6bd4\u4f8b\u7b26\u5408\u4e94\u661f\u7ea2\u65d7\u7684\u8bbe\u8ba1\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed8\u5236\u5e76\u663e\u793a\u6574\u4e2a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b9a\u4e49\u597d\u7684\u51fd\u6570\u548c\u5750\u6807\u4f4d\u7f6e\u6765\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\uff0c\u5e76\u663e\u793a\u6574\u4e2a\u56fe\u50cf\u3002\u6211\u4eec\u9996\u5148\u8bbe\u7f6e\u56fe\u50cf\u7684\u80cc\u666f\u989c\u8272\uff0c\u7136\u540e\u7ed8\u5236\u5927\u4e94\u89d2\u661f\u548c\u5c0f\u4e94\u89d2\u661f\u3002\u6700\u540e\uff0c\u6211\u4eec\u8bbe\u7f6e\u56fe\u50cf\u7684\u5927\u5c0f\uff0c\u5e76\u9690\u85cf\u5750\u6807\u8f74\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u56fe\u50cf\u80cc\u666f\u989c\u8272<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.set_facecolor(background_color)<\/p>\n<h2><strong>\u7ed8\u5236\u5927\u4e94\u89d2\u661f<\/strong><\/h2>\n<p>draw_star(big_star_center, big_star_radius, star_color)<\/p>\n<h2><strong>\u7ed8\u5236\u5c0f\u4e94\u89d2\u661f<\/strong><\/h2>\n<p>for center in small_star_centers:<\/p>\n<p>    draw_star(center, small_star_radius, star_color)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u50cf\u5927\u5c0f\u548c\u9690\u85cf\u5750\u6807\u8f74<\/strong><\/h2>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u548cmatplotlib\u5e93\u6210\u529f\u5730\u7ed8\u5236\u51fa\u4e94\u661f\u7ea2\u65d7\u3002\u8fd9\u4e0d\u4ec5\u5c55\u793a\u4e86Python\u5728\u56fe\u5f62\u7ed8\u5236\u65b9\u9762\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u51e0\u4f55\u56fe\u5f62\u7684\u7ed8\u5236\u539f\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6539\u8fdb<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u6211\u4eec\u5df2\u7ecf\u6210\u529f\u7ed8\u5236\u51fa\u4e86\u4e94\u661f\u7ea2\u65d7\uff0c\u4f46\u4ecd\u7136\u6709\u4e00\u4e9b\u5730\u65b9\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6539\u8fdb\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u7684\u6ce8\u91ca\u548c\u8bf4\u660e\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u4ee3\u7801\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u8c03\u6574\u4e94\u89d2\u661f\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\uff0c\u4ee5\u786e\u4fdd\u56fe\u50cf\u66f4\u52a0\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u5176\u4ed6\u7ed8\u56fe\u5e93\uff08\u5982turtle\u3001pygame\u7b49\uff09\u6765\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\u3002\u4e0d\u540c\u7684\u7ed8\u56fe\u5e93\u6709\u4e0d\u540c\u7684\u7279\u70b9\u548c\u4f18\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684Python\u4ee3\u7801\u793a\u4f8b\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u5e76\u751f\u6210\u4e94\u661f\u7ea2\u65d7\u56fe\u50cf\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>\u5b9a\u4e49\u989c\u8272<\/strong><\/h2>\n<p>background_color = &#39;#FF0000&#39;  # \u7ea2\u8272<\/p>\n<p>star_color = &#39;#FFFF00&#39;  # \u9ec4\u8272<\/p>\n<h2><strong>\u7ed8\u5236\u4e94\u89d2\u661f\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def draw_star(center, radius, color):<\/p>\n<p>    theta = np.linspace(0, 2 * np.pi, 6)<\/p>\n<p>    x = center[0] + radius * np.cos(theta)<\/p>\n<p>    y = center[1] + radius * np.sin(theta)<\/p>\n<p>    xy = np.column_stack([x, y])<\/p>\n<p>    star = plt.Polygon(xy, color=color)<\/p>\n<p>    plt.gca().add_patch(star)<\/p>\n<h2><strong>\u5927\u4e94\u89d2\u661f\u7684\u4e2d\u5fc3\u5750\u6807\u548c\u534a\u5f84<\/strong><\/h2>\n<p>big_star_center = (0.25, 0.75)<\/p>\n<p>big_star_radius = 0.1<\/p>\n<h2><strong>\u5c0f\u4e94\u89d2\u661f\u7684\u4e2d\u5fc3\u5750\u6807\u548c\u534a\u5f84<\/strong><\/h2>\n<p>small_star_centers = [<\/p>\n<p>    (0.35, 0.85),<\/p>\n<p>    (0.45, 0.75),<\/p>\n<p>    (0.45, 0.60),<\/p>\n<p>    (0.35, 0.50)<\/p>\n<p>]<\/p>\n<p>small_star_radius = 0.05<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u50cf\u80cc\u666f\u989c\u8272<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.set_facecolor(background_color)<\/p>\n<h2><strong>\u7ed8\u5236\u5927\u4e94\u89d2\u661f<\/strong><\/h2>\n<p>draw_star(big_star_center, big_star_radius, star_color)<\/p>\n<h2><strong>\u7ed8\u5236\u5c0f\u4e94\u89d2\u661f<\/strong><\/h2>\n<p>for center in small_star_centers:<\/p>\n<p>    draw_star(center, small_star_radius, star_color)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u50cf\u5927\u5c0f\u548c\u9690\u85cf\u5750\u6807\u8f74<\/strong><\/h2>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.axis(&#39;off&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u4f60\u5c06\u770b\u5230\u4e00\u9762\u5b8c\u6574\u7684\u4e94\u661f\u7ea2\u65d7\u56fe\u50cf\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u56fe\u5f62\uff0c\u5982\u4f55\u8ba1\u7b97\u51e0\u4f55\u56fe\u5f62\u7684\u9876\u70b9\u5750\u6807\uff0c\u4ee5\u53ca\u5982\u4f55\u8bbe\u7f6e\u56fe\u50cf\u7684\u80cc\u666f\u989c\u8272\u548c\u9690\u85cf\u5750\u6807\u8f74\u3002\u8fd9\u4e9b\u77e5\u8bc6\u4e0d\u4ec5\u9002\u7528\u4e8e\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\uff0c\u8fd8\u53ef\u4ee5\u5e94\u7528\u4e8e\u5176\u4ed6\u5404\u79cd\u56fe\u5f62\u548c\u56fe\u8868\u7684\u7ed8\u5236\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u4f60\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3Python\u7684\u56fe\u5f62\u7ed8\u5236\u529f\u80fd\uff0c\u5e76\u80fd\u591f\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u77e5\u8bc6\u8fdb\u884c\u56fe\u5f62\u521b\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\u7684\u57fa\u672c\u6b65\u9aa4\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u56fe\u5f62\u5e93\uff0c\u5982Matplotlib\u6216Turtle\u3002\u9996\u5148\uff0c\u8bbe\u7f6e\u753b\u5e03\u7684\u80cc\u666f\u989c\u8272\u4e3a\u7ea2\u8272\u3002\u63a5\u7740\uff0c\u7ed8\u5236\u4e94\u9897\u661f\u661f\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u4f4d\u7f6e\u548c\u5927\u5c0f\u6765\u786e\u4fdd\u5b83\u4eec\u7684\u6bd4\u4f8b\u548c\u5bf9\u79f0\u6027\u7b26\u5408\u4e94\u661f\u7ea2\u65d7\u7684\u6807\u51c6\u3002\u6700\u540e\uff0c\u8fd0\u884c\u4ee3\u7801\u751f\u6210\u56fe\u50cf\u5e76\u4fdd\u5b58\u6216\u663e\u793a\u51fa\u6765\u3002<\/p>\n<p><strong>\u5728\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u661f\u661f\u7684\u6bd4\u4f8b\u548c\u4f4d\u7f6e\u51c6\u786e\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u661f\u661f\u7684\u6bd4\u4f8b\u548c\u4f4d\u7f6e\u51c6\u786e\uff0c\u53ef\u4ee5\u53c2\u8003\u4e94\u661f\u7ea2\u65d7\u7684\u6807\u51c6\u5c3a\u5bf8\u4e0e\u8bbe\u8ba1\u3002\u4f7f\u7528Python\u4e2d\u7684\u5750\u6807\u7cfb\u7edf\u6765\u7cbe\u786e\u5730\u5b9a\u4f4d\u661f\u661f\u7684\u4f4d\u7f6e\uff0c\u540c\u65f6\u53ef\u4ee5\u4f7f\u7528\u6570\u5b66\u8ba1\u7b97\u6765\u4fdd\u6301\u661f\u661f\u7684\u5927\u5c0f\u6bd4\u4f8b\u4e00\u81f4\u3002\u5bf9\u4e8e\u6bcf\u9897\u661f\u661f\u7684\u89d2\u5ea6\u548c\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u4e09\u89d2\u51fd\u6570\u6765\u5e2e\u52a9\u786e\u5b9a\u5b83\u4eec\u5728\u753b\u5e03\u4e0a\u7684\u786e\u5207\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5176\u4ed6\u5e93\u7ed8\u5236\u4e94\u661f\u7ea2\u65d7\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff0c\u9664\u4e86Matplotlib\u548cTurtle\u4e4b\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528Pygame\u6216Pillow\u7b49\u5176\u4ed6\u56fe\u5f62\u5e93\u3002Pygame\u9002\u5408\u521b\u5efa\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u56fe\u5f62\uff0c\u800cPillow\u5219\u66f4\u9002\u5408\u4e8e\u56fe\u50cf\u5904\u7406\u3002\u6839\u636e\u60a8\u7684\u9700\u6c42\u548c\u9879\u76ee\u7c7b\u578b\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u5e26\u6765\u4e0d\u540c\u7684\u7ed8\u56fe\u4f53\u9a8c\u548c\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5236\u4f5c\u4e94\u661f\u7ea2\u65d7 \u4f7f\u7528Python\u5236\u4f5c\u4e94\u661f\u7ea2\u65d7\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u70b9\u6765\u5b9e\u73b0\uff1a\u4f7f\u7528matplotlib\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1094110,"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\/1094105"}],"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=1094105"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094105\/revisions"}],"predecessor-version":[{"id":1094114,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094105\/revisions\/1094114"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094110"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}