【问题标题】:Eliminate white space between subplots in a matplotlib figure消除 matplotlib 图中子图之间的空白
【发布时间】:2017-03-17 16:22:22
【问题描述】:

我正在尝试创建一个漂亮的图,它加入一个 4x4 网格的子图(使用 gridspec 放置,每个子图是 8x8 像素)。我一直在努力让情节之间的间距与我试图告诉它做的事情相匹配。我想问题是由于在图的右侧绘制了一个颜色条,并调整了图中图的位置以适应。但是,即使没有包含彩条,这个问题似乎也会出现,这让我更加困惑。它也可能与边距有关。下面显示的图像由相关代码生成。正如你所看到的,我试图让地块之间的空间变为零,但它似乎不起作用。谁能给点建议?

fig = plt.figure('W Heat Map', (18., 15.))
gs = gridspec.GridSpec(4,4)
gs.update(wspace=0., hspace=0.)
for index in indices:
    loc = (i,j) #determined by the code
    ax = plt.subplot(gs[loc])
    c = ax.pcolor(physHeatArr[index,:,:], vmin=0, vmax=1500)
    # take off axes 
    ax.axis('off')
    ax.set_aspect('equal')
fig.subplots_adjust(right=0.8,top=0.9,bottom=0.1)
cbar_ax = heatFig.add_axes([0.85, 0.15, 0.05, 0.7])
cbar = heatFig.colorbar(c, cax=cbar_ax)
cbar_ax.tick_params(labelsize=16)
fig.savefig("heatMap.jpg")

类似地,在没有颜色条的情况下制作正方形:

fig = plt.figure('W Heat Map', (15., 15.))
gs = gridspec.GridSpec(4,4)
gs.update(wspace=0., hspace=0.)
for index in indices:
    loc = (i,j) #determined by the code
    ax = plt.subplot(gs[loc])
    c = ax.pcolor(physHeatArr[index,:,:], vmin=0, vmax=400, cmap=plt.get_cmap("Reds_r"))
    # take off axes 
    ax.axis('off')
    ax.set_aspect('equal')
fig.savefig("heatMap.jpg")

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    当轴纵横比设置为不自动调整时(例如,使用set_aspect("equal") 或数字纵横比,或者通常使用imshow),子图之间可能会有一些空白,即使wspacehspace 设置为 0。为了消除数字之间的空白,您可以看看以下问题

    1. How to remove gaps between *images* in matplotlib?
    2. How to combine gridspec with plt.subplots() to eliminate space between rows of subplots
    3. How to remove the space between subplots in matplotlib.pyplot?

    您可以首先考虑this answer 第一个问题,解决方案是从单个数组中构建单个数组,然后使用pcolorpcolormesh 绘制这个单个数组或imshow。这使得以后添加颜色条特别方便。

    否则,请考虑设置图形大小和子图参数,以便不会保留任何空白。计算公式见第二个问题的this answer

    带有颜色条的改编版本如下所示:

    import matplotlib.pyplot as plt
    import matplotlib.colors
    import matplotlib.cm
    import numpy as np
    
    image = np.random.rand(16,8,8)
    aspect = 1.
    n = 4 # number of rows
    m = 4 # numberof columns
    bottom = 0.1; left=0.05
    top=1.-bottom; right = 1.-0.18
    fisasp = (1-bottom-(1-top))/float( 1-left-(1-right) )
    #widthspace, relative to subplot size
    wspace=0  # set to zero for no spacing
    hspace=wspace/float(aspect)
    #fix the figure height
    figheight= 4 # inch
    figwidth = (m + (m-1)*wspace)/float((n+(n-1)*hspace)*aspect)*figheight*fisasp
    
    fig, axes = plt.subplots(nrows=n, ncols=m, figsize=(figwidth, figheight))
    plt.subplots_adjust(top=top, bottom=bottom, left=left, right=right, 
                        wspace=wspace, hspace=hspace)
    #use a normalization to make sure the colormapping is the same for all subplots
    norm=matplotlib.colors.Normalize(vmin=0, vmax=1 )
    for i, ax in enumerate(axes.flatten()):
        ax.imshow(image[i, :,:], cmap = "RdBu", norm=norm)
        ax.axis('off')
    # use a scalarmappable derived from the norm instance to create colorbar
    sm = matplotlib.cm.ScalarMappable(cmap="RdBu", norm=norm)
    sm.set_array([])
    cax = fig.add_axes([right+0.035, bottom, 0.035, top-bottom])
    fig.colorbar(sm, cax=cax)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2023-02-09
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 2015-04-09
      • 2022-06-10
      • 2022-09-28
      • 1970-01-01
      相关资源
      最近更新 更多