【问题标题】:changing size of a plot in a subplot figure更改子图中的绘图大小
【发布时间】:2011-08-23 04:58:53
【问题描述】:

我创建了一个包含 4 个子图 (2 x 2) 的图形,其中 3 个是 imshow 类型,另一个是 errorbar。每个imshow 图的右侧还有一个颜色条。我想调整我的第三个图的大小,图形的区域将正好在它上方的区域之下(没有颜色条)

作为例子(这是我现在拥有的):

如何调整第三个图的大小?

问候

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    要调整坐标区实例的尺寸,您需要使用set_position() 方法。这也适用于 subplotAxes。要获取轴的当前位置/尺寸,请使用 get_position() 方法,该方法返回一个 Bbox 实例。对我来说,与位置交互在概念上更容易,即 [left,bottom,right,top] 限制。要从 Bbox 访问此信息,请使用 bounds 属性。

    在这里,我将这些方法应用于与您上面的示例类似的东西:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x,y = np.random.rand(2,10)
    img = np.random.rand(10,10)
    
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    im = ax1.imshow(img,extent=[0,1,0,1])
    
    plt.colorbar(im)
    ax2 = fig.add_subplot(222)
    im = ax2.imshow(img,extent=[0,1,0,1])
    plt.colorbar(im)
    
    ax3 = fig.add_subplot(223)
    ax3.plot(x,y)
    ax3.axis([0,1,0,1])
    
    ax4 = fig.add_subplot(224)
    im = ax4.imshow(img,extent=[0,1,0,1])
    plt.colorbar(im)
    
    pos4 = ax4.get_position().bounds
    pos1 = ax1.get_position().bounds
    # set the x limits (left and right) to first axes limits
    # set the y limits (bottom and top) to the last axes limits
    newpos = [pos1[0],pos4[1],pos1[2],pos4[3]]
    
    ax3.set_position(newpos)
    
    plt.show()
    

    您可能会觉得这两个图看起来并不完全相同(在我的渲染中,left 或 xmin 的位置不太正确),因此请随意调整位置,直到获得所需的效果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-17
      • 2017-04-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 2014-10-14
      相关资源
      最近更新 更多