【问题标题】:Reaching limitations creating a gallery of plots达到限制创建地块画廊
【发布时间】:2015-11-07 20:54:17
【问题描述】:

在我使用的脚本中,代码生成了一个图形,其中生成了许多子图。通常它会创建一个矩形的绘图网格,但在当前使用中,水平参数只有 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 因为图是重叠的,但我不知道如何在不改变图中排列方式的情况下创建更小的图。这些图保持在垂直方向的列中很重要。

【问题讨论】:

    标签: python-2.7 matplotlib figure subplot


    【解决方案1】:

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

    # 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!
    

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

    诚然,第一个子图上方和最后一个子图下方有很多空间,但您可以通过使用图形尺寸或gs.update来解决这个问题

    【讨论】:

    • 非常感谢您抽出宝贵时间试用。对于 gridspec 的混淆,我深表歉意——我更改了变量名称以使发生的事情更清楚,但不小心把它们放在了错误的位置。即使我添加了 figsize 关键字,我仍然无法通过图形创建线。任何线索那里发生了什么?
    • 能够通过在图形创建和保存过程中指定 dpi 来解决该问题。感谢您的帮助!
    猜你喜欢
    • 2015-10-03
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多