{"id":1116795,"date":"2025-01-08T18:19:50","date_gmt":"2025-01-08T10:19:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1116795.html"},"modified":"2025-01-08T18:19:54","modified_gmt":"2025-01-08T10:19:54","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e7%9a%84for%e5%be%aa%e7%8e%af%e7%94%bb%e5%a4%9a%e5%bc%a0%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1116795.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u7684for\u5faa\u73af\u753b\u591a\u5f20\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081007\/b6607ff4-dc7b-4b47-8085-4e529ffc8463.webp\" alt=\"\u5982\u4f55\u5229\u7528python\u7684for\u5faa\u73af\u753b\u591a\u5f20\u56fe\" \/><\/p>\n<p><p> <strong>\u5229\u7528Python\u7684for\u5faa\u73af\u753b\u591a\u5f20\u56fe\u7684\u6838\u5fc3\u89c2\u70b9\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u521b\u5efa\u5b50\u56fe\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6570\u636e\u3001\u8bbe\u7f6e\u6bcf\u5f20\u56fe\u7684\u5c5e\u6027\u3002<\/strong>\u5176\u4e2d\uff0c<strong>matplotlib<\/strong>\u5e93\u662f\u7ed8\u5236\u56fe\u5f62\u7684\u57fa\u7840\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u5f62\u7ed8\u5236\u529f\u80fd\u3002\u901a\u8fc7for\u5faa\u73af\uff0c\u53ef\u4ee5\u7b80\u6d01\u9ad8\u6548\u5730\u521b\u5efa\u591a\u5f20\u56fe\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Python\u7684for\u5faa\u73af\u7ed8\u5236\u591a\u5f20\u56fe\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528matplotlib\u5e93<\/h3>\n<\/p>\n<p><p><strong>matplotlib<\/strong>\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u4e8e\u521b\u5efa\u9759\u6001\u3001\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u7684\u56fe\u8868\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u56fe\u8868\u7c7b\u578b\uff0c\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u6570\u636e\u53ef\u89c6\u5316\u9700\u6c42\u3002\u4e3a\u4e86\u4f7f\u7528matplotlib\u5e93\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u8fdb\u884c\u5b89\u88c5\u548c\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5matplotlib\u5e93<\/p>\n<p>!pip install matplotlib<\/p>\n<h2><strong>\u5bfc\u5165matplotlib\u5e93<\/strong><\/h2>\n<p>import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u5b50\u56fe<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u591a\u5f20\u56fe\u65f6\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528matplotlib\u5e93\u4e2d\u7684<code>subplot<\/code>\u529f\u80fd\u6765\u521b\u5efa\u5b50\u56fe\u3002\u5b50\u56fe\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u4e00\u4e2a\u753b\u5e03\u4e0a\u6392\u5217\u591a\u4e2a\u56fe\u8868\uff0c\u4fbf\u4e8e\u6bd4\u8f83\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u753b\u5e03\uff0c\u5305\u542b2\u884c2\u5217\u7684\u5b50\u56fe<\/p>\n<p>fig, axs = plt.subplots(2, 2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u53ef\u4ee5\u904d\u5386\u6570\u636e\u96c6\uff0c\u5e76\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u7ed8\u5236\u4e00\u5f20\u56fe\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e\u96c6<\/p>\n<p>data = [([1, 2, 3], [4, 5, 6]), ([1, 2, 3], [6, 5, 4]), ([1, 2, 3], [4, 4, 4]), ([1, 2, 3], [6, 6, 6])]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6570\u636e\u96c6\uff0c\u5e76\u7ed8\u5236\u591a\u5f20\u56fe<\/strong><\/h2>\n<p>for i in range(4):<\/p>\n<p>    ax = axs[i \/\/ 2, i % 2]<\/p>\n<p>    ax.plot(data[i][0], data[i][1])<\/p>\n<p>    ax.set_title(f&#39;Plot {i+1}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8bbe\u7f6e\u6bcf\u5f20\u56fe\u7684\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u6bcf\u5f20\u56fe\u7684\u5c55\u793a\u6548\u679c\u66f4\u597d\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u56fe\u8868\u7684\u6807\u9898\u3001\u5750\u6807\u8f74\u6807\u7b7e\u3001\u56fe\u4f8b\u7b49\u5c5e\u6027\u3002\u901a\u8fc7\u5408\u7406\u8bbe\u7f6e\u56fe\u8868\u5c5e\u6027\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u6bcf\u5f20\u56fe\u7684\u5c5e\u6027<\/p>\n<p>for i in range(4):<\/p>\n<p>    ax = axs[i \/\/ 2, i % 2]<\/p>\n<p>    ax.plot(data[i][0], data[i][1], label=f&#39;Line {i+1}&#39;)<\/p>\n<p>    ax.set_title(f&#39;Plot {i+1}&#39;)<\/p>\n<p>    ax.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>    ax.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>    ax.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u5229\u7528Python\u7684for\u5faa\u73af\u7ed8\u5236\u591a\u5f20\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bfc\u5165matplotlib\u5e93<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u753b\u5e03\uff0c\u5305\u542b2\u884c2\u5217\u7684\u5b50\u56fe<\/strong><\/h2>\n<p>fig, axs = plt.subplots(2, 2)<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = [([1, 2, 3], [4, 5, 6]), ([1, 2, 3], [6, 5, 4]), ([1, 2, 3], [4, 4, 4]), ([1, 2, 3], [6, 6, 6])]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6570\u636e\u96c6\uff0c\u5e76\u7ed8\u5236\u591a\u5f20\u56fe<\/strong><\/h2>\n<p>for i in range(4):<\/p>\n<p>    ax = axs[i \/\/ 2, i % 2]<\/p>\n<p>    ax.plot(data[i][0], data[i][1], label=f&#39;Line {i+1}&#39;)<\/p>\n<p>    ax.set_title(f&#39;Plot {i+1}&#39;)<\/p>\n<p>    ax.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>    ax.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>    ax.legend()<\/p>\n<h2><strong>\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd<\/strong><\/h2>\n<p>plt.tight_layout()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/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\u53ef\u4ee5\u8f7b\u677e\u5730\u5229\u7528Python\u7684for\u5faa\u73af\u7ed8\u5236\u591a\u5f20\u56fe\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\u9ad8\u6548\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u7279\u70b9\u548c\u9700\u6c42\uff0c\u8fdb\u4e00\u6b65\u8c03\u6574\u56fe\u8868\u7684\u6837\u5f0f\u548c\u5e03\u5c40\uff0c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6570\u636e\u53ef\u89c6\u5316\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528for\u5faa\u73af\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u7ed3\u5408for\u5faa\u73af\u6765\u7ed8\u5236\u591a\u5f20\u56fe\u5f62\u3002\u901a\u8fc7\u5728\u5faa\u73af\u4e2d\u5b9a\u4e49\u6bcf\u4e2a\u56fe\u5f62\u7684\u7279\u5f81\uff0c\u6bd4\u5982\u6570\u636e\u3001\u6807\u9898\u3001\u6807\u7b7e\u7b49\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u751f\u6210\u591a\u5e45\u56fe\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.linspace(0, 10, 100)\n\nfor i in range(1, 5):\n    y = np.sin(x + i)  # \u751f\u6210\u4e0d\u540c\u7684y\u503c\n    plt.figure(i)\n    plt.plot(x, y)\n    plt.title(f&#39;Sine Wave Shifted by {i}&#39;)\n    plt.xlabel(&#39;X-axis&#39;)\n    plt.ylabel(&#39;Y-axis&#39;)\n    plt.grid(True)\n\nplt.show()\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u4f1a\u7ed8\u5236\u56db\u5f20\u4e0d\u540c\u7684\u6b63\u5f26\u6ce2\u56fe\u5f62\u3002<\/p>\n<p><strong>\u4f7f\u7528for\u5faa\u73af\u7ed8\u5236\u56fe\u5f62\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4f7f\u7528for\u5faa\u73af\u7ed8\u5236\u56fe\u5f62\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u672a\u6b63\u786e\u8c03\u7528<code>plt.show()<\/code>\uff0c\u5bfc\u81f4\u56fe\u5f62\u65e0\u6cd5\u663e\u793a\uff1b\u5728\u5faa\u73af\u5185\u672a\u521b\u5efa\u65b0\u56fe\u5f62\uff0c\u9020\u6210\u56fe\u5f62\u91cd\u53e0\uff1b\u6216\u8005\u5728\u5faa\u73af\u4e2d\u672a\u6b63\u786e\u8bbe\u7f6e\u56fe\u5f62\u7684\u6807\u9898\u548c\u6807\u7b7e\u3002\u786e\u4fdd\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u4f7f\u7528<code>plt.figure()<\/code>\u6765\u521b\u5efa\u65b0\u56fe\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728for\u5faa\u73af\u4e2d\u7ed8\u5236\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u5728for\u5faa\u73af\u4e2d\u7ed8\u5236\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u5f62\u3002\u901a\u8fc7\u5728\u5faa\u73af\u4e2d\u6839\u636e\u9700\u8981\u9009\u62e9\u4e0d\u540c\u7684\u7ed8\u56fe\u51fd\u6570\uff0c\u4f8b\u5982<code>plt.plot()<\/code>\u3001<code>plt.scatter()<\/code>\u3001<code>plt.bar()<\/code>\u7b49\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u56fe\u5f62\u7684\u7ed8\u5236\u3002\u53ea\u9700\u786e\u4fdd\u5728\u6bcf\u6b21\u7ed8\u56fe\u524d\u8c03\u7528<code>plt.figure()<\/code>\u4ee5\u907f\u514d\u56fe\u5f62\u91cd\u53e0\uff0c\u5e76\u5408\u7406\u5b89\u6392\u6570\u636e\u548c\u56fe\u5f62\u5c5e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5229\u7528Python\u7684for\u5faa\u73af\u753b\u591a\u5f20\u56fe\u7684\u6838\u5fc3\u89c2\u70b9\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u521b\u5efa\u5b50\u56fe\u3001\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1116806,"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\/1116795"}],"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=1116795"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116795\/revisions"}],"predecessor-version":[{"id":1116808,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116795\/revisions\/1116808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1116806"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1116795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1116795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1116795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}