Matplotlib:如何实现子图相同的大小(高度)?

4

我有一个包含三个子图的图像。第一个子图是一张图片(imshow),而其他两个子图是分布图(plot)。

以下是代码:

# collects data
imgdata = ...      # img of shape  (800, 1600, 3)    
x = ...            # one 1600-dimensional vector
y = ...            # one  800-dimensional vector

# create the figure 
f = plt.figure()    

# create subplots   
subplot_dim = (1, 3)
p_img = plt.subplot2grid(subplot_dim, (0, 0), aspect="auto")
p_x = plt.subplot2grid(subplot_dim, (0, 1), aspect="auto")
p_y = plt.subplot2grid(subplot_dim, (0, 2), aspect="auto")

p_img.imshow(imgdata, interpolation="None")
p_x.plot(x)         
p_y.plot(y)         


# save figure       
f.set_size_inches(21.0, 12.0)
f.savefig("some/path/image.pdf", dpi=80)

我的问题是,两个子图p_xp_y的高度始终大于图像子图p_img

因此,结果总是看起来像这样:

                  ###############   ###############   
                  #             #   #  *****      #
                  #        *****#   # *     *     #
###############   #     ***     #   # *      *    #
#             #   #    *        #   # *        *  #
#    image    #   #   *         #   #*           *#
#             #   #***          #   #*           *#
###############   ###############   ###############
     p_img              p_x               p_y

我该如何强制使、和的大小(至少是高度)相等?
编辑:这里有一个简单的示例代码,可生成随机数据并使用plt.show()而不是保存图像。然而,人们很容易看到同样的行为:图像比其他子图小得多(高度)。
from matplotlib import pyplot as plt 
from matplotlib import image as mpimg
import numpy as np

imgdata = np.random.rand(200, 400, 3)  
x = np.random.normal(loc=100.0, scale=20.0, size=400)
y = np.random.normal(loc=150.0, scale=15.0, size=200) 

# create the figure
f = plt.figure()

# create subplots
subplot_dim = (1, 3)
p_img = plt.subplot2grid(subplot_dim, (0, 0), aspect="auto")
p_x = plt.subplot2grid(subplot_dim, (0, 1), aspect="auto")
p_y = plt.subplot2grid(subplot_dim, (0, 2), aspect="auto")

p_img.imshow(imgdata, interpolation="None")
p_x.plot(x)
p_y.plot(y)


# save figure
plt.show()

尝试使用 plt.axis() 函数来设置坐标轴的尺寸。 - SvbZ3r0
那没有改变任何事情:( 另外,axis() 不是接受一个列表 [xmin,xmax,ymin,ymax] 吗? p_imgp_xp_y 有一个(成对)不同的 y 轴。 p_img(图像的高度)为 800,而 p_xp_yy_max 通常在区间 [1,10] 中。因此它们永远不共享 y 轴(缩放)。 - daniel451
gridspec是什么?请查看这个问题,看看它是否有帮助。 - SvbZ3r0
我已经使用“gridspec”测试了我的示例代码(使用“gridspec”创建轴而不是“subplot2grid”,并将“height_ratios”添加到gridspec)。这似乎强制执行特定的宽度/高度。但是,我不理解缩放,似乎值是绝对的?Matplotlib没有一些方法使子图的高度固定吗?这看起来像一个巨大的设计缺陷...难道每个子图的宽度/高度都必须手动确定,然后重新调整大小才能获得相等大小的子图吗? - daniel451
1个回答

1
您可以直接在子图中指定它,方法如下:
from matplotlib import pyplot as plt
from matplotlib import image as mpimg
import numpy as np

imgdata = np.random.rand(200, 400, 3)
x = np.random.normal(loc=100.0, scale=20.0, size=400)
y = np.random.normal(loc=150.0, scale=15.0, size=200)

# create the figure
f = plt.figure()

# create subplots
subplot_dim = (1, 3)
p_img = plt.subplot2grid(subplot_dim, (0, 0), aspect="auto")
p_x = plt.subplot2grid(subplot_dim, (0, 1), aspect="auto", adjustable='box-forced', sharex=p_img, sharey=p_img)
p_y = plt.subplot2grid(subplot_dim, (0, 2), aspect="auto", adjustable='box-forced', sharex=p_img, sharey=p_img)

p_img.imshow(imgdata, interpolation="None")
p_x.plot(x)
p_y.plot(y)

问题在于您的数据没有相同的限制。因此,您需要使用set_xlimset_ylim进行调整。我还建议尝试其他sharey的组合,因为它们可能会提供更好的结果。

Same height for all matplotlib subplots


Matplotlib 3.4 给出以下错误:ValueError: 'box-forced' 不是可调整的有效值;支持的值为 'box','datalim' - Greenonline

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接