Python 防止matplotlib随着打印图像数量的增加而减小打印图像的大小

Python 防止matplotlib随着打印图像数量的增加而减小打印图像的大小,python,matplotlib,Python,Matplotlib,我正在使用下面的代码绘制一些图像: start, end = 0, 3 fig = plt.figure(figsize=(25, 25)) n_cols = 3 n_rows = len(images_dict[start:end]) for i, d in enumerate(images_dict[start:end]): ax1 = fig.add_subplot(n_rows, n_cols, n_cols * i + 1) ax2 = fig.add_subplot(

我正在使用下面的代码绘制一些图像:

start, end = 0, 3
fig = plt.figure(figsize=(25, 25))
n_cols = 3
n_rows = len(images_dict[start:end])
for i, d in enumerate(images_dict[start:end]):
    ax1 = fig.add_subplot(n_rows, n_cols, n_cols * i + 1)
    ax2 = fig.add_subplot(n_rows, n_cols, n_cols * i + 2)
    ax3 = fig.add_subplot(n_rows, n_cols, n_cols * i + 3)
    ax1.imshow(d['filled'])
    ax1.set_title(d['name'])
    ax2.imshow(d['img'])
    ax3.imshow(d['overlay'])
我正在使用上面的代码绘制9幅图像,每行3幅(尽管第3行被截断):

现在,如果我想绘制20行,再次将
end
变量更改为
20
我得到:


我想知道当我增加打印的图像数量时,是否有办法防止matplotlib减小打印图像的大小?

作为第一个粗略估计,图形大小需要与每个方向上的图像数量成比例,也就是说,如果图像数量翻倍,则需要将图形大小翻倍

这当然只是部分正确,因为它没有考虑图像周围的边距

因此,解决方案类似于或

您需要根据每个子地块的大小加上边距和间距来计算地物大小。如果我们假设所有图像的大小相同,您可以简化此计算,只需指定图像的大小(以英寸为单位)(同时考虑纵横比)

end=3时

end=6时


您受到
figsize
的约束。使用更多的轴和相同的尺寸,您的图像将被迫更小以适应。
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["font.size"]=8

start, end = 0, 6
n_cols = 3
n_rows = (end-start)

lx,ly = 200,180
def get_image(i):
    return np.random.rayleigh((i+1.)/(end-start)/n_cols, size=(ly, lx, 3))


margin = 0.3 #inch
spacing =0.1 #inch
imsize = 0.6 #inch

figwidth=n_cols*imsize+(n_cols-1)*spacing+2*margin
figheight=n_rows*imsize*ly/lx+(n_rows-1)*spacing+2*margin

left=margin/figwidth
bottom = margin/figheight

fig, axes = plt.subplots(nrows=(end-start),ncols=n_cols, sharex=True, sharey=True)
fig.set_size_inches(figwidth,figheight)
fig.subplots_adjust(left=left, bottom=bottom, right=1.-left, top=1.-bottom, 
                    wspace=spacing/imsize, hspace=spacing/imsize*lx/ly)

for i, ax in enumerate(axes.flatten()):
    ax.imshow(get_image(i))

plt.savefig("imgrid{}.png".format(end))
plt.show()