簡體   English   中英

消除matplotlib圖中子圖之間的空白

[英]Eliminate white space between subplots in a matplotlib figure

我正在嘗試創建一個不錯的圖,該圖連接一個4x4的子圖網格(使用gridspec放置,每個子圖為8x8 pixel)。 我一直在努力使地塊之間的距離與我要告訴它的做事相匹配。 我想這個問題是由於在圖的右側繪制了一個彩條,並調整了圖在圖中的位置以適應的。 但是,即使不包括顏色欄,該問題似乎也出現了,這讓我更加困惑。 它也可能與邊距間隔有關。 下面顯示的圖像由相關代碼生成。 如您所見,我正在嘗試使圖之間的間隔變為零,但似乎沒有用。 有人可以建議嗎?

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_aspect("equal")或數字縱橫比,或者通常使用imshow ),則即使將wspacehspace設置為0 為了消除圖形之間的空白,您可以看看以下問題

  1. 如何消除matplotlib中* images之間的間隙?
  2. 如何將gridspec與plt.subplots()結合使用以消除子圖行之間的空間
  3. 如何刪除matplotlib.pyplot中子圖之間的空間?

您可能首先考慮第一個問題的答案 ,解決方案是從單個數組中構建單個數組 ,然后使用pcolorpcolormeshimshow繪制該單個數組。 這使得以后添加顏色條特別舒適。

否則,請考慮設置圖形尺寸和子圖參數,以使不會殘留白粉。 在第二個問題的答案中可以找到用於該計算的公式。

帶有顏色欄的改編版本如下所示:

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM