Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid overlap between bars in a multi-bar chart in matplotlib

I am trying to plot a multiple bar chart with variable x-values. My plot looks like this

enter image description here

As you can see the bars from StartFFT_Gflops overlap the next tick on the x axis. Is there some way I can increase the spacing between the ticks so that my bars don't overlap?

I am using the following code to plot

def plot_bars(hpcc_data, datapoints):

    fig, ax = plt.subplots()

    ind = np.arange(len(datapoints)) # array of x-ticks = no of datapoints to plot
    offset = 0 # offset distance between bars
    bar_legends = list()
    bar_obj = list()
    width = 0.35
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] # hopefully we won't need more than this
    color_idx = 0 

    for host in hpcc_data:
        host_data = hpcc_data[host]
        vals = list()
        for dp in datapoints:
            vals.append(host_data[dp])

        bar = ax.bar(ind + offset, vals, width, color=colors[color_idx])
        bar_legends.append(host)
        bar_obj.append(bar[0])
        color_idx += 1

        offset += width #for the next host



    ax.legend(bar_obj, bar_legends)
    ax.set_ylabel('Gflops')
    ax.set_title('Scores of hpcc benchmark')
    ax.set_xticks(ind + (len(hpcc_data.keys())/2)*width) #approx center the xticks
    ax.set_xticklabels(datapoints)

    plt.show()
like image 845
syed Avatar asked Jul 15 '26 11:07

syed


1 Answers

As @cphlewis pointed out, the width between x-ticks is one data-value apart. Dynamically generating the width helps to solve the overlap problem. Instead of having a static width = 0.35 I am now generating it using

width = 1.0/(num_items + 2)

Here is the final chart

uncrowded multi-bar chart

like image 121
syed Avatar answered Jul 23 '26 16:07

syed