2016-12-07 68 views
2

我試圖並排繪製相同圖像的兩個版本。當我繪製而不考慮的一個圖像的顏色欄中的人物,似乎有正確的尺寸:如何在使用彩條時保持圖像大小?

without the color bar

但是當我添加顏色條在左邊的圖像,它縮放圖像不知何故下來:

scales the image down

這裏就是我註釋掉了彩條線路代碼:

def plot_amaps(self, anisotropy_map, parallel): 
     timepoint = self.t * self.timestep 
     amap_directory = self.directory + "amaps/" 
     fig = plt.figure(facecolor='w', dpi=180) 

     ax1 = fig.add_subplot(121) 
     fig.subplots_adjust(top=0.85) 
     ax1.grid(False) 
     txt = "Mean(r) = %.3f SD(r)= %.3f t=%dmin" 
     txt = txt %(self.mean, self.sd, timepoint) 
     ax1.set_title(txt) 

     amap = ax1.imshow(anisotropy_map, cmap="jet", clim = self.clim) 
     #divider = make_axes_locatable(ax1) 
     #cax = divider.append_axes('right', size='5%', pad=0.05) 
     #fig.colorbar(amap, cax=cax) 

     ax2 = fig.add_subplot(122) 
     ax2.set_title("Intensity image", fontsize=10) 
     ax2.imshow(parallel, cmap="gray") 
     ax2.grid(False) 
     ax1.axis('off') 
     ax2.axis('off') 

     if self.save is True: 
      self.make_plot_dir(amap_directory) 
      name = self.cell + "_time_"+str(timepoint) 
      plt.savefig(amap_directory+name+self.saveformat, bbox_inches='tight') 
     else: 
      plt.show() 
     plt.close('all') 

我做錯了什麼,以及如何確保兩張圖像尺寸相同?

回答

1

當使用

divider = make_axes_locatable(ax1) 
cax = divider.append_axes('right', size='5%', pad=0.05) 

你明確的要求更小的5%軸。所以如果你不想這樣做,你不應該使用make_axes_locatable來創建彩條的軸。

相反,可以簡單地在任意點上使用

cax = fig.add_axes([left, bottom, width, height]) 

left, bottom, width, height其中在圖單位範圍從0到1然後,彩條添加到它的圖中創建一個軸。
如果你想在中間的彩條,你可以做以前使用

plt.subplots_ajust(wspace=0.3) 

當然,你將不得不做一些試驗用的數字一定的空間。

1

當您使用append_axes()時,它實際上會減小尺寸ax1以爲色彩空間騰出空間。 如果你想確保你的座標軸的大小不變,你應該明確地創建它們。 這裏是我的嘗試:

import matplotlib.gridspec as gridspec 
gs = gridspec.GridSpec(1,3,width_ratios=[5,1,5]) 
fig = plt.figure(facecolor='w', dpi=180) 

randomData = np.random.random(size=(100,100)) 

ax1 = fig.add_subplot(gs[0]) 
ax1.grid(False) 
txt = "Mean(r) = %.3f SD(r)= %.3f t=%dmin" 
txt = txt %(0, 0, 0) 
ax1.set_title(txt) 

amap = ax1.imshow(randomData, cmap="jet") 
#divider = make_axes_locatable(ax1) 
#cax = divider.append_axes('right', size='5%', pad=0.05) 
fig.colorbar(amap, cax=fig.add_subplot(gs[1])) 

ax2 = fig.add_subplot(gs[2]) 
ax2.set_title("Intensity image", fontsize=10) 
ax2.imshow(randomData, cmap="gray") 
ax2.grid(False) 
ax1.axis('off') 
ax2.axis('off') 

enter image description here

+0

愚蠢的問題:我如何能減少顏色條的大小呢? 'fig.colorbar(amap,cax = fig.add_subplot(gs [1]),shrink = .5)'返回一個錯誤。 – pskeshu

+0

您是指垂直尺寸還是水平尺寸。對於水平尺寸,您可以在創建GridSpec時更改'width_ratios'。我做了[5,1,5]'這意味着圖像是彩色條大小的5倍,但您可以根據自己的需要進行調整。對於垂直,我不確定,我看到我的解決方案提供了比您想要的輸出更大的規模。在創建軸時可能會傳遞一些填充選項,或者通過@ImportanceOfBeingErnest查看答案,您可以直接指定軸的大小。 –

相關問題