{"id":995708,"date":"2024-12-27T09:07:11","date_gmt":"2024-12-27T01:07:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/995708.html"},"modified":"2024-12-27T09:07:14","modified_gmt":"2024-12-27T01:07:14","slug":"python%e5%a6%82%e4%bd%95%e7%bb%98%e5%88%b6%e5%a4%9a%e4%b8%aa%e5%9b%be%e5%bd%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/995708.html","title":{"rendered":"python\u5982\u4f55\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072209\/dd6ce76d-3c38-46bf-b42e-130f623068c1.webp\" alt=\"python\u5982\u4f55\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u4f7f\u7528Matplotlib\u5e93\u3001\u5229\u7528subplot\u529f\u80fd\u3001\u901a\u8fc7figure\u5bf9\u8c61\u7ba1\u7406\u591a\u4e2a\u56fe\u5f62<\/strong>\u3002\u5176\u4e2d\uff0c<strong>Matplotlib\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u7ba1\u7406\u591a\u4e2a\u56fe\u5f62\u3002<strong>subplot\u529f\u80fd<\/strong>\u5141\u8bb8\u5728\u540c\u4e00\u7a97\u53e3\u4e2d\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\uff0c\u6bcf\u4e2a\u5b50\u56fe\u53ef\u4ee5\u72ec\u7acb\u7ed8\u5236\u4e0d\u540c\u7684\u6570\u636e\u3002\u6b64\u5916\uff0c<strong>figure\u5bf9\u8c61<\/strong>\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u4e0d\u540c\u7684\u7a97\u53e3\u4e2d\u7ba1\u7406\u591a\u4e2a\u56fe\u5f62\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u53ca\u5176\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u5e93\u6982\u8ff0<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u5305\u62ec\u6298\u7ebf\u56fe\u3001\u6563\u70b9\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u76f4\u65b9\u56fe\u7b49\u3002Matplotlib\u7684\u6838\u5fc3\u5bf9\u8c61\u662fFigure\u548cAxes\uff0cFigure\u4ee3\u8868\u6574\u4e2a\u56fe\u5f62\uff0cAxes\u4ee3\u8868\u56fe\u5f62\u4e2d\u7684\u4e00\u4e2a\u5b50\u533a\u57df\u3002\u901a\u8fc7\u7ec4\u5408\u591a\u4e2aAxes\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2aFigure\u4e2d\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u548c\u5bfc\u5165Matplotlib<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\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\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165Matplotlib\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Matplotlib\u7684\u57fa\u672c\u7ed8\u56fe<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7ed8\u56fe\u901a\u5e38\u9700\u8981\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efaFigure\u5bf9\u8c61\u3001\u521b\u5efaAxes\u5bf9\u8c61\u3001\u7ed8\u5236\u56fe\u5f62\u3001\u8bbe\u7f6e\u6807\u7b7e\u548c\u6807\u9898\u3001\u663e\u793a\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u7b80\u5355\u6298\u7ebf\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528SUBPLOT\u529f\u80fd\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u4f7f\u7528subplot\u529f\u80fd\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u7a97\u53e3\u4e2d\u5c55\u793a\u591a\u4e2a\u5b50\u56fe\u3002subplot\u51fd\u6570\u7684\u53c2\u6570\u901a\u5e38\u4e3a\u4e09\u4e2a\u6574\u6570\uff0c\u5206\u522b\u8868\u793a\u5b50\u56fe\u7684\u884c\u6570\u3001\u5217\u6570\u548c\u5f53\u524d\u5b50\u56fe\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u591a\u884c\u591a\u5217\u5b50\u56fe<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7subplot\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u7a97\u53e3\u4e2d\u521b\u5efa\u591a\u884c\u591a\u5217\u7684\u5b50\u56fe\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a2&#215;2\u7684\u5b50\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.subplot(2, 2, 1)<\/p>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.title(&#39;\u5b50\u56fe1&#39;)<\/p>\n<p>plt.subplot(2, 2, 2)<\/p>\n<p>plt.plot([1, 2, 3], [1, 4, 9])<\/p>\n<p>plt.title(&#39;\u5b50\u56fe2&#39;)<\/p>\n<p>plt.subplot(2, 2, 3)<\/p>\n<p>plt.plot([1, 2, 3], [2, 4, 6])<\/p>\n<p>plt.title(&#39;\u5b50\u56fe3&#39;)<\/p>\n<p>plt.subplot(2, 2, 4)<\/p>\n<p>plt.plot([1, 2, 3], [3, 6, 9])<\/p>\n<p>plt.title(&#39;\u5b50\u56fe4&#39;)<\/p>\n<p>plt.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0csubplot(2, 2, 1)\u8868\u793a\u521b\u5efa\u4e00\u4e2a2&#215;2\u7684\u5b50\u56fe\uff0c\u5e76\u5728\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u7ed8\u5236\u56fe\u5f62\u3002\u6700\u540e\u7684tight_layout()\u51fd\u6570\u7528\u4e8e\u81ea\u52a8\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\uff0c\u4ee5\u907f\u514d\u6807\u7b7e\u548c\u6807\u9898\u91cd\u53e0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5171\u4eabX\u8f74\u6216Y\u8f74<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u591a\u4e2a\u5b50\u56fe\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5171\u4eabX\u8f74\u6216Y\u8f74\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u6bd4\u8f83\u6570\u636e\u3002\u53ef\u4ee5\u901a\u8fc7subplot\u51fd\u6570\u7684sharex\u6216sharey\u53c2\u6570\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>fig, ax = plt.subplots(2, 1, sharex=True)<\/p>\n<p>ax[0].plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>ax[0].set_title(&#39;\u5b50\u56fe1&#39;)<\/p>\n<p>ax[1].plot([1, 2, 3], [1, 4, 9])<\/p>\n<p>ax[1].set_title(&#39;\u5b50\u56fe2&#39;)<\/p>\n<p>plt.xlabel(&#39;\u5171\u4eab\u7684X\u8f74&#39;)<\/p>\n<p>plt.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u901a\u8fc7sharex=True\u53c2\u6570\u5b9e\u73b0\u4e86\u4e24\u4e2a\u5b50\u56fe\u5171\u4eabX\u8f74\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u901a\u8fc7FIGURE\u5bf9\u8c61\u7ba1\u7406\u591a\u4e2a\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5728\u4e0d\u540c\u7684\u7a97\u53e3\u4e2d\u5c55\u793a\u591a\u4e2a\u56fe\u5f62\uff0c\u8fd9\u65f6\u53ef\u4ee5\u901a\u8fc7Figure\u5bf9\u8c61\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u591a\u4e2aFigure\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7plt.figure()\u51fd\u6570\u521b\u5efa\u591a\u4e2aFigure\u5bf9\u8c61\uff0c\u5e76\u5728\u6bcf\u4e2aFigure\u4e2d\u7ed8\u5236\u72ec\u7acb\u7684\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u4e2aFigure<\/strong><\/h2>\n<p>fig1 = plt.figure()<\/p>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.title(&#39;\u56fe\u5f621&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2aFigure<\/strong><\/h2>\n<p>fig2 = plt.figure()<\/p>\n<p>plt.plot([1, 2, 3], [1, 4, 9])<\/p>\n<p>plt.title(&#39;\u56fe\u5f622&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u521b\u5efa\u4e86\u4e24\u4e2a\u72ec\u7acb\u7684Figure\u5bf9\u8c61\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u4e2d\u7ed8\u5236\u4e86\u4e0d\u540c\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728\u4e0d\u540cFigure\u4e2d\u4f7f\u7528subplot<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6bcf\u4e2aFigure\u4e2d\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528subplot\u529f\u80fd\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u4e2aFigure\u5e76\u6dfb\u52a0\u5b50\u56fe<\/strong><\/h2>\n<p>fig1 = plt.figure()<\/p>\n<p>ax1_1 = fig1.add_subplot(121)<\/p>\n<p>ax1_1.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>ax1_1.set_title(&#39;\u56fe\u5f621-\u5b50\u56fe1&#39;)<\/p>\n<p>ax1_2 = fig1.add_subplot(122)<\/p>\n<p>ax1_2.plot([1, 2, 3], [1, 4, 9])<\/p>\n<p>ax1_2.set_title(&#39;\u56fe\u5f621-\u5b50\u56fe2&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2aFigure\u5e76\u6dfb\u52a0\u5b50\u56fe<\/strong><\/h2>\n<p>fig2 = plt.figure()<\/p>\n<p>ax2_1 = fig2.add_subplot(211)<\/p>\n<p>ax2_1.plot([1, 2, 3], [2, 4, 6])<\/p>\n<p>ax2_1.set_title(&#39;\u56fe\u5f622-\u5b50\u56fe1&#39;)<\/p>\n<p>ax2_2 = fig2.add_subplot(212)<\/p>\n<p>ax2_2.plot([1, 2, 3], [3, 6, 9])<\/p>\n<p>ax2_2.set_title(&#39;\u56fe\u5f622-\u5b50\u56fe2&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684Figure\u4e2d\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\uff0c\u5e76\u72ec\u7acb\u7ba1\u7406\u6bcf\u4e2a\u5b50\u56fe\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528GRIDSPEC\u5e03\u5c40\u7ba1\u7406\u591a\u4e2a\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u5728\u590d\u6742\u5e03\u5c40\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u52a0\u7075\u6d3b\u7684\u5e03\u5c40\u7ba1\u7406\u65b9\u5f0f\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684GridSpec\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>GridSpec\u7684\u57fa\u672c\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>GridSpec\u5141\u8bb8\u521b\u5efa\u4efb\u610f\u884c\u5217\u7684\u7f51\u683c\u5e03\u5c40\uff0c\u5e76\u5728\u7f51\u683c\u4e2d\u81ea\u7531\u5b89\u6392\u5b50\u56fe\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.gridspec as gridspec<\/p>\n<p>fig = plt.figure()<\/p>\n<p>gs = gridspec.GridSpec(3, 3)<\/p>\n<p>ax1 = fig.add_subplot(gs[0, :])<\/p>\n<p>ax1.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>ax1.set_title(&#39;\u5b50\u56fe1&#39;)<\/p>\n<p>ax2 = fig.add_subplot(gs[1, :-1])<\/p>\n<p>ax2.plot([1, 2, 3], [1, 4, 9])<\/p>\n<p>ax2.set_title(&#39;\u5b50\u56fe2&#39;)<\/p>\n<p>ax3 = fig.add_subplot(gs[1:, -1])<\/p>\n<p>ax3.plot([1, 2, 3], [2, 4, 6])<\/p>\n<p>ax3.set_title(&#39;\u5b50\u56fe3&#39;)<\/p>\n<p>ax4 = fig.add_subplot(gs[2, 0])<\/p>\n<p>ax4.plot([1, 2, 3], [3, 6, 9])<\/p>\n<p>ax4.set_title(&#39;\u5b50\u56fe4&#39;)<\/p>\n<p>ax5 = fig.add_subplot(gs[2, 1])<\/p>\n<p>ax5.plot([1, 2, 3], [4, 8, 12])<\/p>\n<p>ax5.set_title(&#39;\u5b50\u56fe5&#39;)<\/p>\n<p>plt.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0cGridSpec(3, 3)\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u7f51\u683c\u5e03\u5c40\uff0c\u6bcf\u4e2a\u5b50\u56fe\u53ef\u4ee5\u5360\u636e\u4e0d\u540c\u7684\u7f51\u683c\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528GridSpec\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7update\u65b9\u6cd5\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.gridspec as gridspec<\/p>\n<p>fig = plt.figure()<\/p>\n<p>gs = gridspec.GridSpec(2, 2)<\/p>\n<p>gs.update(wspace=0.5, hspace=0.5)  # \u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u6c34\u5e73\u548c\u5782\u76f4\u95f4\u8ddd<\/p>\n<p>ax1 = fig.add_subplot(gs[0, 0])<\/p>\n<p>ax1.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>ax1.set_title(&#39;\u5b50\u56fe1&#39;)<\/p>\n<p>ax2 = fig.add_subplot(gs[0, 1])<\/p>\n<p>ax2.plot([1, 2, 3], [1, 4, 9])<\/p>\n<p>ax2.set_title(&#39;\u5b50\u56fe2&#39;)<\/p>\n<p>ax3 = fig.add_subplot(gs[1, 0])<\/p>\n<p>ax3.plot([1, 2, 3], [2, 4, 6])<\/p>\n<p>ax3.set_title(&#39;\u5b50\u56fe3&#39;)<\/p>\n<p>ax4 = fig.add_subplot(gs[1, 1])<\/p>\n<p>ax4.plot([1, 2, 3], [3, 6, 9])<\/p>\n<p>ax4.set_title(&#39;\u5b50\u56fe4&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7update\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u5b50\u56fe\u4e4b\u95f4\u7684\u6c34\u5e73\u548c\u5782\u76f4\u95f4\u8ddd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u5b9e\u8df5\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u975e\u5e38\u91cd\u8981\u3002<strong>Matplotlib\u5e93<\/strong>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u7684\u7ed8\u56fe\u9700\u6c42\u3002<strong>subplot\u529f\u80fd<\/strong>\u9002\u7528\u4e8e\u5728\u540c\u4e00\u4e2a\u7a97\u53e3\u4e2d\u5c55\u793a\u591a\u4e2a\u5b50\u56fe\uff0c\u800c<strong>figure\u5bf9\u8c61<\/strong>\u9002\u7528\u4e8e\u5728\u4e0d\u540c\u7684\u7a97\u53e3\u4e2d\u7ba1\u7406\u591a\u4e2a\u56fe\u5f62\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u5e03\u5c40\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>GridSpec<\/strong>\u529f\u80fd\u8fdb\u884c\u66f4\u52a0\u7075\u6d3b\u7684\u5e03\u5c40\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u8df5\u4e2d\uff0c\u5efa\u8bae\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u5145\u5206\u5229\u7528Matplotlib\u63d0\u4f9b\u7684\u529f\u80fd\u8fdb\u884c\u56fe\u5f62\u7684\u7f8e\u5316\u548c\u8c03\u6574\u3002\u901a\u8fc7\u4e0d\u65ad\u5b9e\u8df5\u548c\u603b\u7ed3\u7ecf\u9a8c\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1Python\u7ed8\u56fe\u7684\u6280\u5de7\uff0c\u63d0\u9ad8\u6570\u636e\u53ef\u89c6\u5316\u7684\u6548\u679c\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u540c\u65f6\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u7684<code>subplot<\/code>\u529f\u80fd\u3002\u901a\u8fc7\u6307\u5b9a\u884c\u548c\u5217\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u5728\u540c\u4e00\u7a97\u53e3\u4e2d\u521b\u5efa\u591a\u4e2a\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>plt.subplot(2, 2, 1)<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a2&#215;2\u7684\u56fe\u5f62\u7f51\u683c\uff0c\u5e76\u5728\u5176\u4e2d\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u7ed8\u5236\u56fe\u5f62\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u6bd4\u8f83\u4e0d\u540c\u6570\u636e\u96c6\u6216\u4e0d\u540c\u56fe\u5f62\u7684\u8868\u73b0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\u65f6\uff0c\u5982\u4f55\u8c03\u6574\u56fe\u5f62\u7684\u5e03\u5c40\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\u65f6\uff0c\u4f7f\u7528<code>plt.tight_layout()<\/code>\u53ef\u4ee5\u81ea\u52a8\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\uff0c\u4ee5\u9632\u6b62\u6807\u7b7e\u548c\u6807\u9898\u91cd\u53e0\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u624b\u52a8\u8bbe\u7f6e\u6bcf\u4e2a\u5b50\u56fe\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\uff0c\u901a\u8fc7<code>fig.subplots_adjust()<\/code>\u6765\u7ec6\u81f4\u8c03\u8282\uff0c\u4ee5\u786e\u4fdd\u56fe\u5f62\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\u65f6\uff0c\u5982\u4f55\u4fdd\u5b58\u8fd9\u4e9b\u56fe\u5f62\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>savefig<\/code>\u65b9\u6cd5\u6765\u4fdd\u5b58\u7ed8\u5236\u7684\u591a\u4e2a\u56fe\u5f62\u3002\u53ef\u4ee5\u5728\u7ed8\u5236\u5b8c\u6240\u6709\u5b50\u56fe\u540e\uff0c\u8c03\u7528<code>plt.savefig(&#39;filename.png&#39;)<\/code>\u6765\u4fdd\u5b58\u6574\u4e2a\u56fe\u5f62\u7a97\u53e3\u3002\u786e\u4fdd\u5728\u8c03\u7528<code>savefig<\/code>\u4e4b\u524d\uff0c\u6240\u6709\u7684\u56fe\u5f62\u90fd\u5df2\u7ecf\u8bbe\u7f6e\u597d\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4fdd\u5b58\u4e0d\u5b8c\u6574\u7684\u56fe\u5f62\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528Matplotlib\u5e93\u3001\u5229\u7528subplot\u529f\u80fd\u3001\u901a\u8fc7figure\u5bf9 [&hellip;]","protected":false},"author":3,"featured_media":995720,"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\/995708"}],"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=995708"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995708\/revisions"}],"predecessor-version":[{"id":995721,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/995708\/revisions\/995721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/995720"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=995708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=995708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=995708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}