【问题标题】:Add external margins with constrained layout?添加具有约束布局的外部边距?
【发布时间】:2020-12-09 19:46:48
【问题描述】:

在生成要保存到 pdf 文件的图形时,我想调整图形相对于页面边缘的位置,例如在所有边上添加英寸边距。据我所知,执行此操作的解决方案(例如,this question):

  1. 不要使用constrained_layout 模式——在创建图形之后但在fig.savefig() 之前应用plt.subplots_adjust() 会破坏受约束的布局
  2. 实际上不要对图的位置进行量化调整——添加bbox_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")

产生以下效果(我希望在边缘看到更多珊瑚!):

【问题讨论】:

  • 从 matpotlib 3.3.1 开始,使用constrained_layout=False 保存的图像正确显示右边距。你用的是什么版本?
  • @r-beginners 我正在运行 3.2.2,但我有 constrained_layout=True... 你是什么意思右边距显示正确?
  • 与左边距相同的宽度在右边距中

标签: python matplotlib


【解决方案1】:

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

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")

【讨论】:

  • 谢谢,这是朝着正确方向迈出的一步!我猜测由于缺乏更好的答案,这应该是 mpl 上的功能请求票 :)
【解决方案2】:

如果你想获得更大的灵活性,我个人不建议使用约束布局并指定 [left,right,bottom,top]。要么自己指定边距,要么让 matplotlib 约束布局为你重新排列。要在轴之间留出更多空间来放置文本和标签,只需使用hspacewspace 进行调整。

如果我想要两边的边距更大,并且有足够的空间放置轴标签、刻度标签和一些文本。我按照以下方式进行。

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)]

【讨论】:

  • 嗯,这对于这个玩具示例来说效果很好......直到你开始向情节添加东西。既然你关闭了constrained_layout,我又回到了调整标签大小和间距的所有可能组合的 mpl 地狱中。
  • 这个想法适用于相当灵活的布局。在答案中,我没有做任何事情来改变标签大小。即使您使用受约束的布局,更改标签大小也需要额外的代码,除非您使用自己的默认值。您只需要更改wspace 和`hspace'。至少从我的经验来看,受限的布局意味着更少的灵活性。
猜你喜欢
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多