简体   繁体   English

如何使子图的大小相等?

[英]How to make the size of subplot equally?

I am using matplotlib and GridSpec to plot 9 images in 3x3 subplots.我正在使用 matplotlib 和 GridSpec 在 3x3 子图中绘制 9 个图像。

    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例如,第一行图片大小为256x256,第二行图片大小为200x200,第三行图片大小为128x128

I want to plot the images in the subplot with same size.我想在子图中绘制相同大小的图像。 How should I use it in python?我应该如何在python中使用它? Thanks谢谢

This is an example of 4x3 subplot这是 4x3 子图的示例在此处输入图片说明

Don't use matplotlib.gridspec , but use figure.add_subplot as demonstrated with the runnable code below.不要使用matplotlib.gridspec ,而是使用figure.add_subplot如下面的可运行代码所示。 However, when doing some plotting, you need to set_autoscale_on(False) to suppress its behavior of size adjusting.但是,在进行一些绘图时,您需要set_autoscale_on(False)以抑制其大小调整行为。

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:结果图:

在此处输入图片说明

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).这个想法可以是使用 gridspec 的height_ratios (或width_ratios在列的情况下)参数并将其设置为图像的像素高度(宽度)。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM