2011-08-22 48 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位置不是很右),所以随时调整位置,直到获得所需的效果。