2013-07-28 108 views
0

我在A4紙上繪製了一些索引數據,並在3x2行中顯示了6張圖像。我有基本的代碼工作,它將這個數據幀數據繪製成以下6個圖。matplotlib中的精確子圖大小

http://imgur.com/frNjth3

def plot_idx(df,image,key): 
    # reset the default parameters 
    plt.rcParams['font.size'] = 8. 
    plt.rcParams['figure.figsize'] = (8.267, 11.692) #aA paper 
    fig = plt.figure() 
    #plot all the 6 figures and save the 3x2 image as png 

    ax1 = fig.add_subplot(321) # first row first image 
    #compute all time 
    alltime = df['Close'].count() 
    x,y,x_min, y_min, x_max, y_max = min_max(df,alltime) 
    ax1.plot(x, y,'r') 
    ax1.plot(x_min,y_min,'o') 
    ax1.plot(x_max,y_max,'o') 
    ax1.set_xlabel('year') 
    ax1.set_ylabel('Index Close') 
    ax1.set_title(plot_title[-1]) 
    ax1.fill_between(x,y,facecolor='red') 
    ax1.annotate(y_min, xy=(x_min,y_min), xytext=(x_min,y_min +250)) 
    ax1.annotate(y_max, xy=(x_max,y_max), xytext=(x_max,y_max +250)) 

    ax2 = fig.add_subplot(322) # first row second image 
    # compute ytd 
    ytd = df.ix[baseline_year:]['Close'].count() 
    x,y,x_min, y_min, x_max, y_max = min_max(df,ytd) 
    ax2.plot(x, y,'r') 
    .... 
    # repeat 4 more times for other figs 

    fig.suptitle(key, fontsize=10) 
    plt.tight_layout() 
    plt.savefig(image) 

如何獲得次要情節爲A4/6相同大小的地塊與空間頂部爲標題的一點點?如8.267/2 x 11.69/3尺寸? tight_layout有幫助,但我想更多地控制尺寸和位置。

+0

我的答案解決了您的問題嗎? – tacaswell

回答

0

如果你想要更多的控制,你可以使用fig.add_axesdoc

fig = plt.figure() 
ax1 = fig.add_axes([.1, .1, .4, .4,]) 
ax2 = fig.add_axes([.1, .5, .4, .4,]) 
# ... and so one for as many figures as you want 
# or wrap it all up in a loop 

的一面是這得到真煩不寫/維持,因爲你必須確保蜱和軸標籤不重疊與彼此。

還有GridSpec它可以讓你更多地控制跨越列等。

+0

矩形的值在子圖的上下文中意味着什麼?我可以看到l,b,w,h部分,但左邊,底部是什麼意思? – Sivaram

+0

'left'和'bottom'是圖形分數單元中軸的左下角的位置。 – tacaswell