消除matplotlib图中

时间:2017-03-17 05:57:18

标签: python matplotlib plot

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

Rectangle figure

同样,在制作没有彩条的方形图时:

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

Square figure

1 个答案:

答案 0 :(得分:3)

当轴纵横比设置为不自动调整时(例如,使用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?
  4. 您可以首先考虑this answer到第一个问题,其中解决方案是从单个数组中构建单个数组,然后使用pcolor绘制此单个数组,pcolormeshimshow。这使得以后添加颜色栏特别舒服。

    否则,请考虑设置figureize和subplot参数,以便不会留下白人。该计算的公式可在this answer中找到第二个问题。

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

    enter image description here