pythonmatplotlibfigsize

pyplot figsize with multiple figures


Heyho, I've got this code from SO:
(link)

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

    Parameters
    ----------
    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())
        axeslist.ravel()[ind].set_title(title)
        axeslist.ravel()[ind].set_axis_off()
    plt.tight_layout() # optional

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)

it works perfectly fine, but I just can't resize the images *cry*
can I work with this?

plt.figure(figsize=(10,10))

I tried it everywhere, but it just gives me printed this:

<Figure size 720x720 with 0 Axes>

Appreciate all help. Regards, Eli


Solution

  • So, you could use plt.gcf() to get the current figure instance, so after the code you posted above, try:

    f = plt.gcf()
    

    and then:

    f.set_figwidth(10)  # Sets overall figure width to 10 inches
    f.set_figheight(10)  # Sets overall figure height to 10 inches
    

    This works to adjust the overall figures dimensions. When you used plt.figure(figsize=(10,10)), it merely created a new figure instance, instead of adjusting that figure already there.

    An alternative would be to have the plot_figure method return the figure instance so you don't have to use the plt.gcf method.