{"id":1112037,"date":"2025-01-08T17:34:51","date_gmt":"2025-01-08T09:34:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112037.html"},"modified":"2025-01-08T17:34:53","modified_gmt":"2025-01-08T09:34:53","slug":"python%e5%a6%82%e4%bd%95%e4%b8%80%e4%b8%aa%e5%b9%b3%e9%9d%a2%e6%98%be%e7%a4%ba%e5%a4%9a%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112037.html","title":{"rendered":"python\u5982\u4f55\u4e00\u4e2a\u5e73\u9762\u663e\u793a\u591a\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074228\/bc331c8e-38e2-4fa3-9b43-6e083b238722.webp\" alt=\"python\u5982\u4f55\u4e00\u4e2a\u5e73\u9762\u663e\u793a\u591a\u56fe\" \/><\/p>\n<p><p> <strong>Python\u5728\u4e00\u4e2a\u5e73\u9762\u663e\u793a\u591a\u56fe\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528Matplotlib\u5e93\u7684subplot\u529f\u80fd\u3001\u4f7f\u7528Gridspec\u6a21\u5757\u3001\u4f7f\u7528seaborn\u5e93\u7b49\u3002<\/strong> \u4ee5\u4e0b\u5c06\u8be6\u7ec6\u5c55\u5f00Matplotlib\u5e93\u7684subplot\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002subplot\u529f\u80fd\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5e73\u9762\u4e0a\u5c55\u793a\u591a\u4e2a\u56fe\u8868\u3002\u901a\u8fc7\u8c03\u6574subplot\u7684\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u56fe\u8868\u7684\u5e03\u5c40\u548c\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Matplotlib\u5e93\u7684subplot\u529f\u80fd<\/h3>\n<\/p>\n<p><p>Matplotlib\u7684subplot\u529f\u80fd\u5141\u8bb8\u6211\u4eec\u5728\u4e00\u4e2a\u5e73\u9762\u4e0a\u6392\u5217\u591a\u4e2a\u56fe\u8868\u3002\u901a\u8fc7\u6307\u5b9a\u884c\u6570\u548c\u5217\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7f51\u683c\uff0c\u7136\u540e\u5728\u6bcf\u4e2a\u5b50\u56fe\u4e2d\u7ed8\u5236\u4e0d\u540c\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528subplot\u51fd\u6570\u5728\u4e00\u4e2a\u5e73\u9762\u4e0a\u663e\u793a\u591a\u4e2a\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b2\u884c2\u5217\u7684\u5b50\u56fe\u7f51\u683c<\/strong><\/h2>\n<p>fig, axs = plt.subplots(2, 2)<\/p>\n<h2><strong>\u5728\u7b2c\u4e00\u4e2a\u5b50\u56fe\u4e2d\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>axs[0, 0].plot([1, 2, 3], [1, 4, 9])<\/p>\n<p>axs[0, 0].set_title(&#39;Line Plot&#39;)<\/p>\n<h2><strong>\u5728\u7b2c\u4e8c\u4e2a\u5b50\u56fe\u4e2d\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>axs[0, 1].scatter([1, 2, 3], [1, 4, 9])<\/p>\n<p>axs[0, 1].set_title(&#39;Scatter Plot&#39;)<\/p>\n<h2><strong>\u5728\u7b2c\u4e09\u4e2a\u5b50\u56fe\u4e2d\u7ed8\u5236\u6761\u5f62\u56fe<\/strong><\/h2>\n<p>axs[1, 0].bar([1, 2, 3], [1, 4, 9])<\/p>\n<p>axs[1, 0].set_title(&#39;Bar Plot&#39;)<\/p>\n<h2><strong>\u5728\u7b2c\u56db\u4e2a\u5b50\u56fe\u4e2d\u7ed8\u5236\u997c\u56fe<\/strong><\/h2>\n<p>axs[1, 1].pie([10, 20, 30])<\/p>\n<p>axs[1, 1].set_title(&#39;Pie Chart&#39;)<\/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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a2\u884c2\u5217\u7684\u5b50\u56fe\u7f51\u683c\uff0c\u5e76\u5728\u6bcf\u4e2a\u5b50\u56fe\u4e2d\u7ed8\u5236\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4f7f\u7528<code>plt.tight_layout()<\/code>\u6765\u81ea\u52a8\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\uff0c\u786e\u4fdd\u56fe\u8868\u4e0d\u4f1a\u91cd\u53e0\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8c03\u6574\u5b50\u56fe\u7684\u5927\u5c0f\u548c\u6bd4\u4f8b<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u8c03\u6574\u5b50\u56fe\u7684\u5927\u5c0f\u548c\u6bd4\u4f8b\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>figsize<\/code>\u53c2\u6570\u6765\u63a7\u5236\u6574\u4e2a\u56fe\u8868\u7684\u5927\u5c0f\uff0c\u5e76\u4f7f\u7528<code>gridspec_kw<\/code>\u53c2\u6570\u6765\u8c03\u6574\u5b50\u56fe\u7684\u6bd4\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b2\u884c2\u5217\u7684\u5b50\u56fe\u7f51\u683c\uff0c\u5e76\u8bbe\u7f6e\u56fe\u8868\u5927\u5c0f<\/strong><\/h2>\n<p>fig, axs = plt.subplots(2, 2, figsize=(10, 10), gridspec_kw={&#39;width_ratios&#39;: [2, 1], &#39;height_ratios&#39;: [1, 2]})<\/p>\n<h2><strong>\u5728\u5b50\u56fe\u4e2d\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>axs[0, 0].plot([1, 2, 3], [1, 4, 9])<\/p>\n<p>axs[0, 0].set_title(&#39;Line Plot&#39;)<\/p>\n<p>axs[0, 1].scatter([1, 2, 3], [1, 4, 9])<\/p>\n<p>axs[0, 1].set_title(&#39;Scatter Plot&#39;)<\/p>\n<p>axs[1, 0].bar([1, 2, 3], [1, 4, 9])<\/p>\n<p>axs[1, 0].set_title(&#39;Bar Plot&#39;)<\/p>\n<p>axs[1, 1].pie([10, 20, 30])<\/p>\n<p>axs[1, 1].set_title(&#39;Pie Chart&#39;)<\/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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>figsize<\/code>\u53c2\u6570\u5c06\u6574\u4e2a\u56fe\u8868\u7684\u5927\u5c0f\u8bbe\u7f6e\u4e3a10&#215;10\u82f1\u5bf8\uff0c\u5e76\u4f7f\u7528<code>gridspec_kw<\/code>\u53c2\u6570\u8c03\u6574\u5b50\u56fe\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u6bd4\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Gridspec\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Gridspec\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u6765\u63a7\u5236\u5b50\u56fe\u7684\u5e03\u5c40\u3002\u901a\u8fc7\u6307\u5b9a\u5b50\u56fe\u7684\u884c\u5217\u8de8\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u66f4\u52a0\u590d\u6742\u7684\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Gridspec\u6a21\u5757\u521b\u5efa\u4e0d\u89c4\u5219\u7684\u5b50\u56fe\u5e03\u5c40\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<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b3\u884c3\u5217\u7684\u5b50\u56fe\u7f51\u683c<\/strong><\/h2>\n<p>fig = plt.figure(figsize=(10, 10))<\/p>\n<p>gs = gridspec.GridSpec(3, 3)<\/p>\n<h2><strong>\u5728\u6307\u5b9a\u4f4d\u7f6e\u548c\u8de8\u5ea6\u521b\u5efa\u5b50\u56fe<\/strong><\/h2>\n<p>ax1 = fig.add_subplot(gs[0, 0:2])  # \u5360\u636e\u7b2c1\u884c\u7684\u524d\u4e24\u5217<\/p>\n<p>ax2 = fig.add_subplot(gs[0, 2])    # \u5360\u636e\u7b2c1\u884c\u7684\u7b2c3\u5217<\/p>\n<p>ax3 = fig.add_subplot(gs[1:, 0])   # \u5360\u636e\u7b2c2\u884c\u548c\u7b2c3\u884c\u7684\u7b2c1\u5217<\/p>\n<p>ax4 = fig.add_subplot(gs[1, 1:])   # \u5360\u636e\u7b2c2\u884c\u7684\u7b2c2\u5217\u548c\u7b2c3\u5217<\/p>\n<p>ax5 = fig.add_subplot(gs[2, 1:])   # \u5360\u636e\u7b2c3\u884c\u7684\u7b2c2\u5217\u548c\u7b2c3\u5217<\/p>\n<h2><strong>\u5728\u5b50\u56fe\u4e2d\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>ax1.plot([1, 2, 3], [1, 4, 9])<\/p>\n<p>ax1.set_title(&#39;Line Plot&#39;)<\/p>\n<p>ax2.scatter([1, 2, 3], [1, 4, 9])<\/p>\n<p>ax2.set_title(&#39;Scatter Plot&#39;)<\/p>\n<p>ax3.bar([1, 2, 3], [1, 4, 9])<\/p>\n<p>ax3.set_title(&#39;Bar Plot&#39;)<\/p>\n<p>ax4.pie([10, 20, 30])<\/p>\n<p>ax4.set_title(&#39;Pie Chart&#39;)<\/p>\n<p>ax5.hist([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])<\/p>\n<p>ax5.set_title(&#39;Histogram&#39;)<\/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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Gridspec\u6a21\u5757\u521b\u5efa\u4e86\u4e00\u4e2a3\u884c3\u5217\u7684\u5b50\u56fe\u7f51\u683c\uff0c\u5e76\u901a\u8fc7\u6307\u5b9a\u5b50\u56fe\u7684\u884c\u5217\u8de8\u5ea6\u6765\u521b\u5efa\u4e0d\u89c4\u5219\u7684\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u66f4\u591a\u7684\u5e03\u5c40\u63a7\u5236<\/h4>\n<\/p>\n<p><p>Gridspec\u6a21\u5757\u8fd8\u5141\u8bb8\u6211\u4eec\u7cbe\u786e\u63a7\u5236\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\u548c\u8fb9\u8ddd\uff0c\u53ef\u4ee5\u4f7f\u7528<code>GridSpec<\/code>\u7c7b\u7684<code>update<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u8fd9\u4e9b\u53c2\u6570\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<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b3\u884c3\u5217\u7684\u5b50\u56fe\u7f51\u683c<\/strong><\/h2>\n<p>fig = plt.figure(figsize=(10, 10))<\/p>\n<p>gs = gridspec.GridSpec(3, 3)<\/p>\n<h2><strong>\u66f4\u65b0\u5b50\u56fe\u7684\u95f4\u8ddd\u548c\u8fb9\u8ddd<\/strong><\/h2>\n<p>gs.update(wspace=0.5, hspace=0.5, left=0.1, right=0.9, top=0.9, bottom=0.1)<\/p>\n<h2><strong>\u5728\u6307\u5b9a\u4f4d\u7f6e\u548c\u8de8\u5ea6\u521b\u5efa\u5b50\u56fe<\/strong><\/h2>\n<p>ax1 = fig.add_subplot(gs[0, 0:2])<\/p>\n<p>ax2 = fig.add_subplot(gs[0, 2])<\/p>\n<p>ax3 = fig.add_subplot(gs[1:, 0])<\/p>\n<p>ax4 = fig.add_subplot(gs[1, 1:])<\/p>\n<p>ax5 = fig.add_subplot(gs[2, 1:])<\/p>\n<h2><strong>\u5728\u5b50\u56fe\u4e2d\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>ax1.plot([1, 2, 3], [1, 4, 9])<\/p>\n<p>ax1.set_title(&#39;Line Plot&#39;)<\/p>\n<p>ax2.scatter([1, 2, 3], [1, 4, 9])<\/p>\n<p>ax2.set_title(&#39;Scatter Plot&#39;)<\/p>\n<p>ax3.bar([1, 2, 3], [1, 4, 9])<\/p>\n<p>ax3.set_title(&#39;Bar Plot&#39;)<\/p>\n<p>ax4.pie([10, 20, 30])<\/p>\n<p>ax4.set_title(&#39;Pie Chart&#39;)<\/p>\n<p>ax5.hist([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])<\/p>\n<p>ax5.set_title(&#39;Histogram&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>gs.update<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u5b50\u56fe\u4e4b\u95f4\u7684\u6c34\u5e73\u95f4\u8ddd\uff08wspace\uff09\u548c\u5782\u76f4\u95f4\u8ddd\uff08hspace\uff09\uff0c\u4ee5\u53ca\u56fe\u8868\u7684\u5de6\u53f3\uff08left\u3001right\uff09\u548c\u4e0a\u4e0b\uff08top\u3001bottom\uff09\u8fb9\u8ddd\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528seaborn\u5e93<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u52a0\u7b80\u6d01\u7684API\u548c\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002\u867d\u7136Seaborn\u6ca1\u6709\u76f4\u63a5\u7684subplot\u529f\u80fd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528Matplotlib\u7684subplot\u529f\u80fd\u6765\u5728\u4e00\u4e2a\u5e73\u9762\u4e0a\u663e\u793a\u591a\u4e2a\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Seaborn\u5e93\u548cMatplotlib\u7684subplot\u529f\u80fd\u5728\u4e00\u4e2a\u5e73\u9762\u4e0a\u663e\u793a\u591a\u4e2a\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u52a0\u8f7d\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>tips = sns.load_dataset(&#39;tips&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b2\u884c2\u5217\u7684\u5b50\u56fe\u7f51\u683c<\/strong><\/h2>\n<p>fig, axs = plt.subplots(2, 2, figsize=(10, 10))<\/p>\n<h2><strong>\u5728\u5b50\u56fe\u4e2d\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>sns.lineplot(x=&#39;total_bill&#39;, y=&#39;tip&#39;, data=tips, ax=ax1)<\/p>\n<p>axs[0, 0].set_title(&#39;Line Plot&#39;)<\/p>\n<p>sns.scatterplot(x=&#39;total_bill&#39;, y=&#39;tip&#39;, data=tips, ax=ax2)<\/p>\n<p>axs[0, 1].set_title(&#39;Scatter Plot&#39;)<\/p>\n<p>sns.barplot(x=&#39;day&#39;, y=&#39;total_bill&#39;, data=tips, ax=ax3)<\/p>\n<p>axs[1, 0].set_title(&#39;Bar Plot&#39;)<\/p>\n<p>sns.histplot(tips[&#39;total_bill&#39;], kde=True, ax=ax4)<\/p>\n<p>axs[1, 1].set_title(&#39;Histogram&#39;)<\/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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Seaborn\u5e93\u52a0\u8f7d\u4e86\u793a\u4f8b\u6570\u636e\u96c6\uff0c\u5e76\u5728Matplotlib\u7684\u5b50\u56fe\u4e2d\u7ed8\u5236\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ec4\u5408\u4f7f\u7528Seaborn\u548cGridspec<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528Seaborn\u5e93\u548cGridspec\u6a21\u5757\u6765\u521b\u5efa\u66f4\u52a0\u590d\u6742\u7684\u5b50\u56fe\u5e03\u5c40\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.gridspec as gridspec<\/p>\n<h2><strong>\u52a0\u8f7d\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>tips = sns.load_dataset(&#39;tips&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b3\u884c3\u5217\u7684\u5b50\u56fe\u7f51\u683c<\/strong><\/h2>\n<p>fig = plt.figure(figsize=(10, 10))<\/p>\n<p>gs = gridspec.GridSpec(3, 3)<\/p>\n<h2><strong>\u5728\u6307\u5b9a\u4f4d\u7f6e\u548c\u8de8\u5ea6\u521b\u5efa\u5b50\u56fe<\/strong><\/h2>\n<p>ax1 = fig.add_subplot(gs[0, 0:2])<\/p>\n<p>ax2 = fig.add_subplot(gs[0, 2])<\/p>\n<p>ax3 = fig.add_subplot(gs[1:, 0])<\/p>\n<p>ax4 = fig.add_subplot(gs[1, 1:])<\/p>\n<p>ax5 = fig.add_subplot(gs[2, 1:])<\/p>\n<h2><strong>\u5728\u5b50\u56fe\u4e2d\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>sns.lineplot(x=&#39;total_bill&#39;, y=&#39;tip&#39;, data=tips, ax=ax1)<\/p>\n<p>ax1.set_title(&#39;Line Plot&#39;)<\/p>\n<p>sns.scatterplot(x=&#39;total_bill&#39;, y=&#39;tip&#39;, data=tips, ax=ax2)<\/p>\n<p>ax2.set_title(&#39;Scatter Plot&#39;)<\/p>\n<p>sns.barplot(x=&#39;day&#39;, y=&#39;total_bill&#39;, data=tips, ax=ax3)<\/p>\n<p>ax3.set_title(&#39;Bar Plot&#39;)<\/p>\n<p>sns.histplot(tips[&#39;total_bill&#39;], kde=True, ax=ax4)<\/p>\n<p>ax4.set_title(&#39;Histogram&#39;)<\/p>\n<p>sns.boxplot(x=&#39;day&#39;, y=&#39;total_bill&#39;, data=tips, ax=ax5)<\/p>\n<p>ax5.set_title(&#39;Box Plot&#39;)<\/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>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7ed3\u5408\u4f7f\u7528\u4e86Seaborn\u5e93\u548cGridspec\u6a21\u5757\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a3\u884c3\u5217\u7684\u4e0d\u89c4\u5219\u5b50\u56fe\u5e03\u5c40\uff0c\u5e76\u5728\u6bcf\u4e2a\u5b50\u56fe\u4e2d\u7ed8\u5236\u4e86\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u7684subplot\u529f\u80fd\u3001Gridspec\u6a21\u5757\u4ee5\u53caSeaborn\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5e73\u9762\u4e0a\u663e\u793a\u591a\u4e2a\u56fe\u8868\uff0c\u5e76\u7075\u6d3b\u5730\u63a7\u5236\u5b50\u56fe\u7684\u5e03\u5c40\u548c\u663e\u793a\u3002Matplotlib\u7684subplot\u529f\u80fd\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b50\u56fe\u5e03\u5c40\uff0c\u800cGridspec\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u6765\u521b\u5efa\u590d\u6742\u7684\u5e03\u5c40\u3002Seaborn\u5e93\u867d\u7136\u6ca1\u6709\u76f4\u63a5\u7684subplot\u529f\u80fd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528Matplotlib\u7684subplot\u529f\u80fd\u6765\u5728\u4e00\u4e2a\u5e73\u9762\u4e0a\u663e\u793a\u591a\u4e2a\u7f8e\u89c2\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u663e\u793a\u591a\u4e2a\u56fe\u50cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>subplot<\/code>\u6216<code>subplots<\/code>\u51fd\u6570\u6765\u5728\u4e00\u4e2a\u5e73\u9762\u4e0a\u663e\u793a\u591a\u4e2a\u56fe\u50cf\u3002<code>subplot<\/code>\u5141\u8bb8\u60a8\u5728\u4e00\u4e2a\u56fe\u5f62\u7a97\u53e3\u4e2d\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\uff0c\u800c<code>subplots<\/code>\u53ef\u4ee5\u4e00\u6b21\u6027\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\u7684\u7f51\u683c\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u884c\u548c\u5217\u7684\u53c2\u6570\u6765\u6539\u53d8\u56fe\u50cf\u7684\u5e03\u5c40\uff0c\u5e76\u4f7f\u7528<code>imshow<\/code>\u51fd\u6570\u6765\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<p><strong>\u5728\u663e\u793a\u591a\u4e2a\u56fe\u50cf\u65f6\uff0c\u5982\u4f55\u63a7\u5236\u6bcf\u4e2a\u56fe\u50cf\u7684\u5927\u5c0f\u548c\u95f4\u8ddd\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>subplots<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>figsize<\/code>\u53c2\u6570\u6765\u8c03\u6574\u6574\u4e2a\u56fe\u5f62\u7684\u5927\u5c0f\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.subplots_adjust()<\/code>\u51fd\u6570\u6765\u63a7\u5236\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\uff0c\u4f8b\u5982\u901a\u8fc7\u8bbe\u7f6e<code>hspace<\/code>\u548c<code>wspace<\/code>\u53c2\u6570\u6765\u8c03\u6574\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u7684\u95f4\u8ddd\uff0c\u4ece\u800c\u4f7f\u663e\u793a\u6548\u679c\u66f4\u4f73\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u591a\u4e2a\u56fe\u50cf\u4e0a\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>set_title()<\/code>\u65b9\u6cd5\u4e3a\u6bcf\u4e2a\u5b50\u56fe\u6dfb\u52a0\u6807\u9898\uff0c\u540c\u65f6\u4f7f\u7528<code>set_xlabel()<\/code>\u548c<code>set_ylabel()<\/code>\u65b9\u6cd5\u4e3a\u6bcf\u4e2a\u56fe\u50cf\u6dfb\u52a0X\u8f74\u548cY\u8f74\u6807\u7b7e\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u89e3\u91ca\u6bcf\u4e2a\u56fe\u50cf\u7684\u5185\u5bb9\u548c\u610f\u4e49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5728\u4e00\u4e2a\u5e73\u9762\u663e\u793a\u591a\u56fe\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528Matplotlib\u5e93\u7684subplot\u529f\u80fd\u3001\u4f7f\u7528Gridspe [&hellip;]","protected":false},"author":3,"featured_media":1112043,"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\/1112037"}],"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=1112037"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112037\/revisions"}],"predecessor-version":[{"id":1112045,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112037\/revisions\/1112045"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112043"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}