2015-12-08 66 views
0

感謝您的時間:調整條形圖中的位置

我有一個Flask應用程序可以創建條形圖。

此功能:

def make_bar(g_title, y_title, x_labels, data_series, file_name, cat_title, 
      x_title): 
    rcParams['figure.figsize'] = 6.55, 3.8 
    n_groups = 13 
    bar_width = 0.35 
    opacity = 0.4 
    fig, ax = plt.subplots() 
    index = np.arange(n_groups) 
    error_config = {'ecolor': '0.3'} 
    plt.bar(index, tuple(data_series[0][1]), bar_width, 
        alpha=opacity, 
        color='b', 
        error_kw=error_config, 
        label='{}'.format(data_series[0][0])) 
    plt.bar(index + bar_width, tuple(data_series[1][1]), bar_width, 
        alpha=opacity, 
        color='r', 
        error_kw=error_config, 
        label='{}'.format(data_series[1][0])) 
    box = ax.get_position() 
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) 
    plt.xlabel(x_title, fontsize=10) 
    plt.ylabel(y_title, fontsize=10) 
    plt.title(g_title, fontsize=11) 
    plt.xticks(index + bar_width, tuple(x_labels), fontsize=8) 
    plt.yticks(fontsize=8) 
    plt.axis('tight') 
    lgd = plt.legend(fontsize=8, bbox_to_anchor=(1.15, 0.5)) 
    plt.tight_layout() 
    plt.savefig('{}/{}.png'.format(images, file_name), 
       dpi=100, format='png', bbox_extra_artists=(lgd,), 
       bbox_inches='tight') 
    return 

產生這樣的: okay

但我需要的東西,看起來像這樣:

better

我如何調整我的代碼,所以一切都坐並排?

感謝

+1

這裏有什麼問題?那個傳說在右邊失去了? – tom

+0

是的。我希望兩者都像圖2一樣處於同一水平面上。我嘗試過使用ax.set_position([box.x0,box.y0,box.width,box.height])進行調整,但我無法獲得right和bbox_extra_artists =(lgd,),bbox_inches ='tight'params,這打破了我的尺寸要求 –

+1

好的,'subplots_adjust'可能工作。看到我的回答 – tom

回答

1

要控制次要情節的位置,以使房間的傳說,你可以使用fig.subplots_adjust

fig.subplots_adjust(right=0.8) # adjust 0.8 for your needs. 
+0

這似乎已修復它。感謝您的幫助。 –