Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

python - Preventing matplotlib from decrease the size of plotted images as number of images plotted increases

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

enter image description here

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

enter image description here

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here

With end=6:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...