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.

    figures : <title, figure> dictionary

    for title in figures:

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.


You can define a function based on the subplots command (note the s at the end, different from the subplot command pointed by urinieto) of matplotlib.pyplot.

Below is an example of such a function, based on yours, allowing to plot multiples axes in a figure. You can define the number of rows and columns you want in the figure layout.

def plot_figures(figures, nrows = 1, ncols=1):
    """Plot a dictionary of figures.

    figures : <title, figure> dictionary
    ncols : number of columns of subplots wanted in the display
    nrows : number of rows of subplots wanted in the figure

    fig, axeslist = plt.subplots(ncols=ncols, nrows=nrows)
    for ind,title in enumerate(figures):
        axeslist.ravel()[ind].imshow(figures[title], cmap=plt.gray())
    plt.tight_layout() # optional

Basically, the function creates a number of axes in the figures, according to the number of rows (nrows) and columns (ncols) you want, and then iterates over the list of axis to plot your images and adds the title for each of them.

Note that if you only have one image in your dictionary, your previous syntax plot_figures(figures) will work since nrows and ncols are set to 1 by default.

An example of what you can obtain:

import matplotlib.pyplot as plt
import numpy as np

# generation of a dictionary of (title, images)
number_of_im = 6
figures = {'im'+str(i): np.random.randn(100, 100) for i in range(number_of_im)}

# plot of the images in a figure, with 2 rows and 3 columns
plot_figures(figures, 2, 3)

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)
    k += 1

Check out the documentation for other options.

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):

You can also do this:

import matplotlib.pyplot as plt

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

plt.suptitle("Your title!")

Need Your Help

Can't get my object to point at the mouse

math opengl input sdl

I'm using a combination of SDL and OpenGL in a sort of crash course project to teach myself how this all works. I'm really only interested in OpenGL as a way to use acceleration in 2D games so I just

How to get the offsetParent() which is relative only

javascript jquery css css-position

I have searched this one a bit now. Is there a way to get only the offsetParent() of an element where that parent is positioned relative only? I don't want other parents with all positions. Just the