Python 是否使用受约束的布局添加外部页边距?

Python 是否使用受约束的布局添加外部页边距?,python,matplotlib,Python,Matplotlib,生成要保存到pdf文件的图形时,我希望调整图形相对于页面边缘的位置,例如,在所有边上添加一英寸的边距。据我所知,解决方案如下(例如,在中): 不要使用约束布局模式——在创建图形之后,但在图savefig()之前,应用plt.subplot\u adjust()会弄乱约束布局 实际上,不要定量地调整图形的位置——添加bbox\u inches=“tight”或pad=-1似乎没有任何意义 是否有一种直接的方法来调整受约束布局图形的外部边距 例如: fig = plt.figure(constrai

生成要保存到pdf文件的图形时,我希望调整图形相对于页面边缘的位置,例如,在所有边上添加一英寸的边距。据我所知,解决方案如下(例如,在中):

  • 不要使用
    约束布局
    模式——在创建图形之后,但在
    图savefig()之前,应用
    plt.subplot\u adjust()
    会弄乱约束布局
  • 实际上,不要定量地调整图形的位置——添加
    bbox\u inches=“tight”
    pad=-1似乎没有任何意义
  • 是否有一种直接的方法来调整受约束布局图形的外部边距

    例如:

    fig = plt.figure(constrained_layout=True, figsize=(11, 8.5))
    
    page_grid = gridspec.GridSpec(nrows=2, ncols=1, figure=fig)
    
    # this doesn't appear to do anything with constrained_layout=True
    page_grid.update(left=0.2, right=0.8, bottom=0.2, top=0.8)
    
    top_row_grid = gridspec.GridSpecFromSubplotSpec(1, 3, subplot_spec=page_grid[0])
    for i in range(3):
        ax = fig.add_subplot(top_row_grid[:, i], aspect="equal")
    
    n_bottom_row_plots = 10
    qc_grid = gridspec.GridSpecFromSubplotSpec(1, n_bottom_row_plots, subplot_spec=page_grid[1])
    for i, metric in enumerate(range(n_bottom_row_plots)):
        ax = fig.add_subplot(qc_grid[:, i])
        plt.plot(np.arange(5), np.arange(5))
    
    fig.suptitle("my big label", fontweight="bold", fontsize="x-large", y=0.9)
    
    # this ruins the constrained layout
    # plt.subplots_adjust(left=0.2,right=0.8, bottom=0.2, top=0.8)
    
    fig.savefig("temp.png", facecolor="coral")
    
    产生以下结果(我希望看到更多的珊瑚围绕边缘!):


    如果您想获得更大的灵活性,我个人不建议使用约束布局并同时指定[
    左侧
    右侧
    底部
    顶部
    ]。您可以自己指定边距,也可以让matplotlib CONTAINE layout为您重新设置边距。要在放置文本和标签的轴之间留出更多空间,只需使用
    hspace
    wspace
    进行调整

    如果我想从两边留出更多的边距,并且有足够的空间放置轴标签、勾号标签和一些文本。我是这样做的

    fig = plt.figure(figsize=(11, 8.5), facecolor='coral')
    # you code already has this
    left, right, bottom, top = [0.1, 0.95, 0.1, 0.5]
    # You can specify wspace and hspace to hold axes labels and some other texts.
    wspace = 0.25
    hspace = 0.1
    
    nrows=1
    ncols=3
    gs1 = fig.add_gridspec(nrows=1, ncols=3, left=left, right=right, bottom=0.6, 
    top=0.9, wspace=wspace, hspace=hspace)
    axes1 = [fig.add_subplot(gs1[row, col]) for row in range(nrows) for col in 
    range(ncols)]
    
    nrows=1
    ncols=10
    # this grid have larger wspace than gs1
    gs2 = fig.add_gridspec(nrows=1, ncols=ncols, left=left, right=right, 
    bottom=0.1, top=top, wspace=0.6, hspace=hspace)
    axes2 = [fig.add_subplot(gs2[row, col]) for row in range(nrows) for col in 
    range(ncols)]
    

    您是否尝试过使用受约束的布局填充选项

    fig.set_constrained_layout_pads(w_pad=4./72., h_pad=4./72.,
                hspace=0./72., wspace=0./72.)
    
    虽然这可能有助于增加间距,但受约束布局将限制可以添加到定义空间中的对象数量

    ''' Here is the modified code '''
    
    import matplotlib.pyplot as plt
    import matplotlib.colors as mcolors
    import matplotlib.gridspec as gridspec
    import numpy as np
    
    
    fig = plt.figure(constrained_layout=True, figsize=(11, 8.5))
    fig.set_constrained_layout_pads(w_pad=2./12., h_pad=4./12.,
                hspace=0., wspace=0.)
    page_grid = gridspec.GridSpec(nrows=2, ncols=1, figure=fig)
    
    fig.suptitle("My BIG Label", fontweight="bold", fontsize="x-large", y=0.98)
    
    
    # this doesn't appear to do anything with constrained_layout=True
    page_grid.update(left=0.2, right=0.8, bottom=0.2, top=0.8)
    
    top_row_grid = gridspec.GridSpecFromSubplotSpec(1, 3, subplot_spec=page_grid[0])
    for i in range(3):
        ax = fig.add_subplot(top_row_grid[:, i], aspect="equal")
    
    n_bottom_row_plots = 10
    qc_grid = gridspec.GridSpecFromSubplotSpec(1, n_bottom_row_plots, subplot_spec=page_grid[1])
    for i, metric in enumerate(range(n_bottom_row_plots)):
        ax = fig.add_subplot(qc_grid[:, i])
        plt.plot(np.arange(5), np.arange(5))
    
    
    # this ruins the constrained layout
    # plt.subplots_adjust(left=0.2,right=0.8, bottom=0.2, top=0.8)
    
    fig.savefig("temp.png", facecolor="coral")
    

    从matpotlib 3.3.1开始,使用
    constrated_layout=False
    保存的图像正确显示右边距。您使用的是什么版本?@r-初学者我正在运行3.2.2,但我有
    constrated\u layout=True
    。。。正确显示右边距是什么意思?右边距与左边距的宽度相同。对于这个玩具示例来说,这很好。。。直到你开始向情节中添加内容。由于您已经关闭了
    约束布局
    ,我又回到了mpl的地狱,调整标签大小和间距的所有可能组合。这个想法适用于非常灵活的布局。在回答中,我没有做任何改变标签大小的事情。即使使用约束布局,更改标签大小也需要附加代码,除非使用自己的默认值。您只需更改
    wspace
    和'hspace'。从我的经验来看,受限布局意味着灵活性降低。谢谢,这是朝着正确方向迈出的一步!由于缺乏更好的答案,我猜测这应该是mpl上的一个功能请求票证:)