Python Matplotlib子批次卡在一起

Python Matplotlib子批次卡在一起,python,matplotlib,Python,Matplotlib,我对mnist数据集进行了建模,测试集(x\u test)的长度为2000位图像,模型在测试集中使用索引error\u predicted\u images对图像进行了错误预测。我正在尝试绘制图像以及正确的标签(来自测试集:y\u test)。我想到了这个。然而,当我绘制它时,所有的图像都被卡在一起了 num_cols = 5 num_images = len(wrong_predicted_images) num_rows = num_images//num_cols +1 f, axarr

我对mnist数据集进行了建模,测试集(
x\u test
)的长度为2000位图像,模型在测试集中使用索引
error\u predicted\u images
对图像进行了错误预测。我正在尝试绘制图像以及正确的标签(来自测试集:
y\u test
)。我想到了这个。然而,当我绘制它时,所有的图像都被卡在一起了

num_cols = 5
num_images = len(wrong_predicted_images)
num_rows = num_images//num_cols +1
f, axarr = plt.subplots(num_rows, num_cols, sharex='col', sharey='row')
f.subplots_adjust(hspace=1,wspace= 1) # to have space between images
for count, i in enumerate(wrong_predicted_images):
  x, y = count//num_cols, count % num_cols
  axarr[x,y].imshow(x_test[i])
  axarr[x,y].imshow(x_test[i])
  axarr[x,y].set_title(y_test[i], loc = 'right')
  plt.grid(False)
plt.show()
In [28]: N = 299 
    ...: nc = 5 
    ...: nr = (N-1)//nc + 1 
    ...: f, axarr = plt.subplots(nr, nc, sharex='col', sharey='row') 
    ...: f.subplots_adjust(hspace=1, wspace=1) 
    ...: for c in range(N): 
    ...:     x, y = c//nc, c%nc 
    ...:     axarr[x,y].imshow(img) 
    ...:     axarr[x,y].set_title('title')                                                
这是生成的图像

In [28]: N = 299 
    ...: nc = 5 
    ...: nr = (N-1)//nc + 1 
    ...: f, axarr = plt.subplots(nr, nc, sharex='col', sharey='row') 
    ...: f.subplots_adjust(hspace=1, wspace=1) 
    ...: for c in range(N): 
    ...:     x, y = c//nc, c%nc 
    ...:     axarr[x,y].imshow(img) 
    ...:     axarr[x,y].set_title('title')                                                

In [28]: N = 299 
    ...: nc = 5 
    ...: nr = (N-1)//nc + 1 
    ...: f, axarr = plt.subplots(nr, nc, sharex='col', sharey='row') 
    ...: f.subplots_adjust(hspace=1, wspace=1) 
    ...: for c in range(N): 
    ...:     x, y = c//nc, c%nc 
    ...:     axarr[x,y].imshow(img) 
    ...:     axarr[x,y].set_title('title')                                                

如何解决这个问题?

我猜您试图在一个图形中绘制太多的图像,在下面我将使用9对299图像模拟您的过程,第二个图形与您所显示的非常相似

In [26]: import matplotlib.pyplot as plt 
    ...: import numpy as np 
    ...: img = np.arange(54*108).reshape(108,54)                                          

In [27]: N = 9 
    ...: nc = 5 
    ...: nr = (N-1)//nc + 1 
    ...: f, axarr = plt.subplots(nr, nc, sharex='col', sharey='row') 
    ...: f.subplots_adjust(hspace=1, wspace=1) 
    ...: for c in range(N): 
    ...:     x, y = c//nc, c%nc 
    ...:     axarr[x,y].imshow(img) 
    ...:     axarr[x,y].set_title('title')
In [28]: N = 299 
    ...: nc = 5 
    ...: nr = (N-1)//nc + 1 
    ...: f, axarr = plt.subplots(nr, nc, sharex='col', sharey='row') 
    ...: f.subplots_adjust(hspace=1, wspace=1) 
    ...: for c in range(N): 
    ...:     x, y = c//nc, c%nc 
    ...:     axarr[x,y].imshow(img) 
    ...:     axarr[x,y].set_title('title')                                                

In [28]: N = 299 
    ...: nc = 5 
    ...: nr = (N-1)//nc + 1 
    ...: f, axarr = plt.subplots(nr, nc, sharex='col', sharey='row') 
    ...: f.subplots_adjust(hspace=1, wspace=1) 
    ...: for c in range(N): 
    ...:     x, y = c//nc, c%nc 
    ...:     axarr[x,y].imshow(img) 
    ...:     axarr[x,y].set_title('title')                                                

num\u rows=(num\u images-1)//num\u cols+1
不可能对大数字进行分页吗?如果不是,也许我应该每10幅图像循环一次,然后创建一个图形并绘制它们。据我所知,一个图形就是一个图形,如果我是对的,你必须决定一个图形中合理地包含多少个图像,然后在图形、行和列上循环——顺便说一句,我扩展了我的答案,以包括这样一个解决方案。棘手的部分是删除最后一个图中未使用的子图…这太棘手了,因为很容易删除最后的子图,但这会导致另一个问题,x轴标记没有绘制。。。对于您来说,最好在图下方有空白的子批次,但正确的XTICK。
In [28]: N = 299 
    ...: nc = 5 
    ...: nr = (N-1)//nc + 1 
    ...: f, axarr = plt.subplots(nr, nc, sharex='col', sharey='row') 
    ...: f.subplots_adjust(hspace=1, wspace=1) 
    ...: for c in range(N): 
    ...:     x, y = c//nc, c%nc 
    ...:     axarr[x,y].imshow(img) 
    ...:     axarr[x,y].set_title('title')