{"id":1175114,"date":"2025-01-15T17:27:26","date_gmt":"2025-01-15T09:27:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1175114.html"},"modified":"2025-01-15T17:27:30","modified_gmt":"2025-01-15T09:27:30","slug":"python%e5%a6%82%e4%bd%95%e5%90%8c%e6%97%b6%e7%94%bb%e5%a4%9a%e4%b8%aa%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1175114.html","title":{"rendered":"python\u5982\u4f55\u540c\u65f6\u753b\u591a\u4e2a\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110640\/34747a91-e716-4838-8394-ca8ac927c652.webp\" alt=\"python\u5982\u4f55\u540c\u65f6\u753b\u591a\u4e2a\u56fe\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u6765\u540c\u65f6\u7ed8\u5236\u591a\u4e2a\u56fe\u3002<strong>\u4f7f\u7528<code>subplot<\/code>\u3001<code>subplots<\/code>\u3001<code>GridSpec<\/code>\u3001<code>plt.figure<\/code>\u7b49\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u540c\u65f6\u7ed8\u5236\u591a\u4e2a\u56fe<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u6765\u521b\u5efa\u591a\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001SUBPLOT<\/h3>\n<\/p>\n<p><p><strong><code>subplot<\/code>\u65b9\u6cd5<\/strong>\u662fMatplotlib\u5e93\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4e00\u4e2a\u5355\u4e00\u7684\u56fe\u5f62\u7a97\u53e3\u4e2d\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\u3002\u4f7f\u7528<code>subplot<\/code>\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6307\u5b9a\u5b50\u56fe\u7684\u884c\u6570\u3001\u5217\u6570\u4ee5\u53ca\u5f53\u524d\u56fe\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2\u884c2\u5217\u7684\u7f51\u683c\uff0c\u7ed8\u52364\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 8))<\/p>\n<p>plt.subplot(2, 2, 1)<\/p>\n<p>plt.plot([0, 1], [0, 1])<\/p>\n<p>plt.title(&#39;Plot 1&#39;)<\/p>\n<p>plt.subplot(2, 2, 2)<\/p>\n<p>plt.plot([0, 1], [1, 0])<\/p>\n<p>plt.title(&#39;Plot 2&#39;)<\/p>\n<p>plt.subplot(2, 2, 3)<\/p>\n<p>plt.plot([0, 1], [0.5, 0.5])<\/p>\n<p>plt.title(&#39;Plot 3&#39;)<\/p>\n<p>plt.subplot(2, 2, 4)<\/p>\n<p>plt.plot([0, 1], [0, 0.5])<\/p>\n<p>plt.title(&#39;Plot 4&#39;)<\/p>\n<p>plt.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>plt.subplot(2, 2, 1)<\/code>\u6765\u6307\u5b9a\u4e00\u4e2a2\u884c2\u5217\u7684\u7f51\u683c\uff0c\u5e76\u5c06\u7b2c\u4e00\u4e2a\u5b50\u56fe\u653e\u5728\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u3002\u4f9d\u6b21\u7c7b\u63a8\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u7f51\u683c\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u5b50\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001SUBPLOTS<\/h3>\n<\/p>\n<p><p><strong><code>subplots<\/code>\u65b9\u6cd5<\/strong>\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u5355\u3001\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\uff0c\u8fd8\u80fd\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5b50\u56fe\u7684\u6570\u7ec4\uff0c\u4f7f\u5f97\u5bf9\u6bcf\u4e2a\u5b50\u56fe\u7684\u5904\u7406\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2\u884c2\u5217\u7684\u7f51\u683c\uff0c\u7ed8\u52364\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p>fig, axs = plt.subplots(2, 2, figsize=(10, 8))<\/p>\n<p>axs[0, 0].plot([0, 1], [0, 1])<\/p>\n<p>axs[0, 0].set_title(&#39;Plot 1&#39;)<\/p>\n<p>axs[0, 1].plot([0, 1], [1, 0])<\/p>\n<p>axs[0, 1].set_title(&#39;Plot 2&#39;)<\/p>\n<p>axs[1, 0].plot([0, 1], [0.5, 0.5])<\/p>\n<p>axs[1, 0].set_title(&#39;Plot 3&#39;)<\/p>\n<p>axs[1, 1].plot([0, 1], [0, 0.5])<\/p>\n<p>axs[1, 1].set_title(&#39;Plot 4&#39;)<\/p>\n<p>fig.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>subplots<\/code>\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5b50\u56fe\u7684\u6570\u7ec4<code>axs<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15<code>axs[i, j]<\/code>\u6765\u8bbf\u95ee\u6bcf\u4e2a\u5b50\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001GRIDSPEC<\/h3>\n<\/p>\n<p><p><strong><code>GridSpec<\/code>\u65b9\u6cd5<\/strong>\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u7f51\u683c\u5e03\u5c40\u914d\u7f6e\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u4ee5\u66f4\u590d\u6742\u7684\u65b9\u5f0f\u6392\u5217\u5b50\u56fe\uff0c\u751a\u81f3\u53ef\u4ee5\u8de8\u8d8a\u591a\u884c\u6216\u591a\u5217\u3002<\/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\u4e2aGridSpec\u5bf9\u8c61<\/strong><\/h2>\n<p>fig = plt.figure(figsize=(10, 8))<\/p>\n<p>gs = gridspec.GridSpec(3, 3)<\/p>\n<p>ax1 = fig.add_subplot(gs[0, 0])<\/p>\n<p>ax1.plot([0, 1], [0, 1])<\/p>\n<p>ax1.set_title(&#39;Plot 1&#39;)<\/p>\n<p>ax2 = fig.add_subplot(gs[0, 1:])<\/p>\n<p>ax2.plot([0, 1], [1, 0])<\/p>\n<p>ax2.set_title(&#39;Plot 2&#39;)<\/p>\n<p>ax3 = fig.add_subplot(gs[1:, 0])<\/p>\n<p>ax3.plot([0, 1], [0.5, 0.5])<\/p>\n<p>ax3.set_title(&#39;Plot 3&#39;)<\/p>\n<p>ax4 = fig.add_subplot(gs[1:, 1:])<\/p>\n<p>ax4.plot([0, 1], [0, 0.5])<\/p>\n<p>ax4.set_title(&#39;Plot 4&#39;)<\/p>\n<p>plt.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>GridSpec<\/code>\u5bf9\u8c61<code>gs<\/code>\uff0c\u5e76\u901a\u8fc7<code>fig.add_subplot(gs[i, j])<\/code>\u65b9\u6cd5\u6765\u6307\u5b9a\u6bcf\u4e2a\u5b50\u56fe\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001PLT.FIGURE<\/h3>\n<\/p>\n<p><p><strong><code>plt.figure<\/code>\u65b9\u6cd5<\/strong>\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684\u56fe\u5f62\u7a97\u53e3\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u4e0d\u540c\u7684\u56fe\u5f62\u7a97\u53e3\u4e2d\u7ed8\u5236\u591a\u4e2a\u56fe\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>plt.figure<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u4e2a\u56fe\u5f62\u7a97\u53e3<\/strong><\/h2>\n<p>plt.figure(1, figsize=(6, 4))<\/p>\n<p>plt.plot([0, 1], [0, 1])<\/p>\n<p>plt.title(&#39;Figure 1&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2a\u56fe\u5f62\u7a97\u53e3<\/strong><\/h2>\n<p>plt.figure(2, figsize=(6, 4))<\/p>\n<p>plt.plot([0, 1], [1, 0])<\/p>\n<p>plt.title(&#39;Figure 2&#39;)<\/p>\n<h2><strong>\u663e\u793a\u6240\u6709\u56fe\u5f62\u7a97\u53e3<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>plt.figure(1)<\/code>\u548c<code>plt.figure(2)<\/code>\u5206\u522b\u521b\u5efa\u4e86\u4e24\u4e2a\u72ec\u7acb\u7684\u56fe\u5f62\u7a97\u53e3\uff0c\u5e76\u5728\u6bcf\u4e2a\u7a97\u53e3\u4e2d\u7ed8\u5236\u4e86\u4e00\u4e2a\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001PLOTTING WITH SEABORN<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\uff0cSeaborn\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\u3002Seaborn\u57fa\u4e8eMatplotlib\u6784\u5efa\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u548c\u66f4\u590d\u6742\u7684\u7ed8\u56fe\u529f\u80fd\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528Seaborn\u6765\u7ed8\u5236\u591a\u4e2a\u56fe\u3002<\/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\u4e2a2\u884c2\u5217\u7684\u7f51\u683c\uff0c\u7ed8\u52364\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p>fig, axs = plt.subplots(2, 2, figsize=(10, 8))<\/p>\n<p>sns.scatterplot(data=tips, x=&#39;total_bill&#39;, y=&#39;tip&#39;, ax=axs[0, 0])<\/p>\n<p>axs[0, 0].set_title(&#39;Scatter Plot&#39;)<\/p>\n<p>sns.histplot(data=tips, x=&#39;total_bill&#39;, kde=True, ax=axs[0, 1])<\/p>\n<p>axs[0, 1].set_title(&#39;Histogram&#39;)<\/p>\n<p>sns.boxplot(data=tips, x=&#39;day&#39;, y=&#39;total_bill&#39;, ax=axs[1, 0])<\/p>\n<p>axs[1, 0].set_title(&#39;Box Plot&#39;)<\/p>\n<p>sns.violinplot(data=tips, x=&#39;day&#39;, y=&#39;total_bill&#39;, ax=axs[1, 1])<\/p>\n<p>axs[1, 1].set_title(&#39;Violin Plot&#39;)<\/p>\n<p>fig.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Seaborn\u6765\u7ed8\u5236\u591a\u4e2a\u7c7b\u578b\u7684\u56fe\uff0c\u5e76\u5c06\u5b83\u4eec\u653e\u7f6e\u5728\u4e00\u4e2a2\u884c2\u5217\u7684\u7f51\u683c\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001USING OBJECT-ORIENTED INTERFACE<\/h3>\n<\/p>\n<p><p>Matplotlib\u8fd8\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u66f4\u7cbe\u7ec6\u5730\u63a7\u5236\u6bcf\u4e2a\u56fe\u5f62\u5143\u7d20\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u521b\u5efa\u548c\u7ba1\u7406\u591a\u4e2a\u5b50\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u7a97\u53e3<\/strong><\/h2>\n<p>fig = plt.figure(figsize=(10, 8))<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p>ax1 = fig.add_subplot(2, 2, 1)<\/p>\n<p>ax2 = fig.add_subplot(2, 2, 2)<\/p>\n<p>ax3 = fig.add_subplot(2, 2, 3)<\/p>\n<p>ax4 = fig.add_subplot(2, 2, 4)<\/p>\n<h2><strong>\u7ed8\u5236\u6570\u636e<\/strong><\/h2>\n<p>ax1.plot([0, 1], [0, 1])<\/p>\n<p>ax1.set_title(&#39;Plot 1&#39;)<\/p>\n<p>ax2.plot([0, 1], [1, 0])<\/p>\n<p>ax2.set_title(&#39;Plot 2&#39;)<\/p>\n<p>ax3.plot([0, 1], [0.5, 0.5])<\/p>\n<p>ax3.set_title(&#39;Plot 3&#39;)<\/p>\n<p>ax4.plot([0, 1], [0, 0.5])<\/p>\n<p>ax4.set_title(&#39;Plot 4&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u5e03\u5c40<\/strong><\/h2>\n<p>fig.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>fig.add_subplot<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\uff0c\u5e76\u901a\u8fc7\u9762\u5411\u5bf9\u8c61\u7684\u63a5\u53e3\u6765\u7ed8\u5236\u6570\u636e\u548c\u8bbe\u7f6e\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001MULTIPAGE PDF FILES<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u591a\u4e2a\u56fe\u4fdd\u5b58\u5230\u4e00\u4e2a\u591a\u9875\u7684PDF\u6587\u4ef6\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>PdfPages<\/code>\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from matplotlib.backends.backend_pdf import PdfPages<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aPdfPages\u5bf9\u8c61<\/strong><\/h2>\n<p>pdf = PdfPages(&#39;multipage.pdf&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u56fe\u5f62\u7a97\u53e3\u5e76\u4fdd\u5b58\u5230PDF\u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p>for i in range(1, 5):<\/p>\n<p>    fig = plt.figure(figsize=(6, 4))<\/p>\n<p>    plt.plot([0, 1], [0, 1])<\/p>\n<p>    plt.title(f&#39;Figure {i}&#39;)<\/p>\n<p>    pdf.savefig(fig)<\/p>\n<p>    plt.close(fig)<\/p>\n<h2><strong>\u5173\u95edPdfPages\u5bf9\u8c61<\/strong><\/h2>\n<p>pdf.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>PdfPages<\/code>\u5bf9\u8c61\uff0c\u5e76\u5728\u5faa\u73af\u4e2d\u521b\u5efa\u591a\u4e2a\u56fe\u5f62\u7a97\u53e3\uff0c\u5c06\u5b83\u4eec\u4fdd\u5b58\u5230\u4e00\u4e2a\u591a\u9875\u7684PDF\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001INTERACTIVE PLOTTING WITH PLOTLY<\/h3>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528Plotly\u6765\u7ed8\u5236\u591a\u4e2a\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.subplots as sp<\/p>\n<p>import plotly.graph_objs as go<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2\u884c2\u5217\u7684\u5b50\u56fe\u5e03\u5c40<\/strong><\/h2>\n<p>fig = sp.make_subplots(rows=2, cols=2)<\/p>\n<h2><strong>\u6dfb\u52a0\u591a\u4e2a\u56fe\u5230\u5b50\u56fe\u5e03\u5c40\u4e2d<\/strong><\/h2>\n<p>fig.add_trace(go.Scatter(x=[0, 1], y=[0, 1], mode=&#39;lines&#39;, name=&#39;Plot 1&#39;), row=1, col=1)<\/p>\n<p>fig.add_trace(go.Scatter(x=[0, 1], y=[1, 0], mode=&#39;lines&#39;, name=&#39;Plot 2&#39;), row=1, col=2)<\/p>\n<p>fig.add_trace(go.Scatter(x=[0, 1], y=[0.5, 0.5], mode=&#39;lines&#39;, name=&#39;Plot 3&#39;), row=2, col=1)<\/p>\n<p>fig.add_trace(go.Scatter(x=[0, 1], y=[0, 0.5], mode=&#39;lines&#39;, name=&#39;Plot 4&#39;), row=2, col=2)<\/p>\n<h2><strong>\u66f4\u65b0\u5e03\u5c40<\/strong><\/h2>\n<p>fig.update_layout(title=&#39;Multiple Plots with Plotly&#39;, showlegend=False)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Plotly\u7684<code>make_subplots<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a2\u884c2\u5217\u7684\u5b50\u56fe\u5e03\u5c40\uff0c\u5e76\u901a\u8fc7<code>add_trace<\/code>\u65b9\u6cd5\u5c06\u591a\u4e2a\u56fe\u6dfb\u52a0\u5230\u5b50\u56fe\u5e03\u5c40\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u540c\u65f6\u7ed8\u5236\u591a\u4e2a\u56fe\u3002<strong>\u4f7f\u7528<code>subplot<\/code>\u3001<code>subplots<\/code>\u3001<code>GridSpec<\/code>\u3001<code>plt.figure<\/code>\u7b49\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u540c\u65f6\u7ed8\u5236\u591a\u4e2a\u56fe<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b9e\u73b0\u6570\u636e\u53ef\u89c6\u5316\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u7ed3\u5408Seaborn\u548cPlotly\u7b49\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u521b\u5efa\u4e30\u5bcc\u591a\u5f69\u7684\u6570\u636e\u53ef\u89c6\u5316\u56fe\u5f62\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\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u8f7b\u677e\u5730\u540c\u65f6\u7ed8\u5236\u591a\u4e2a\u56fe\u8868\u3002\u901a\u8fc7\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\uff08subplots\uff09\uff0c\u60a8\u53ef\u4ee5\u5728\u540c\u4e00\u7a97\u53e3\u4e2d\u663e\u793a\u591a\u4e2a\u56fe\u5f62\u3002\u4f7f\u7528<code>plt.subplots()<\/code>\u51fd\u6570\u53ef\u4ee5\u6307\u5b9a\u884c\u6570\u548c\u5217\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<p><strong>\u4f7f\u7528\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u8c03\u6574\u591a\u4e2a\u56fe\u8868\u7684\u5927\u5c0f\u548c\u95f4\u8ddd\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>figsize<\/code>\u53c2\u6570\u5728<code>plt.subplots()<\/code>\u4e2d\u8bbe\u7f6e\u6574\u4e2a\u56fe\u5f62\u7684\u5927\u5c0f\u3002\u540c\u65f6\uff0c<code>plt.subplots_adjust()<\/code>\u51fd\u6570\u5141\u8bb8\u60a8\u8c03\u6574\u5b50\u56fe\u4e4b\u95f4\u7684\u95f4\u8ddd\uff0c\u4f8b\u5982\u4f7f\u7528<code>wspace<\/code>\u548c<code>hspace<\/code>\u6765\u63a7\u5236\u6c34\u5e73\u548c\u5782\u76f4\u95f4\u8ddd\uff0c\u4ee5\u786e\u4fdd\u56fe\u8868\u4e0d\u4f1a\u91cd\u53e0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4e0d\u540c\u7684\u56fe\u8868\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u6837\u5f0f\u548c\u989c\u8272\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u5b50\u56fe\u5355\u72ec\u8bbe\u7f6e\u6837\u5f0f\u548c\u989c\u8272\u3002\u901a\u8fc7\u5728\u7ed8\u5236\u6bcf\u4e2a\u56fe\u8868\u65f6\u4f7f\u7528\u4e0d\u540c\u7684\u989c\u8272\u53c2\u6570\u548c\u6837\u5f0f\u8bbe\u7f6e\uff08\u4f8b\u5982\u7ebf\u6761\u7c7b\u578b\u3001\u6807\u8bb0\u6837\u5f0f\u7b49\uff09\uff0c\u53ef\u4ee5\u5b9e\u73b0\u89c6\u89c9\u4e0a\u7684\u591a\u6837\u6027\u3002\u8fd9\u4f7f\u5f97\u6bcf\u4e2a\u56fe\u8868\u4e0d\u4ec5\u5177\u6709\u72ec\u7279\u7684\u5916\u89c2\uff0c\u540c\u65f6\u4e5f\u80fd\u66f4\u597d\u5730\u4f20\u8fbe\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u6765\u540c\u65f6\u7ed8\u5236\u591a\u4e2a\u56fe\u3002\u4f7f\u7528subplot\u3001subplots [&hellip;]","protected":false},"author":3,"featured_media":1175123,"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\/1175114"}],"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=1175114"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175114\/revisions"}],"predecessor-version":[{"id":1175127,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175114\/revisions\/1175127"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1175123"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1175114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1175114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1175114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}