随着绘制的图像数量增加,防止matplotlib减小绘制图像的大小 [英] Preventing matplotlib from decrease the size of plotted images as number of images plotted increases

查看:64
本文介绍了随着绘制的图像数量增加,防止matplotlib减小绘制图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 开始,结束= 0、3无花果= plt.figure(figsize =(25,25))n_cols = 3n_rows = len(images_dict[start:end])对于 i, d 在 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['填充'])ax1.set_title(d['name'])ax2.imshow(d ['img'])ax3.imshow(d['overlay'])

我正在绘制 9 个图像,每行 3 个,使用上面我得到的代码(尽管第三行被切断了):

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

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

解决方案

作为第一个粗略估计,图形尺寸必须与沿每个方向的图像数量成正比,即,如果将图像数量加倍,则需要加倍图大小.

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

解决方案因此类似于

使用 end = 6 :

I'm plotting some images using the code below:

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'])

I'm plotting 9 images, 3 per row, using the code above I get (though the 3rd row is cut off):

Now if I instead want to plot 20 rows, again just changing the end variable to 20 I get:

I was wondering if there is a way to prevent matplotlib from decreasing the size of the plotted image when I increase the number of images that are plotted?

解决方案

As a first rough estimate the figure size needs to be proportional to the number of images along each direction, i.e. if you double the number of images you need to double the figure size.

This is of course only partially correct, because it does not take into account the margins around the images.

A solution is hence similar to Python: Showing multiple images in one figure WITHOUT scaling or How to combine gridspec with plt.subplots() to eliminate space between rows of subplots

You need to calculate the figure size in terms of the size of each subplot plus the margins and spacing. If we assume that all images are of the same size you may simplify this calculation and just specify how large an image should be in inches (taking into account the aspect ratio as well).

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()

With end=3:

With end=6:

这篇关于随着绘制的图像数量增加,防止matplotlib减小绘制图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆