{"id":1187483,"date":"2025-01-15T20:06:06","date_gmt":"2025-01-15T12:06:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187483.html"},"modified":"2025-01-15T20:06:09","modified_gmt":"2025-01-15T12:06:09","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e6%a8%aa%e5%90%91%e7%94%bb%e6%9d%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1187483.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u521b\u5efa\u6a2a\u5411\u753b\u677f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140050\/32ce6959-53cf-44ec-88c1-09a9e6121e6f.webp\" alt=\"python\u4e2d\u5982\u4f55\u521b\u5efa\u6a2a\u5411\u753b\u677f\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u521b\u5efa\u6a2a\u5411\u753b\u677f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u4e2d\u7684subplots\u529f\u80fd\u3001\u4f7f\u7528set_size_inches\u65b9\u6cd5\u8c03\u6574\u753b\u677f\u5c3a\u5bf8\u3001\u4f7f\u7528add_subplot\u65b9\u6cd5\u6dfb\u52a0\u6a2a\u5411\u5b50\u56fe\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4f7f\u7528subplots\u529f\u80fd\u521b\u5efa\u6a2a\u5411\u753b\u677f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u4f60\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>\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><h3>\u4e8c\u3001\u4f7f\u7528subplots\u529f\u80fd\u521b\u5efa\u6a2a\u5411\u753b\u677f<\/h3>\n<\/p>\n<p><p><strong>1. \u521b\u5efa\u57fa\u672c\u6a2a\u5411\u753b\u677f<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u6a2a\u5411\u753b\u677f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.subplots<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u548c\u4e00\u4e2a\u5b50\u56fe\u5bf9\u8c61\u7684\u6570\u7ec4\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>ncols<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u6a2a\u5411\u7684\u5b50\u56fe\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))<\/p>\n<h2><strong>\u5176\u4e2dnrows=1\u8868\u793a\u53ea\u6709\u4e00\u884c\uff0cncols=3\u8868\u793a\u6709\u4e09\u5217<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u8bbe\u7f6e\u753b\u677f\u5927\u5c0f<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>figsize<\/code>\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u56fe\u5f62\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u3002\u5bf9\u4e8e\u6a2a\u5411\u753b\u677f\uff0c\u6211\u4eec\u901a\u5e38\u5e0c\u671b\u5bbd\u5ea6\u6bd4\u9ad8\u5ea6\u5927\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5728\u6a2a\u5411\u753b\u677f\u4e0a\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p><strong>1. \u7b80\u5355\u7ed8\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u4e2a\u5b50\u56fe\u4e0a\u7ed8\u5236\u4e0d\u540c\u7684\u5185\u5bb9\u3002\u5047\u8bbe\u6211\u4eec\u60f3\u5728\u6bcf\u4e2a\u5b50\u56fe\u4e0a\u7ed8\u5236\u4e00\u6761\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>for i in range(3):<\/p>\n<p>    axs[i].plot(x, np.sin(x + i))<\/p>\n<p>    axs[i].set_title(f&#39;Subplot {i+1}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5b50\u56fe\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e\uff0c\u4f7f\u5f97\u56fe\u5f62\u66f4\u52a0\u6e05\u6670\u6613\u61c2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(3):<\/p>\n<p>    axs[i].plot(x, np.sin(x + i))<\/p>\n<p>    axs[i].set_title(f&#39;Subplot {i+1}&#39;)<\/p>\n<p>    axs[i].set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>    axs[i].set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u5b50\u56fe\u4e4b\u95f4\u7684\u9ed8\u8ba4\u95f4\u8ddd\u53ef\u80fd\u4e0d\u5408\u9002\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>plt.tight_layout<\/code>\u51fd\u6570\u6765\u81ea\u52a8\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\uff0c\u6216\u8005\u4f7f\u7528<code>fig.subplots_adjust<\/code>\u51fd\u6570\u624b\u52a8\u8c03\u6574\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.tight_layout()<\/p>\n<h2><strong>\u6216\u8005\u624b\u52a8\u8c03\u6574<\/strong><\/h2>\n<p>fig.subplots_adjust(wspace=0.3, hspace=0.3)<\/p>\n<h2><strong>\u5176\u4e2dwspace\u8868\u793a\u5b50\u56fe\u4e4b\u95f4\u7684\u5bbd\u5ea6\u95f4\u8ddd\uff0chspace\u8868\u793a\u9ad8\u5ea6\u95f4\u8ddd<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4fdd\u5b58\u548c\u5c55\u793a\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u56fe\u5f62\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u76f4\u63a5\u5c55\u793a\u51fa\u6765\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u5f62<\/p>\n<p>fig.savefig(&#39;horizontal_subplot.png&#39;)<\/p>\n<h2><strong>\u5c55\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528matplotlib\u5e93\u4e2d\u7684<code>subplots<\/code>\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u521b\u5efa\u6a2a\u5411\u753b\u677f\u3002\u901a\u8fc7\u8bbe\u7f6e<code>ncols<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u6a2a\u5411\u7684\u5b50\u56fe\u6570\u91cf\uff0c\u4f7f\u7528<code>figsize<\/code>\u53c2\u6570\u8bbe\u7f6e\u56fe\u5f62\u7684\u5927\u5c0f\uff0c\u5e76\u901a\u8fc7\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\u548c\u6dfb\u52a0\u6807\u9898\u3001\u6807\u7b7e\uff0c\u4f7f\u5f97\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u8bfb\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\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>\u521b\u5efa\u6a2a\u5411\u753b\u677f<\/strong><\/h2>\n<p>fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<h2><strong>\u5728\u6bcf\u4e2a\u5b50\u56fe\u4e0a\u7ed8\u5236\u4e0d\u540c\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>for i in range(3):<\/p>\n<p>    axs[i].plot(x, np.sin(x + i))<\/p>\n<p>    axs[i].set_title(f&#39;Subplot {i+1}&#39;)<\/p>\n<p>    axs[i].set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>    axs[i].set_ylabel(&#39;Y-axis&#39;)<\/p>\n<h2><strong>\u81ea\u52a8\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd<\/strong><\/h2>\n<p>plt.tight_layout()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>fig.savefig(&#39;horizontal_subplot.png&#39;)<\/p>\n<h2><strong>\u5c55\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u6a2a\u5411\u753b\u677f\uff0c\u5e76\u5728\u6bcf\u4e2a\u5b50\u56fe\u4e0a\u7ed8\u5236\u4e0d\u540c\u7684\u5185\u5bb9\u3002\u65e0\u8bba\u662f\u7528\u6765\u5c55\u793a\u6570\u636e\u5206\u6790\u7ed3\u679c\uff0c\u8fd8\u662f\u7528\u4e8e\u751f\u6210\u62a5\u544a\uff0c\u8fd9\u79cd\u65b9\u6cd5\u90fd\u80fd\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u6a2a\u5411\u753b\u677f\uff1f<\/strong><br \/>\u5728Python\u4e2d\u521b\u5efa\u6a2a\u5411\u753b\u677f\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u56fe\u5f62\u5e93\uff0c\u4f8b\u5982Tkinter\u3001Pygame\u6216Matplotlib\u3002Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u9002\u5408\u4e8e\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aCanvas\u5bf9\u8c61\u5e76\u8bbe\u7f6e\u5176\u5bbd\u5ea6\u5927\u4e8e\u9ad8\u5ea6\u6765\u5b9e\u73b0\u6a2a\u5411\u753b\u677f\u3002<\/p>\n<p><strong>\u5728\u521b\u5efa\u6a2a\u5411\u753b\u677f\u65f6\uff0c\u5982\u4f55\u8bbe\u7f6e\u753b\u677f\u7684\u5c3a\u5bf8\u548c\u80cc\u666f\u989c\u8272\uff1f<\/strong><br \/>\u5728Tkinter\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7Canvas\u7684<code>create_rectangle<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u753b\u677f\u7684\u5c3a\u5bf8\uff0c\u540c\u65f6\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u80cc\u666f\u989c\u8272\u6765\u7f8e\u5316\u753b\u677f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u521b\u5efaCanvas\u65f6\u4f20\u5165<code>bg<\/code>\u53c2\u6570\u6765\u5b9a\u4e49\u80cc\u666f\u989c\u8272\uff0c\u5e76\u901a\u8fc7<code>width<\/code>\u548c<code>height<\/code>\u53c2\u6570\u63a7\u5236\u753b\u677f\u7684\u5927\u5c0f\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u5728\u6a2a\u5411\u753b\u677f\u4e0a\u7ed8\u5236\u54ea\u4e9b\u7c7b\u578b\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u6a2a\u5411\u753b\u677f\u652f\u6301\u591a\u79cd\u56fe\u5f62\u7684\u7ed8\u5236\uff0c\u5305\u62ec\u76f4\u7ebf\u3001\u77e9\u5f62\u3001\u5706\u5f62\u3001\u6587\u672c\u548c\u591a\u8fb9\u5f62\u7b49\u3002\u4f7f\u7528Canvas\u5bf9\u8c61\u7684\u76f8\u5173\u65b9\u6cd5\uff0c\u5982<code>create_line<\/code>\u3001<code>create_rectangle<\/code>\u3001<code>create_oval<\/code>\u7b49\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e9b\u56fe\u5f62\u7684\u7ed8\u5236\u3002\u60a8\u8fd8\u53ef\u4ee5\u4e3a\u8fd9\u4e9b\u56fe\u5f62\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u548c\u7ebf\u6761\u6837\u5f0f\uff0c\u4ee5\u589e\u5f3a\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u521b\u5efa\u6a2a\u5411\u753b\u677f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u4e2d\u7684subplots\u529f\u80fd\u3001\u4f7f\u7528set_size [&hellip;]","protected":false},"author":3,"featured_media":1187494,"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\/1187483"}],"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=1187483"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187483\/revisions"}],"predecessor-version":[{"id":1187497,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187483\/revisions\/1187497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1187494"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1187483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1187483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1187483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}