Matplotlib图像中的Numpy数组堆栈仅在显示每个单独的绘图时有效

Matplotlib图像中的Numpy数组堆栈仅在显示每个单独的绘图时有效,numpy,matplotlib,astropy,astronomy,Numpy,Matplotlib,Astropy,Astronomy,我通过在matplotlib中绘制每个波段,然后使用numpy阵列堆叠,来堆叠三波段rgb图像。问题是,只有当我显示每个绘制的条带时,它才起作用,否则我会在一个大的灰色条带上叠加一个小的彩色图像。我的完整代码有点太长,但这里是最终的堆叠和绘图 img1 = Image.open(dir_input+'/redband.png') img2 = Image.open(dir_input+'/greenband.png') img3 = Image.open(dir_inpu

我通过在matplotlib中绘制每个波段,然后使用numpy阵列堆叠,来堆叠三波段rgb图像。问题是,只有当我显示每个绘制的条带时,它才起作用,否则我会在一个大的灰色条带上叠加一个小的彩色图像。我的完整代码有点太长,但这里是最终的堆叠和绘图

    img1 = Image.open(dir_input+'/redband.png')
    img2 = Image.open(dir_input+'/greenband.png')
    img3 = Image.open(dir_input+'/blueband.png')
    data = np.array(img1, dtype='uint8')
    data2 = np.array(img2, dtype='uint8')
    data3 = np.array(img3, dtype='uint8')
    #Converts images to single channel
    data = data[:,:,0]
    data2 = data2[:,:,0]
    data3 = data3[:,:,0]

    rgb = np.dstack((data,data2,data3)) #Stack the three arrays
    plt.imshow(rgb, cmap = 'hsv') #Shows stacked image and applies HSV colormap
    #Removes white space and axes
    final_file = 'Final_{}'.format(objects)
    plt.title(objects)
    plt.gca().set_axis_off()
    plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)
    plt.margins(0,0)
    plt.gca().xaxis.set_major_locator(plt.NullLocator())
    plt.gca().yaxis.set_major_locator(plt.NullLocator())
    plt.savefig(dir_input+'/'+final_file, bbox_inches = 'tight', pad_inches = 0) #Saves final image in inputted directory
有关图像:

错误的


如果无法访问原始图像,恐怕无法确认(请帮助)。我用不同的png图像尝试了相同的代码(分割示例图像),它似乎工作得很好。@Asmus它似乎与堆叠时带空格的打印格式有关,将其删除是固定的。不确定为什么这一冲突的想法没有访问原始图像,我担心没有办法证实这一点(帮助你)。我用不同的png图像尝试了相同的代码(分割示例图像),它似乎工作得很好。@Asmus它似乎与堆叠时带空格的打印格式有关,将其删除是固定的。不确定为什么会有冲突