2015-12-08 105 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

这似乎已修复它。感谢您的帮助。 –