2011-08-22 21 views
1

我創建了一個包含4個子圖(2 x 2)的圖,其中3個是imshow類型,另一個是errorbar。每個imshow地塊在其右側還有一個彩條。我想調整我的第三個情節,該圖形的面積會完全它上面的一個下(有出彩條)改變小區圖中的地塊大小

爲例(這是我現在有):

example

我怎樣才能調整第三個陰謀?

問候

回答

3

要調整軸實例的尺寸,你需要使用set_position()方法。這也適用於subplotAxes。要獲取軸的當前位置/尺寸,請使用返回Bbox實例的get_position()方法。對我而言,與位置相互作用在概念上更容易,即[左,右,右,上]限制。要從Bbox訪問此信息,請輸入bounds屬性。

這裏我應用這些方法類似於你上面的例子東西:

import matplotlib.pyplot as plt 
import numpy as np 

x,y = np.random.rand(2,10) 
img = np.random.rand(10,10) 

fig = plt.figure() 
ax1 = fig.add_subplot(221) 
im = ax1.imshow(img,extent=[0,1,0,1]) 

plt.colorbar(im) 
ax2 = fig.add_subplot(222) 
im = ax2.imshow(img,extent=[0,1,0,1]) 
plt.colorbar(im) 

ax3 = fig.add_subplot(223) 
ax3.plot(x,y) 
ax3.axis([0,1,0,1]) 

ax4 = fig.add_subplot(224) 
im = ax4.imshow(img,extent=[0,1,0,1]) 
plt.colorbar(im) 

pos4 = ax4.get_position().bounds 
pos1 = ax1.get_position().bounds 
# set the x limits (left and right) to first axes limits 
# set the y limits (bottom and top) to the last axes limits 
newpos = [pos1[0],pos4[1],pos1[2],pos4[3]] 

ax3.set_position(newpos) 

plt.show() 

你可能會覺得這兩個地塊不完全看起來是一樣的(在我的渲染,向左或XMIN位置不是很右),所以隨時調整位置,直到獲得所需的效果。