2015-12-08 103 views
1

我試圖重新創建:顯示Y軸網格線在地塊

enter image description here

我的代碼幾乎有:

def make_bar(g_title, y_title, x_labels, data_series, file_name, cat_title, 
      x_title): 
    rcParams['figure.figsize'] = 6.4125, 3.6 
    n_groups = 13 
    bar_width = 0.35 
    opacity = 0.4 
    fig, ax = plt.subplots() 
    fig.subplots_adjust(right=1.5) # adjust 0.8 for your needs. 
    index = np.arange(n_groups) 
    plt.bar(index, tuple(data_series[0][1]), bar_width, 
        alpha=opacity, 
        color='b', 
        label='{}'.format(data_series[0][0])) 
    plt.bar(index + bar_width, tuple(data_series[1][1]), bar_width, 
        alpha=opacity, 
        color='r', 
        label='{}'.format(data_series[1][0])) 
    box = ax.get_position() 
    ax.set_position([box.x0, box.y0, box.width * 0.6, 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') 
    plt.close('all') 
    return 

輸出:

enter image description here

我需要像原始的y軸網格線,不知道如何獲得 那裏。我用盡了所有的想法。幫幫我?

+2

http://stackoverflow.com/questions/16074392/getting -vertical-gridlines -to-appear-in-line-plot-in-matplotlib – jonchar

+0

看起來像vertical == horizo​​ntal現在:\ – talentlesshack

回答

3

您正在尋找plt.grid。由於您只想Y軸格,不過,你需要指定axis關鍵字,所以它看起來像這樣:

plt.grid(color='black', which='major', axis='y', linestyle='solid') 

See here for more info.

+0

這讓我覺得很棒,但它們並沒有像我的例子那樣落後。 http://i.imgur.com/U69O4su.png我怎麼能做到這一點?我已經嘗試[line.set_zorder(3)for line.line]沒有運氣。 – talentlesshack

+0

也嘗試添加zorder到您的代碼。累了所有3沒有運氣 – talentlesshack

+0

我一直在試圖回答這個問題,但我目前在訪問matplotlib文檔時遇到問題,沒有任何我知道的解決方案正在爲我工​​作。另一件你可以嘗試設置'ax.set_axisbelow(True)'。由於某種原因,我似乎沒有爲我工作,但也許你會有更好的運氣。 – Vorticity