'Multiple figures in a single window

I want to create a function which plot on screen a set of figures in a single window. By now I write this code:

import pylab as pl

def plot_figures(figures):
    """Plot a dictionary of figures.

    Parameters
    ----------
    figures : <title, figure> dictionary

    """
    for title in figures:
        pl.figure()
        pl.imshow(figures[title])
        pl.gray()
        pl.title(title)
        pl.axis('off')

It works perfectly but I would like to have the option for plotting all the figures in single window. And this code doesn't. I read something about subplot but it looks quite tricky.



Solution 1:[1]

You should use subplot.

In your case, it would be something like this (if you want them one on top of the other):

fig = pl.figure(1)
k = 1
for title in figures:
    ax = fig.add_subplot(len(figures),1,k)
    ax.imshow(figures[title])
    ax.gray()
    ax.title(title)
    ax.axis('off')
    k += 1

Check out the documentation for other options.

Solution 2:[2]

If you want to group multiple figures in one window you can do smth. like this:

import matplotlib.pyplot as plt
import numpy as np


img = plt.imread('C:/.../Download.jpg') # Path to image
img = img[0:150,50:200,0] # Define image size to be square --> Or what ever shape you want

fig = plt.figure()

nrows = 10 # Define number of columns
ncols = 10 # Define number of rows
image_heigt = 150 # Height of the image
image_width = 150 # Width of the image


pixels = np.zeros((nrows*image_heigt,ncols*image_width)) # Create 
for a in range(nrows):
    for b in range(ncols):
        pixels[a*image_heigt:a*image_heigt+image_heigt,b*image_heigt:b*image_heigt+image_heigt] = img
plt.imshow(pixels,cmap='jet')
plt.axis('off')
plt.show()

As result you receive: enter image description here

Solution 3:[3]

Building on the answer from: How to display multiple images in one figure correctly?, here is another method:

import math
import numpy as np
import matplotlib.pyplot as plt

def plot_images(np_images, titles = [], columns = 5, figure_size = (24, 18)):
    count = np_images.shape[0]
    rows = math.ceil(count / columns)

    fig = plt.figure(figsize=figure_size)
    subplots = []
    for index in range(count):
        subplots.append(fig.add_subplot(rows, columns, index + 1))
        if len(titles):
            subplots[-1].set_title(str(titles[index]))
        plt.imshow(np_images[index])

    plt.show()

Solution 4:[4]

You can also do this:

import matplotlib.pyplot as plt

f, axarr = plt.subplots(1, len(imgs))
for i, img in enumerate(imgs):
    axarr[i].imshow(img)

plt.suptitle("Your title!")
plt.show()

Solution 5:[5]

def plot_figures(figures, nrows=None, ncols=None):
    if not nrows or not ncols:
        # Plot figures in a single row if grid not specified
        nrows = 1
        ncols = len(figures)
    else:
        # check minimum grid configured
        if len(figures) > nrows * ncols:
            raise ValueError(f"Too few subplots ({nrows*ncols}) specified for ({len(figures)}) figures.")

    fig = plt.figure()

    # optional spacing between figures
    fig.subplots_adjust(hspace=0.4, wspace=0.4)

    for index, title in enumerate(figures):
        plt.subplot(nrows, ncols, index + 1)
        plt.title(title)
        plt.imshow(figures[title])
    plt.show()

Any grid configuration (or none) can be specified as long as the product of the number of rows and the number of columns is equal to or greater than the number of figures.

For example, for len(figures) == 10, these are acceptable

plot_figures(figures)
plot_figures(figures, 2, 5)
plot_figures(figures, 3, 4)
plot_figures(figures, 4, 3)
plot_figures(figures, 5, 2)

Solution 6:[6]

import numpy as np

def save_image(data, ws=0.1, hs=0.1, sn='save_name'):
    import matplotlib.pyplot as plt
    m = n = int(np.sqrt(data.shape[0])) # (36, 1, 32, 32)

    fig, ax = plt.subplots(m,n, figsize=(m*6,n*6))
    ax = ax.ravel()
    for i in range(data.shape[0]):
        ax[i].matshow(data[i,0,:,:])
        ax[i].set_xticks([])
        ax[i].set_yticks([])

    plt.subplots_adjust(left=0.1, bottom=0.1, right=0.9, 
                        top=0.9, wspace=ws, hspace=hs)
    plt.tight_layout()
    plt.savefig('{}.png'.format(sn))

data = np.load('img_test.npy')

save_image(data, ws=0.1, hs=0.1, sn='multiple_plot')

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oriol Nieto
Solution 2 2Obe
Solution 3 Breck
Solution 4 tsveti_iko
Solution 5 vorian77
Solution 6 GuokLiu