Lars Kakavandi-Nielsen
Lars Kakavandi-Nielsen

Reputation: 2198

Matplotlib bar plot, bars is on top of each other, how to create space

I have this function for plotting a bar plot for some latency values:

import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt 


def plot_bar(g):
    auto = [1.36, 5.34, 10.2, 16.48, 24.3, 45.6, 83.89, 155.19, 289.68, 598.85]
    four = [1.81, 5.57, 11.48, 18, 27.69, 47.72, 89.11, 164.74, 315.24, 637.89]
    eight = [1.44, 5.45, 8.56, 16.64, 26.85, 43.44, 82.41, 152.32, 294.11, 598.57]
    sixteen = [2.29, 5.79, 19.99, 18.44, 33.73, 75.31, 177.74, 365.39, 774.57, 1619.99]
    thirtytwo = [3.62, 13.84, 25.39, 42.21, 80.14, 150.41, 311.46, 645.37, 1330.94, 2688.48]   

    N = 10
    fig, ax = plt.subplots()

    ind = np.arange(N)    # the x locations for the groups
    width = 0.30         # the width of the bars
    p1 = ax.bar(ind, auto, width, color='r')
    p2 = ax.bar(ind+width, four, width, color='y')
    p3 = ax.bar(ind+width+width, eight, width, color='b')
    p4 = ax.bar(ind+width+width+width, sixteen, width, color='k')
    p5 = ax.bar(ind+width+width+width+width, thirtytwo, width, color='g')



    #ax.set_title('Scores by group and gender')
    ax.set_xticks(ind * (5 * width))
    ax.set_xticklabels(('1MB', '4MB', '8MB', '16MB', '32MB', '64MB', '128MB', '256MB', '512MB', '1GB'))

    plt.xticks(rotation=75)

    ax.legend((p1[0], p2[0], p3[0], p4[0], p5[0]), ('Automatic t=8', 't=4','t=8', 't=16', 't=32'))
    ax.autoscale_view()
    plt.ylabel('time (ms)')
    plt.xlabel('Data Size')
    plt.yscale("log", nonposy='clip')
    plt.tight_layout()
    fig.savefig('./graphs/nope_{!s}.eps'.format(g))

and the result is this: enter image description here

where the bars are overlapping with each other which I would like to avoid. I have tried with figure size but no luck. I also tried changing set_xticks also to know avail, and I am out of ideas of how to fix this.

The code presented should work, please advice.

Upvotes: 0

Views: 1939

Answers (2)

Sheldore
Sheldore

Reputation: 39052

Another alternate solution is to use a widely separated x-positions for the center bar. The problem with your plot was that your bar width was 0.3 and you had 5 bars, so 5*0.3 = 1.5 total x-spacing per group. And since the spacing between your x-positions for centering bars was 1, you had an overlap of 0.5 between each group of bars.

To avoid this, you can use a spacing of 2 between your x-indices for centering bars using the following. I also noticed that your were not using all the x-ticklabels properly. Add the following two lines to make things look good.

ind = np.arange(0,2*N, 2)    # the x locations for the groups
ax.set_xticks(ind + 2*width)

enter image description here

Upvotes: 3

Scott Boston
Scott Boston

Reputation: 153460

Basically, your bar widths are to wide. The xticks are 1 unit wide, you are trying to plot 5 bars of width .3 which is more than 1 unit hence overlapping. Reduce your width to .2 for 5 bars.

width = 0.20         # the width of the bars

Output:

enter image description here

Upvotes: 1

Related Questions