【问题标题】:Matplotlib - Change the white space between specific subplots in a grid gridMatplotlib - 更改网格中特定子图之间的空白
【发布时间】:2022-09-28 02:56:19
【问题描述】:

我有一个子图网格,我想调整其中两个之间的空白空间,以便共享的 x 标签居中而不重叠任何一个图。

This question 有一个解决方案,当这些是仅有的两个子图时。但是,我正在努力将其调整为许多网格中的两个特定子图。

这段代码可以用来说明我的问题。

In [1]
fig = plt.figure(figsize = (15, 10))
gs = fig.add_gridspec(2,4)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1:3])
ax3 = fig.add_subplot(gs[0, 3])
ax4 = fig.add_subplot(gs[1, 0])
ax5 = fig.add_subplot(gs[1, 1])
ax6 = fig.add_subplot(gs[1, 2])
ax7 = fig.add_subplot(gs[1, 3])


np.random.seed(19680801)

# Example data
people = (\'Really Really Long Name\', \'Another Long Name\', \'Short Name\', \'Name\', \'N\')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))

ax5.barh(y_pos, performance, align=\'center\')
ax5.set_yticks(y_pos, labels=people)
ax5.invert_xaxis()
ax5.set_xlabel(\'Label\')
ax5.set_title(\'Bar 1\')

ax6.barh(y_pos, performance, align=\'center\')
ax6.set_yticks(y_pos, labels=people)
ax6.set_xlabel(\'Label\')
ax6.set_title(\'Bar 2\')

Out [1]

如果我将解决方案应用于此处的链接问题,那么每个子图的空白都会受到影响。我知道这是因为它调用了fig.dpi_scale_trans,这会影响整个数字,但我是变换的新手,不知道用什么代替它

In [2]
fig.tight_layout()
fig.subplots_adjust(wspace=0.7)

plt.setp(axes[0].yaxis.get_majorticklabels(), ha=\'center\')

# Create offset transform by some points in x direction
dx = 60 / 72.
dy = 0 / 72.
offset = mlb.transforms.ScaledTranslation(dx, dy, fig.dpi_scale_trans)
# apply offset transform to all y ticklabels.
for label in ax6.yaxis.get_majorticklabels():
    label.set_transform(label.get_transform() + offset)

Out [2]

    标签: matplotlib grid whitespace centering axis-labels


    【解决方案1】:

    我想出了如何解决这个问题,所以发布我自己的答案,以防将来有人遇到类似问题。

    7 年前的This question and answer 包含解决问题的必要帮助。

    本质上,您必须使用matplotlib.gridspec 中的GridSpec 在图中绘制和定位不同的GridSpec,而不是使用fig.add_gridspec() 调用一个

    Link to GridSpec documentation

    继上面的示例之后,我想创建一个 2x4 网格。为此,我们可以在图形的设定位置绘制以下网格:

    Left: 1x2
    Top Centre: 1x1
    Bottom Centre: 2x1
    Right: 1x2   
    
    
    In [1]:
    from matplotlib.gridspec import GridSpec
    
    fig = plt.figure(figsize = (15, 10))
    
    # Example Data
    people = ('Really Really Long Name', 'Another Long Name', 'Short Name', 'Name', 
    'N')
    y_pos = np.arange(len(people))
    performance = 3 + 10 * np.random.rand(len(people))
    
    # Left portion of grid (2x1). 
    # 'left' and 'right' tell the grid where it should start and finish
    gs1 = GridSpec(2, 1)
    gs1.update(left = 0, right = 0.2)
    
    # Plotting empty subplots for illustration purposes
    for i in gs1:
        ax = plt.subplot(i)
    
    # Mirroring on the right portion of the grid    
    gs2 = GridSpec(2, 1)
    gs2.update(left = 0.8, right = 1)
    for i in gs2:
        ax = plt.subplot(i)
        
    # Plotting in top center
    # Note here we only need to plot a 1x1
    gs3 = GridSpec(1, 1)
    gs3.update(left = 0.25, right = 0.75, bottom = 0.53) #0.53 aligns with sides
    ax3 = plt.subplot(gs3[0])
    
    # Plotting the barh in the bottom center
    # wsapce only adjusts this grid not the entire figure
    gs4 = GridSpec(1, 2)
    gs4.update(left = 0.2, right = 0.8, top = 0.45, wspace = 0.75) 
    
    
    # Left barh
    ax5 = plt.subplot(gs4[0])
    ax5.barh(y_pos, performance, align='center')
    ax5.set_yticks([])
    ax5.invert_xaxis()
    ax5.set_xlabel('Label')
    ax5.set_title('Bar 1')
    
    # Right barh
    ax6 = plt.subplot(gs4[1])
    ax6.barh(y_pos, performance, align='center')
    ax6.set_yticks(y_pos, labels=people)
    ax6.set_xlabel('Label')
    ax6.set_title('Bar 2')
    
    
    
    plt.show()
    
    Out [1]:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 2023-03-31
      • 2021-11-19
      • 2021-06-27
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多