使用多个子图显示/保存多个图形(matplotlib/python)

使用多个子图显示/保存多个图形(matplotlib/python),python,matplotlib,plot,subplot,Python,Matplotlib,Plot,Subplot,我正在对大量图像进行预处理(裁剪、通过网络运行等),然后将这些图像以单位形式存储。从这些单元中,我绘制了一个带有多个子图的图形,显示了特定卷积层上图像的激活 我正在努力实现的是,给定一组多个图像,每个图像的数字(每个图像的图形)将与每个图形子图一起显示或保存到一个目录(如果这样可以更容易在jupyter中处理的话) 转换为单位的单个图像的单位: [[0.0000000e+00 0.0000000e+00 0.0000000e+00 ... 3.3075356e-01 0.0000000e

我正在对大量图像进行预处理(裁剪、通过网络运行等),然后将这些图像以单位形式存储。从这些单元中,我绘制了一个带有多个子图的图形,显示了特定卷积层上图像的激活

我正在努力实现的是,给定一组多个图像,每个图像的数字(每个图像的图形)将与每个图形子图一起显示或保存到一个目录(如果这样可以更容易在jupyter中处理的话)

转换为单位的单个图像的单位:

[[0.0000000e+00 0.0000000e+00 0.0000000e+00 ... 3.3075356e-01
    0.0000000e+00 0.0000000e+00]
   [0.0000000e+00 1.4396116e-01 0.0000000e+00 ... 0.0000000e+00
    0.0000000e+00 0.0000000e+00]
   [0.0000000e+00 5.4249477e-01 1.9857159e-01 ... 0.0000000e+00
    1.5366032e+00 1.0890217e+00]
   ...
   [7.5161266e-01 9.6204914e-02 6.8889879e-02 ... 1.3110014e+00
    5.4128194e-01 4.9922270e-01]
   [7.5161266e-01 9.6204914e-02 6.8889879e-02 ... 1.3110014e+00
    5.4128194e-01 4.9922270e-01]
   [7.5161266e-01 9.6204914e-02 6.8889879e-02 ... 1.3110014e+00
    5.4128194e-01 4.9922270e-01]]
职能:

def getActivations(layer,stimuli):
    with tf.Session(graph=graph) as sess:
        #print (stimuli)
        #im=stimuli
        im=np.reshape(stimuli,[-1,224,224],order='F')#stimuli
        im=np.expand_dims(im,axis=0)

        #im=np.reshape(im,[-1,224,224],order='F')#stimuli
        #plt.imshow(im,interpolation="nearest", cmap="gray")
        #print (im)
        #for im in stimuli:
        #batch = np.array([im for i in range(1)])
        x = graph.get_tensor_by_name('prefix/data:0')
        #x2 = tf.reshape(x,[-1,224,224])
        y=graph.get_tensor_by_name(layer)
        units = sess.run(y,feed_dict={x: np.swapaxes(im,1,3)})#np.reshape(stimuli,[-1,224,224],order='F'),keep_prob:1.0})
        #print (units)
        plotNNFilter(units)



def plotNNFilter(units):
    #for a in units:

    #print ("###############################################################")
    #print (units)
    filters = units.shape[3]
    #print ("###############################################################")
    #print (filters)
    plt.figure(1,figsize=(20,20))
    n_columns = 6
    n_rows = math.ceil(filters / n_columns) + 1
    for i in range (filters):
        #plt.subplot(n_rows,n_columns, i+1)
        plt.subplot(n_rows,n_columns, i+1)
        plt.title('Filter' + str(i))
        plt.imshow(units[0,:,:,i],interpolation="nearest",cmap="gray")
我得到了这个折旧错误:

MatplotlibDeprecationWarning:使用相同的参数添加轴 作为前一个实例,axes当前重用前一个实例。将来 版本时,将始终创建并返回一个新实例。 同时,此警告可以被抑制,并且未来的行为 通过向每个轴实例传递唯一的标签来确保。 warnings.warn(消息,mpldeprection,stacklevel=1)

我看到了另一个带有相同错误警告的问题:

但我认为答案并不适用于我想要实现的目标?


打印透镜(单位),以便我可以使用数字并将其作为迭代来进行plt。图(i…)为每个单元打印1。

您可以首先使用
fig,axes=plt创建子批次。子批次(numrows,numcols)
<代码>轴将是一个子图数组,您可以迭代并绘制任何您喜欢的图

注意:行数和列数必须是整数

filters = units.shape[3]
n_columns = 6
n_rows = int(math.ceil(filters / n_columns) + 1)
fig, axes = plt.subplots(n_rows, n_columns, figsize=(20, 20))

for i, ax in enumerate(axes.flatten()):
    if i>=filters:
        ax.remove()
    else:
        ax.set_title('Filter' + str(i))
        ax.imshow(units[0, :, :, i], interpolation="nearest", cmap="gray")

是一个子地块的数组,因此要在它们之间循环,我们需要展平此数组。然后我们循环遍历它们,将
ax
分配给每个子批,
i
本质上是一个计数器。因为没有使用所有子批次(最后两个将为空),我检查
I
是否大于或等于图像数量
,如果I>=过滤器:
,如果是这样,我将删除这些子批次。如果这不是真的,我们继续绘制图像。

为什么不先创建子图,然后使用面向对象的API对其进行迭代?我刚刚开始在这个堆栈中编程,因此还不知道太多。HI@DavidG,这引发了“fig,axes…”行a TypeError:“float”对象不能解释为index@ProddoBaggins
n_行
是一个浮点数。但是,传递给
plt.subplot()
的行数和列数必须是整数。我已经更新了我的回答。这会生成第一个(?)图形,然后抛出:Indexer错误:索引64超出轴3的范围,大小为64。我已经计算了子批次,因此它会正确打印一个带有64个子批次的图形,然后“尝试”在停止之前打印另一个2,其尺寸与之前的子批次不同@DavidG@ProddoBaggins您能告诉我
n_行
的值是多少吗(没有int()转换)?