0

嘿嘿,我从 SO 得到这个代码:(
链接)

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)

它工作得很好,但我无法调整图像的大小*哭*
我可以用这个吗?

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

我到处都试过了,但它只是让我打印了这个:

<图形尺寸 720x720 0 轴>

感谢所有帮助。问候, 伊莱

4

1 回答 1

1

因此,您可以使用 plt.gcf() 来获取当前图形实例,因此在您上面发布的代码之后,尝试:

f = plt.gcf()

进而:

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

这可以调整整体图形尺寸。当您使用plt.figure(figsize=(10,10))时,它只是创建了一个新的图形实例,而不是调整已经存在的图形。

另一种方法是让该plot_figure方法返回图形实例,这样您就不必使用该plt.gcf方法。

于 2020-10-15T12:40:28.717 回答