简体   繁体   English

如何使用 matlib function plt.imshow(image) 显示多个图像?

[英]How do I use the matlib function plt.imshow(image) to display multiple images?

def exiacc():
    global my_img, readimg , im, list_name, exis_img, tkimage
    print('Opening Existing Account...')
    topl = Toplevel()  
    readimg = './facedata/'
    list_img = os.listdir(readimg)
    row, col = 0, 0
    k = 0
    for fn in os.listdir(readimg):
        if fn.endswith('.jpg'):
            list_name.append(fn[:fn.index('_')])
            im = Image.open(os.path.join(readimg, fn))
            im2 = im.resize((200, 200), Image.LANCZOS)
            tkimage = ImageTk.PhotoImage(im2)
            exis_img = Label(topl, image=tkimage)
            exis_img.grid(row = row + 1, column = col + 1, padx=2, pady=2)
            exis_name = Label(topl, text = list_name[k] , font = ("consolas", 12, "bold"))
            exis_name.grid(row=row + 2, column = col + 1, padx=2, pady=2)
            col += 1
            k +=1
            if col == 5:
                row += 2
                col = 0

My results show that only the last processed image is shown effectively overwriting the other images.我的结果表明,只有最后处理的图像有效地覆盖了其他图像。

plt.imshow has a parameter extent=[x0, x1, y0, y1] that can position the image anywhere in a plot. plt.imshow有一个参数extent=[x0, x1, y0, y1]可以 position 图像在 plot 中的任何位置。 Annoying is that imshow forces the aspect ratio to be 'equal' (so distances in x and in y are forced to be the same number of pixels).烦人的是imshow强制纵横比“相等”(因此 x 和 y 中的距离被强制为相同数量的像素)。 Even more annoying is that imshow also forcefully sets the limits in x and in y.更烦人的是, imshow还强行设置了 x 和 y 的限制。 To have more than one image, the limits need to be set explicitly (later than the call to imshow ).要拥有多个图像,需要明确设置限制(在调用imshow )。 If the 'equal' aspect ratio isn't wanted (it often is desired for images) set_aspect('auto') frees the aspect ratio again.如果不需要“相等”的纵横比(图像通常需要它) set_aspect('auto')再次释放纵横比。

Here is an example, using different colormaps to get differently colored images.这是一个示例,使用不同的颜色图来获取不同颜色的图像。

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
cmaps = ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
         'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
         'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']
img = ((np.arange(100) % 10) * np.arange(100)).reshape(10, 10)
cols = 4
for i, cmap in enumerate(cmaps):
    plt.imshow(img, extent=[i % cols, i % cols +1, i // cols, i // cols +1], cmap=cmap)
plt.xlim(0, cols)
plt.ylim(0, (len(cmaps) - 1) // cols + 1)
plt.gca().set_aspect('auto') # allow a free aspect ratio, to fully fit the plot figure
plt.show()

示例图

A straight forward solution would be to use plt.subplots instead:一个直接的解决方案是使用 plt.subplots 代替:

import numpy as np
import matplotlib.pyplot as plt

lst_cmaps = ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
         'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
         'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn']

data = ((np.arange(100) % 10) * np.arange(100)).reshape(10, 10)

nxn = int(np.sqrt(len(lst_cmaps)))

plt, ax = plt.subplots(nxn, nxn)

for i, ax_ in enumerate(ax.flatten()):
    ax_.imshow(data, cmap=lst_cmaps[i])

This gives you an image like this:这会给你一个像这样的图像:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM