python - 在 matplotlib 中绘制多个图时出现索引错误

标签 python matplotlib

我正在尝试通过子图绘制多个图表。代码“有效”,但它总是给我一个索引错误,我一生都无法弄清楚。

作为一个附带问题,我想知道是否有人知道如何使每个单独的图保持相同的大小。例如,如果我添加更多的行或列,那么每个图都会变小。谢谢。

count = 0
n_rows = 2
n_columns = 2
f, axarr = plt.subplots(n_rows, n_columns)
plt.figure(figsize=(20,20))

for column in range(n_cols):
    for row in range(n_rows):
        axarr[row, column].imshow(generate_pattern('block3_conv1', count, size=150))

        count += 1

enter image description here

错误

IndexError                                Traceback (most recent call last)
<ipython-input-37-7f7ae19e07e9> in <module>()
      7 for column in range(n_cols):
      8     for row in range(n_rows):
----> 9         axarr[row, column].imshow(generate_pattern('block3_conv1', count, size=150))
     10 
     11         count += 1

IndexError: index 2 is out of bounds for axis 1 with size 2

所使用函数的代码

def generate_pattern(layer_name, filter_index, size=150):
    # Build a loss function that maximizes the activation
    # of the nth filter of the layer considered.
    layer_output = model.get_layer(layer_name).output
    loss = K.mean(layer_output[:, :, :, filter_index])

    # Compute the gradient of the input picture wrt this loss
    grads = K.gradients(loss, model.input)[0]

    # Normalization trick: we normalize the gradient
    grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)

    # This function returns the loss and grads given the input picture
    iterate = K.function([model.input], [loss, grads])

    # We start from a gray image with some noise
    input_img_data = np.random.random((1, size, size, 3)) * 20 + 128.

    # Run gradient ascent for 40 steps
    step = 1.
    for i in range(40):
        loss_value, grads_value = iterate([input_img_data])
        input_img_data += grads_value * step

    img = input_img_data[0]
    return deprocess_image(img)

def deprocess_image(x):
    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1

    x += 0.5
    x = np.clip(x,0,1)

    x *= 255
    x = np.clip(x,0,255).astype('uint8')

    return x  

最佳答案

此错误是由于尝试使用超出索引范围的值对 plt.subplots() 生成的数组进行索引而导致的。展示这一点的一种方法是将循环中的变量仅替换为数字。在这种情况下,您将看到 axarr[1,2] 将产生以下错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-39-31f90736bd1d> in <module>()
      2 #plt.figure(figsize=(20,20))
      3 
----> 4 a[0,2]

IndexError: index 2 is out of bounds for axis 1 with size 2

我们知道错误并未发生在 generate_pattern 函数中,因为错误消息已表明了这一点。

关于python - 在 matplotlib 中绘制多个图时出现索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52268195/

相关文章:

python - 围绕一个点绘制/绘制一个具有一定半径的圆(matplotlib)

python - 用黑线绘制 3D 图形,其中 Z = 0?

python - python 中的 matplotlib - 使用 x 轴中的相应值和大数绘制图形

python - 在 Python 中使用整数除法时可以保持小数精度吗?

python - 读取 csv 标题空白和不区分大小写

python - 优化这个 Django 代码?

python - 为什么子图在 x 轴上产生空白区域?

python - 如何使用 matplotlib 或 seaborn 在两行中绘制 5 个子图?

python - 如何缩小 avi 文件的数据集输出

python - Pandas - 更改因子类型对象的级别顺序