2014-03-03 27 views
1

使用tight_layout(h_pad = -1)更改两个子图之间的垂直距离可更改总图形大小。我如何使用tight_layout定义图形大小?子图:tight_layout更改图大小

下面是代码:

#define figure 
pl.figure(figsize=(10, 6.25)) 

ax1=subplot(211) 
img=pl.imshow(np.random.random((10,50)), interpolation='none') 
ax1.set_xticklabels(()) #hides the tickslabels of the first plot 

subplot(212) 
x=linspace(0,50) 
pl.plot(x,x,'k-') 
xlim(ax1.get_xlim()) #same x-axis for both plots 

这里是结果:

如果我写

pl.tight_layout(h_pad=-2) 
在最后一行

,然后我得到这样的:

enter image description here

正如你所看到的,这个数字是大...

回答

1

可以使用GridSpec对象来精确控制宽度和高度的比例,作为回答on this threaddocumented here

与您的代码做实验,我可能会产生像你想要什么,用height_ratio的空间分配两次上的插曲,并增加了h_pad参数来调用tight_layout。这听起来并不完全正确,但也许你可以在此进一步调整......

import numpy as np 
from matplotlib.pyplot import * 
import matplotlib.pyplot as pl 
import matplotlib.gridspec as gridspec 

#define figure 
fig = pl.figure(figsize=(10, 6.25)) 

gs = gridspec.GridSpec(2, 1, height_ratios=[2,1]) 

ax1=subplot(gs[0]) 
img=pl.imshow(np.random.random((10,50)), interpolation='none') 
ax1.set_xticklabels(()) #hides the tickslabels of the first plot 

ax2=subplot(gs[1]) 
x=np.linspace(0,50) 
ax2.plot(x,x,'k-') 
xlim(ax1.get_xlim()) #same x-axis for both plots 
fig.tight_layout(h_pad=-5) 
show() 

还有其他的问题,比如纠正进口,增加numpy的,并密谋ax2,而不是直接与pl。我看到的输出是这样的:

Corrected figure

+0

我想这就是答案,但unfortunatley fig.tight_layout仍然改变数字大小。您可以通过打开和关闭fig.tight_laout来测试它:fig = pl.figure(figsize =(10,6.25)) pl.imshow(np.random.random((10,50)),interpolation ='none' ) #fig.tight_layout(h_pad = 0) – FrankTheTank