达到限制创建地块画廊

Reaching limitations creating a gallery of plots

在我使用的脚本中,代码生成了一个图形,其中生成了许多子图。通常它会创建一个矩形网格图,但就其当前用途而言,水平参数只有 1 个值,而垂直参数的值比以前多得多。这导致我的程序在 运行 时崩溃,因为(大概)垂直尺寸太大。导致问题的代码是:

#can't get past the first line here
self.fig1 = plt.figure('Title',figsize=(4.6*numXparams,2.3*numYparams))
self.gs = gridspec.GridSpec(numYparams,numXparams)
self.gs.update(left=0.03, right=0.97, top=0.9, bottom=0.1, wspace=0.5, hspace=0.5)

然后在两个参数的嵌套 for 循环中 运行:

ax = plt.subplot(self.gs[par0, par1])

我得到的错误是:

X Error of failed request: badAlloc (insufficient resources for operation)
Major opcode of failed request: 53 (X_CreatePixmap)
Serial number of failed request: 295
Current serial number in output stream: 296

我的垂直参数目前有 251 个值,所以我可以看到 251*2.3 英寸会如何导致麻烦。我添加了 2.3*numYparams 因为绘图重叠,但我不知道如何在不改变绘图在图中的排列方式的情况下创建更小的图形。这些图保持在垂直方向的列中很重要。

您的代码中有几个错误。修复它们使我能够生成您要求的数字。

# I needed the figsize keyword here
self.fig1 = plt.figure(figsize=(4.6*numXparams,2.3*numYparams))

# You had x and y switched around here
self.gs = gridspec.GridSpec(numYparams,numXparams)
self.gs.update(left=0.03, right=0.97, top=0.9, bottom=0.1, wspace=0.5, hspace=0.5)

# I ran this loop
for i in range(numYparams):
    ax = fig1.add_subplot(gs[i, 0]) # note the y coord in the gridspec comes first
    ax.text(0.5,0.5,i) # just an identifier

fig1.savefig('column.png',dpi=50) # had to drop the dpi, because you can't have a png that tall!

这是输出图的顶部和底部:

不可否认,在第一个子图上方和最后一个子图下方有很多 space,但您可以通过调整图形尺寸或 gs.update

来解决这个问题