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.
You can define a function based on the subplots command (note the s at the end, different from the
subplotcommand pointed by urinieto) ofmatplotlib.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.
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 sincenrowsandncolsare set to1by default.An example of what you can obtain: