Python 消除matplotlib图形中子地块之间的空白

Python 消除matplotlib图形中子地块之间的空白,python,matplotlib,plot,Python,Matplotlib,Plot,我正在尝试创建一个漂亮的绘图,它连接一个4x4网格的子地块(与gridspec一起放置,每个子地块为8x8像素)。我一直在努力使情节之间的间隔与我试图告诉它的内容相匹配。我想问题是在图的右侧绘制一个颜色条,然后调整图中的绘图位置以适应。然而,即使没有包含颜色栏,这个问题似乎也会出现,这让我更加困惑。这也可能与页边间距有关。下面显示的图像是由相关代码生成的。正如你所看到的,我试图使两个图之间的空间为零,但它似乎不起作用。有人能提供建议吗 fig = plt.figure('W Heat Map',

我正在尝试创建一个漂亮的绘图,它连接一个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")

当轴纵横比设置为不自动调整时(例如,使用
set\u aspect(“equal”)
或数字纵横比,或通常使用
imshow
),即使
wspace
hspace
设置为
0
,子批次之间也可能存在一些空白。为了消除数字之间的空白,您可以查看以下问题

  • 5月1日,考虑第一个问题,解决方案是<强>从单个数组中创建单个数组< /强>,然后使用“代码> Po色色镜< /COD>”、“代码> PCORMOSFET< /CODE >或<代码> IMSID绘制这个单行数组。这使得以后添加颜色条特别舒服

    否则,请考虑设置图大小和子图参数,这样就不会出现空白。计算公式见第二个问题的附件

    带有colorbar的修改版本如下所示:

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