【问题标题】:pyplot figsize with multiple figures带有多个数字的pyplot figsize
【发布时间】:2026-01-20 18:30:01
【问题描述】:

嘿嘿, 我从 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)

效果很好,但我无法调整图像的大小 *cry*
我可以用这个吗?

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

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

感谢所有帮助。 问候, 以利

【问题讨论】:

    标签: python matplotlib figsize


    【解决方案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 方法。

    【讨论】:

    • 非常感谢,这就是我所需要的