2017-03-17 6 views
2

にサブプロット間の空白を排除(gridspecで配置し、各サブプロットは、8×8ピクセルです)。私は、プロット間の間隔を、私がそれをするように指示しようとしているものと一致するように、常に苦労しています。私は、図の右側にカラーバーをプロットし、図の中のプロットの位置を調整することによって問題が発生していると思います。しかし、この問題はカラーバーが含まれていなくても収穫されているようで、それは私をさらに混乱させました。また、マージンの間隔に関係する場合もあります。以下に示すイメージは、関連するコードによって生成されます。あなたが見ることができるように、私はプロット間のスペースを0にしようとしていますが、動作していないようです。誰も助言することはできますか?私はサブプロットの4×4のグリッド合流素敵なプロット作成しようとしていますmatplotlibのフィギュア

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

答えて

3

(例えば、使用set_aspect("equal")または数値の態様は、OR)imshowを用いて、一般的に、SUとの間のいくつかの空白があるかもしれませんbplotsは、たとえwspacehspace0に設定されています。数字の間に空白を解消するためには、次の質問

  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. を見ているかもしれ
  4. How to remove the space between subplots in matplotlib.pyplot?

あなたはまず最初の質問にthis answerを考慮することができます溶液は、個々のアレイのうちの単一のアレイを構築した後pcolor、を使用して、この単一のアレイをプロットでありますまたはimshowです。これにより、後でカラーバーを追加するのが特に快適になります。

そうでなければ何のwhitespaeが残っていないということなfiguresizeやサブプロットのパラメータを設定することを検討してください。計算の式は、this answerの2番目の質問にあります。

Anは次のようになりますカラーバーとバージョンを適応:

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

関連する問題