matplotlib:如何在不应用于所有行的情况下调整图形中各行之间的间距

3
我正在尝试使用matplotlib创建一个包含12个图的图形。 这些图分布如下:有3列和4行。我希望前两行在y轴/垂直方向上“粘”在一起,即它们之间不应该有坐标轴标签和下面一行的图形标题。第3行和第4行也是如此。但是,我希望在两个“行组”之间留出一些空间。因此,基本上第1行和第2行粘在一起,第3行和第4行粘在一起,但第2行和第3行之间有一些空间。
我只能找到影响所有子图高度空间的参数,但没有让我单独修改高度空间的参数。在这个MWE中,我使用“figure.subplot.hspace”rcParam来修改hspace。
#!/usr/bin/env python3

import matplotlib as mpl
from matplotlib import pyplot as plt
import numpy as np

params = {
    "xtick.labelsize": 12,
    "ytick.labelsize": 12,
    "xtick.direction": "in",
    "ytick.direction": "in",
    "xtick.top": True,
    "ytick.right": True,
    "figure.subplot.left": 0.045,
    "figure.subplot.right": 0.99,
    "figure.subplot.bottom": 0.05,
    "figure.subplot.top": 0.93,
    "figure.subplot.wspace": 0.15,
    "figure.subplot.hspace": 0.0,
}
mpl.rcParams.update(params)


if __name__ == "__main__":

    # invent some data
    x = np.linspace(0, 2*np.pi, 100)
    y_main = np.sin(x)
    y_diff = 1. - y_main


    fig = plt.figure(figsize=(18, 22))
    ax1 = fig.add_subplot(4,3,1,)
    ax2 = fig.add_subplot(4,3,2,)
    ax3 = fig.add_subplot(4,3,3,)
    ax1_sub = fig.add_subplot(4,3,4)
    ax2_sub = fig.add_subplot(4,3,5)
    ax3_sub = fig.add_subplot(4,3,6)
    ax4 = fig.add_subplot(4,3,7,)
    ax5 = fig.add_subplot(4,3,8,)
    ax6 = fig.add_subplot(4,3,9,)
    ax4_sub = fig.add_subplot(4,3,10)
    ax5_sub = fig.add_subplot(4,3,11)
    ax6_sub = fig.add_subplot(4,3,12)


    subaxes = [ax1_sub, ax2_sub, ax3_sub, ax4_sub, ax5_sub, ax6_sub]
    mainaxes = [ax1, ax2, ax3, ax4, ax5, ax6]

    for ax in mainaxes:
        ax.set_title("Title")
        ax.plot(x, y_main)
        ax.set_ylabel("y(x)")
        ax.tick_params(labelbottom=False)

    for ax in subaxes:
        ax.plot(x, y_diff)
        ax.set_xlabel("xlabel")
        ax.set_ylabel("1 - y(x)")


    #  plt.tight_layout()
    plt.savefig("mwe.png", dpi=200)
    plt.close()

结果如下图所示:

enter image description here

这正是我想要让第1&2行和第3&4行“粘在一起”的方式。但是,正如您所看到的,轴标签和图形标题现在分别丢失和位于其他图形内。

我该如何仅在两个特定行之间添加一些空间?

1个回答

1
对于这种“特殊”的图形布局,我喜欢使用 gridspec。(另请参见使用Gridspec制作多列/行子图布局)。
它包括subgridspec,通过同时使用两者,您可以按预期单独设置hspaceenter image description here 代码:
import matplotlib as mpl
from matplotlib import pyplot as plt
import numpy as np

params = {
    "xtick.labelsize": 12,
    "ytick.labelsize": 12,
    "xtick.direction": "in",
    "ytick.direction": "in",
    "xtick.top": True,
    "ytick.right": True,
    "figure.subplot.left": 0.045,
    "figure.subplot.right": 0.99,
    "figure.subplot.bottom": 0.05,
    "figure.subplot.top": 0.93,
    "figure.subplot.wspace": 0.15,
    # "figure.subplot.hspace": 0.0,  # done within gridspec
}
mpl.rcParams.update(params)

# invent some data
x = np.linspace(0, 2*np.pi, 100)
y_main = np.sin(x)
y_diff = 1. - y_main

fig = plt.figure(figsize=(18, 22), facecolor=(1, 1, 1))
    
gs = fig.add_gridspec(2, 1, hspace=0.15)  # spacing between the two groups
# print(gs)
    
gs0 = gs[0].subgridspec(2, 3, hspace=0, wspace=0.2)  # spacing within the groups
gs1 = gs[1].subgridspec(2, 3, hspace=0, wspace=0.2)
# print(gs0)

set1, set1_sub = gs0.subplots()
# print(set1)
set1[0].text(0.5, 0.5, "set1[0]")
set1[1].text(0.5, 0.5, "set1[1]")
set1[2].text(0.5, 0.5, "set1[2]")
set1_sub[0].text(0.5, 0.5, "set1_sub[0]")
set1_sub[1].text(0.5, 0.5, "set1_sub[1]")
set1_sub[2].text(0.5, 0.5, "set1_sub[2]")
    
set2 = gs1.subplots()
# print(set2)
set2[0,0].text(0.5, 0.5, "set2[0]")
set2[0,1].text(0.5, 0.5, "set2[1]")
set2[0,2].text(0.5, 0.5, "set2[2]")
set2[1,0].text(0.5, 0.5, "set2_sub[0]")
set2[1,1].text(0.5, 0.5, "set2_sub[1]")
set2[1,2].text(0.5, 0.5, "set2_sub[2]")

mainaxes = [set1[0], set1[1], set1[2], set2[0,0], set2[0,1], set2[0,2]]
subaxes = [set1_sub[0], set1_sub[1], set1_sub[2], set2[1,0], set2[1,1], set2[1,2]]

for ax in mainaxes:
    ax.set_title("Title")
    ax.plot(x, y_main)
    ax.set_ylabel("y(x)")
    ax.tick_params(labelbottom=False)

for ax in subaxes:
    ax.plot(x, y_diff)
    ax.set_xlabel("xlabel")
    ax.set_ylabel("1 - y(x)")

#plt.savefig("mwe.png", dpi=200)
plt.show()

注意:

  • .text 是为了帮助识别位置而包含的,当您获得概述后,请将其删除。
  • set1/2 (groups) 的构建略有不同,以展示 gridspec 的工作方式
    • 两种方式都具有相同的结果
    • 激活 # print 语句以获取更深入的洞见
  • fig.add_gridspec(2, 1) 'main' 网格,具有 2 行和 1 列
  • gs[0].subgridspec(2, 3) 在第一行主网格中的子网格,具有 2 行和 3 列

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接