2014-03-03 26 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