{"id":1069960,"date":"2025-01-08T10:53:52","date_gmt":"2025-01-08T02:53:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069960.html"},"modified":"2025-01-08T10:53:54","modified_gmt":"2025-01-08T02:53:54","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e5%87%ba%e4%b8%80%e4%b8%aa%e6%b0%b4%e6%bb%b4-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069960.html","title":{"rendered":"\u5982\u4f55\u7528Python\u753b\u51fa\u4e00\u4e2a\u6c34\u6ef4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100839\/43a900c0-3c4b-47d0-99ee-108a71fd7e4c.webp\" alt=\"\u5982\u4f55\u7528Python\u753b\u51fa\u4e00\u4e2a\u6c34\u6ef4\" \/><\/p>\n<p><p> <strong>\u7528Python\u753b\u51fa\u4e00\u4e2a\u6c34\u6ef4\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5229\u7528Matplotlib\u5e93\u521b\u5efa\u57fa\u7840\u56fe\u5f62\u3001\u4f7f\u7528Numpy\u751f\u6210\u6570\u636e\u70b9\u3001\u8c03\u6574\u56fe\u5f62\u53c2\u6570\u3001\u6dfb\u52a0\u989c\u8272\u548c\u900f\u660e\u5ea6\u3001\u4f18\u5316\u56fe\u5f62\u663e\u793a\u6548\u679c<\/strong>\u3002\u9996\u5148\uff0c\u4f7f\u7528Matplotlib\u5e93\u548cNumpy\u751f\u6210\u6570\u636e\u70b9\uff0c\u7136\u540e\u8c03\u6574\u56fe\u5f62\u7684\u989c\u8272\u548c\u900f\u660e\u5ea6\uff0c\u6700\u540e\u4f18\u5316\u56fe\u5f62\u7684\u663e\u793a\u6548\u679c\uff0c\u4f7f\u5176\u66f4\u50cf\u4e00\u4e2a\u6c34\u6ef4\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u7ed8\u5236\u56fe\u5f62\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3002Matplotlib\u548cNumpy\u662f\u4e24\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5e93\uff0c\u5206\u522b\u7528\u4e8e\u7ed8\u56fe\u548c\u6570\u636e\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8fd9\u4e9b\u5e93\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<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u751f\u6210\u6c34\u6ef4\u5f62\u72b6\u7684\u57fa\u672c\u6570\u636e\u70b9<\/p>\n<\/p>\n<p><p>\u6c34\u6ef4\u5f62\u72b6\u7684\u56fe\u5f62\u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u51fd\u6570\u6765\u751f\u6210\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\u65b9\u7a0b\u6765\u751f\u6210\u6c34\u6ef4\u5f62\u72b6\u7684\u66f2\u7ebf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Numpy\u751f\u6210\u6570\u636e\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">theta = np.linspace(0, 2 * np.pi, 100)<\/p>\n<p>r = 1 - np.sin(theta)<\/p>\n<p>x = r * np.cos(theta)<\/p>\n<p>y = r * np.sin(theta)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>theta<\/code> \u662f\u4ece0\u52302\u03c0\u7684100\u4e2a\u7b49\u95f4\u8ddd\u70b9\uff0c<code>r<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u6c34\u6ef4\u5f62\u72b6\u7684\u534a\u5f84\u7684\u6570\u7ec4\u3002<code>x<\/code> \u548c <code>y<\/code> \u5206\u522b\u662f\u6c34\u6ef4\u5f62\u72b6\u7684\u6a2a\u7eb5\u5750\u6807\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7ed8\u5236\u57fa\u7840\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684 <code>plot<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u751f\u6210\u7684 <code>x<\/code> \u548c <code>y<\/code> \u6570\u636e\u70b9\u7ed8\u5236\u51fa\u6765\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>plt.axis(&#39;equal&#39;)<\/code> \u786e\u4fddx\u8f74\u548cy\u8f74\u7684\u6bd4\u4f8b\u76f8\u540c\uff0c\u4f7f\u56fe\u5f62\u4e0d\u5931\u771f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6dfb\u52a0\u989c\u8272\u548c\u900f\u660e\u5ea6<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u56fe\u5f62\u66f4\u50cf\u4e00\u4e2a\u6c34\u6ef4\uff0c\u53ef\u4ee5\u6dfb\u52a0\u586b\u5145\u989c\u8272\u548c\u900f\u660e\u5ea6\u3002\u4f7f\u7528 <code>fill<\/code> \u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.fill(x, y, &#39;b&#39;, alpha=0.5)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>&#39;b&#39;<\/code> \u8868\u793a\u84dd\u8272\uff0c<code>alpha=0.5<\/code> \u8bbe\u7f6e\u900f\u660e\u5ea6\u4e3a50%\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f18\u5316\u56fe\u5f62\u663e\u793a\u6548\u679c<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u56fe\u5f62\u66f4\u903c\u771f\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u8c03\u6574\u56fe\u5f62\u7684\u53c2\u6570\u548c\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6dfb\u52a0\u8fb9\u6846\u3001\u8c03\u6574\u989c\u8272\u6e10\u53d8\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p>ax.fill(x, y, &#39;b&#39;, alpha=0.5, edgecolor=&#39;black&#39;, linewidth=2)<\/p>\n<h2><strong>\u6dfb\u52a0\u6e10\u53d8\u6548\u679c<\/strong><\/h2>\n<p>for i in range(1, 5):<\/p>\n<p>    ax.fill(x * i \/ 5, y * i \/ 5, &#39;b&#39;, alpha=0.5\/i, edgecolor=&#39;black&#39;, linewidth=2)<\/p>\n<p>ax.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u4e86 <code>subplots<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u5f62\u548c\u5750\u6807\u8f74\u5bf9\u8c61\uff0c\u5e76\u5728\u56fe\u5f62\u4e0a\u6dfb\u52a0\u4e86\u591a\u5c42\u6e10\u53d8\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u4e2a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7ed8\u5236\u51fa\u4e00\u4e2a\u903c\u771f\u7684\u6c34\u6ef4\u5f62\u72b6\u3002\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u751f\u6210\u6c34\u6ef4\u5f62\u72b6\u7684\u57fa\u672c\u6570\u636e\u70b9\u3001\u7ed8\u5236\u57fa\u7840\u56fe\u5f62\u3001\u6dfb\u52a0\u989c\u8272\u548c\u900f\u660e\u5ea6\u3001\u4f18\u5316\u56fe\u5f62\u663e\u793a\u6548\u679c\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u7ed8\u5236\u51fa\u5404\u79cd\u5f62\u72b6\u7684\u56fe\u5f62\uff0c\u4e0d\u4ec5\u9650\u4e8e\u6c34\u6ef4\u5f62\u72b6\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u6c34\u6ef4\u5f62\u72b6\u7684\u56fe\u50cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7ed8\u56fe\u5e93\u5982Matplotlib\u548cPygame\u6765\u7ed8\u5236\u6c34\u6ef4\u5f62\u72b6\u3002Matplotlib\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u6c34\u6ef4\u7684\u65b9\u7a0b\u6765\u7ed8\u5236\uff1b\u800cPygame\u5219\u5141\u8bb8\u60a8\u66f4\u7075\u6d3b\u5730\u521b\u5efa\u52a8\u753b\u548c\u4e92\u52a8\u6548\u679c\u3002\u60a8\u53ef\u4ee5\u5148\u5b9a\u4e49\u6c34\u6ef4\u7684\u8f6e\u5ed3\uff0c\u7136\u540e\u7528\u586b\u5145\u989c\u8272\u6765\u589e\u5f3a\u6548\u679c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u66f4\u597d\u5730\u7ed8\u5236\u6c34\u6ef4\uff1f<\/strong><br \/>\u7ed8\u5236\u6c34\u6ef4\u7684\u5e38\u7528\u5e93\u5305\u62ecMatplotlib\u3001Pygame\u548cTurtle\u3002Matplotlib\u9002\u5408\u9759\u6001\u56fe\u5f62\uff0cPygame\u66f4\u9002\u5408\u52a8\u6001\u6548\u679c\u548c\u6e38\u620f\u5f00\u53d1\uff0c\u800cTurtle\u5219\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u7ed8\u56fe\u63a5\u53e3\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002\u6839\u636e\u60a8\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u8ba9\u7ed8\u56fe\u8fc7\u7a0b\u66f4\u52a0\u987a\u5229\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u5347\u6211\u7ed8\u5236\u6c34\u6ef4\u6548\u679c\u7684\u89c6\u89c9\u6548\u679c\uff1f<\/strong><br \/>\u4e3a\u63d0\u5347\u6c34\u6ef4\u6548\u679c\u7684\u89c6\u89c9\u6548\u679c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6e10\u53d8\u8272\u3001\u900f\u660e\u5ea6\u548c\u9634\u5f71\u6548\u679c\u3002\u4f8b\u5982\uff0c\u5728Matplotlib\u4e2d\u4f7f\u7528\u900f\u660e\u5ea6\u8bbe\u7f6e\u548c\u6e10\u53d8\u586b\u5145\uff0c\u53ef\u4ee5\u8ba9\u6c34\u6ef4\u770b\u8d77\u6765\u66f4\u52a0\u771f\u5b9e\u3002\u6b64\u5916\uff0c\u6dfb\u52a0\u5149\u5f71\u6548\u679c\u548c\u53cd\u5c04\u4e5f\u80fd\u4f7f\u6c34\u6ef4\u770b\u8d77\u6765\u66f4\u751f\u52a8\u3002\u7814\u7a76\u4e00\u4e9b\u56fe\u5f62\u8bbe\u8ba1\u7684\u6280\u5de7\uff0c\u4e5f\u80fd\u5e2e\u52a9\u60a8\u63d0\u9ad8\u7ed8\u56fe\u7684\u7f8e\u89c2\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u753b\u51fa\u4e00\u4e2a\u6c34\u6ef4\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5229\u7528Matplotlib\u5e93\u521b\u5efa\u57fa\u7840\u56fe\u5f62\u3001\u4f7f\u7528Numpy\u751f\u6210\u6570\u636e\u70b9\u3001\u8c03\u6574\u56fe [&hellip;]","protected":false},"author":3,"featured_media":1069963,"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\/1069960"}],"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=1069960"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069960\/revisions"}],"predecessor-version":[{"id":1069964,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069960\/revisions\/1069964"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069963"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}