Jame
Jame

Reputation: 3854

How to make the size of subplot equally

I am using matplotlib and GridSpec to plot 9 images in 3x3 subplots.

    fig = plt.figure(figsize=(30,40))
    fig.patch.set_facecolor('white')
    gs1 = gridspec.GridSpec(3,3)
    gs1.update(wspace=0.05, hspace=0.05)
    ax1 = plt.subplot(gs1[0])
    ax2 = plt.subplot(gs1[1])
    ax3 = plt.subplot(gs1[2])
    ax4 = plt.subplot(gs1[3])
    ax5 = plt.subplot(gs1[4])
    ax6 = plt.subplot(gs1[5])
    ax7 = plt.subplot(gs1[6])
    ax8 = plt.subplot(gs1[7])
    ax9 = plt.subplot(gs1[8])
    ax1.imshow(img1,cmap='gray')
    ax2.imshow(img2,cmap='gray')
    ...
    ax9.imshow(img9,cmap='gray')
          

However, the images have a different size from each row. For example, the first-row images size is 256x256, the images in the second row have a size of 200x200 and the third row has a size of 128x128

I want to plot the images in the subplot with same size. How should I use it in python?

This is an example of 4x3 subplot

enter image description here

Upvotes: 3

Views: 1110

Answers (2)

swatchai
swatchai

Reputation: 18812

Don't use matplotlib.gridspec, but use figure.add_subplot as demonstrated with the runnable code below. However, when doing some plotting, you need to set_autoscale_on(False) to suppress its behavior of size adjusting.

import numpy as np
import matplotlib.pyplot as plt

# a function that creates image array for `imshow()`
def make_img(h):
    return np.random.randint(16, size=(h,h)) 

fig = plt.figure(figsize=(8, 12))
columns = 3
rows = 4
axs = []

for i in range(columns*rows):
    axs.append( fig.add_subplot(rows, columns, i+1) )

    # axs[-1] is the new axes, write its title as `axs[number]`
    axs[-1].set_title("axs[%d]" % (i))

    # plot raster image on this axes
    plt.imshow(make_img(i+1), cmap='viridis', alpha=(i+1.)/(rows*columns))

    # maniputate axs[-1] here, plot something on it
    axs[-1].set_autoscale_on(False)   # suppress auto sizing
    axs[-1].plot(np.random.randint(2*(i+1), size=(i+1)), color="red", linewidth=2.5)


fig.subplots_adjust(wspace=0.3, hspace=0.4)
plt.show()

The resulting plot:

enter image description here

Upvotes: 3

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339755

I suppose you want to show the images in different sizes, such that all pixels of the different images are equally sized.

This is in general hard, but for the case where all images in a row (or column) of the subplot grid are of the same size, it becomes easy. The idea can be to use the gridspec's height_ratios (or width_ratios in case of columns) argument and set it to the image's pixel height (width).

import matplotlib.pyplot as plt
import numpy as np

images = [np.random.rand(r,r) for r in [25,20,12] for _ in range(3)]


r = [im.shape[0] for im in images[::3]]
fig, axes = plt.subplots(3,3, gridspec_kw=dict(height_ratios=r, hspace=0.3))

for ax, im in zip(axes.flat, images):
    ax.imshow(im)


plt.show()

enter image description here

Upvotes: 2

Related Questions