2

I'm trying to recreate this:

enter image description here

My code is almost there:

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

Output:

enter image description here

I need the y axis grid lines like the original and have no idea how to get there. I've exhausted all my ideas. Help?

codeMagic
  • 44,549
  • 13
  • 77
  • 93

1 Answers1

7

You are looking for plt.grid. Since you only want the Y axis grid, though, you need to specify the axis keyword, so it would look like this:

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

See here for more info.

Vorticity
  • 4,582
  • 4
  • 32
  • 49
  • That got me the lines, but they aren't behind like in my example. http://i.imgur.com/U69O4su.png How could I accomplish this? I've tried [line.set_zorder(3) for line in ax.lines] without luck. – talentlesshack Dec 09 '15 at 14:49
  • Also tried adding zorder to your code. Tired all 3 without luck – talentlesshack Dec 09 '15 at 14:59
  • 1
    I've been trying to answer this, but I'm having problems accessing the matplotlib documentation at the moment and none of the solutions that I know of are working for me. Another thing you can try setting `ax.set_axisbelow(True)`. Neither appear to be working for me for some reason, but maybe you will have better luck. – Vorticity Dec 10 '15 at 00:08