2015-02-09 35 views
1

如何將圖像置於GridSpec分隔框中?我已經得到了下面的圖片,我想讓上面的圖片居中。如何在GridSpec中對齊框架?

Picture to be centered

相關的代碼(它的一部分,尤其是單位,是從AMUSE框架):

lim = [-5,5] | units.kpc 
bins = [100,100] 
xyrange = [[-5,5],[-5,5]] 
cmap = cm.jet 

hist2d_xy,_,_ = np.histogram2d(particles.x.value_in(units.kpc),particles.y.value_in(units.kpc),bins=bins, range=xyrange) 
hist2d_xz,_,_ = np.histogram2d(particles.x.value_in(units.kpc),particles.z.value_in(units.kpc),bins=bins, range=xyrange) 

maximum_value = max([hist2d_xy.max(),hist2d_xz.max()]) 

gs = gridspec.GridSpec(2,1, height_ratios=[3,1]) 
ax1 = pyplot.subplot(gs[0]) 


pyplot.imshow(np.flipud(hist2d_xy.T),cmap=cmap,extent = np.array(xyrange).flatten(), interpolation='none',norm=colors.LogNorm(vmin=1,vmax=maximum_value)) 
cbar = pyplot.colorbar() 

ax2 = pyplot.subplot(gs[1]) 
pyplot.imshow(np.flipud(hist2d_xz.T),cmap=cmap,extent = np.array(xyrange).flatten(), interpolation='none',norm=colors.LogNorm(vmin=1,vmax=maximum_value)) 
pyplot.ylim([-1,1]) 
cbar = pyplot.colorbar() 
pyplot.tight_layout() 
pyplot.savefig(file_location_for_pictures+'test_%.2f.png' %threshold) 
pyplot.close() 
+0

做一個3x2的網格,有一個底部跨度所有三個最高的一個只使用中間。 – tacaswell 2015-02-09 14:02:00

+0

http://matplotlib.org/users/gridspec.html – tacaswell 2015-02-09 14:02:42

+0

我使用'subplot2grid()',但上面的圖片太小了。但感謝鏈接,'subplot2grid()'似乎非常方便。 – Mathias711 2015-02-09 14:27:59

回答

0

我取代

gs = gridspec.GridSpec(2,1, height_ratios=[3,1]) 
ax1 = pyplot.subplot(gs[0]) 

ax2 = pyplot.subplot(gs[1]) 

ax1 = pyplot.subplot2grid((6,6), (0,1), colspan = 4, rowspan = 4) 

ax2 = pyplot.subplot2grid((6,6), (4,0), colspan = 6, rowspan = 2) 

(在正確的地方),並且它現在生產這個畫面(編輯顏色並增加了一些文字,但這個想法是清楚的):

Centered upper picture