Search code examples
pythonmatplotlibgrouped-bar-chart

How to increase the bar width in a grouped bar plot without reducing the gap between groups


I found the following example for a grouped bar plot using matplotlib.pyplot, and have been able to successfully replicate the example which produces the following output:

enter image description here

The code for the above plot is:

# libraries
import numpy as np
import matplotlib.pyplot as plt
 
# set width of bar
barWidth = 0.25
 
# set height of bar
bars1 = [12, 30, 1, 8, 22]
bars2 = [28, 6, 16, 5, 10]
bars3 = [29, 3, 24, 25, 17]
 
# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
 
# Make the plot
plt.bar(r1, bars1, color='#7f6d5f', width=barWidth, edgecolor='white', label='var1')
plt.bar(r2, bars2, color='#557f2d', width=barWidth, edgecolor='white', label='var2')
plt.bar(r3, bars3, color='#2d7f5e', width=barWidth, edgecolor='white', label='var3')
 
# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], ['A', 'B', 'C', 'D', 'E'])
 
# Create legend & Show graphic
plt.legend()
plt.show()

I have then adapted this sample code to use my own data, and labels but found that I needed to increase the width of the bars. When I try to do this increasing:

barWidth = 0.5

I find that I loose the gap in between the grouped bars, and some of the bars end up stacked, as seen here:

enter image description here

I tried increasing the figure size but this did not fix the problem, it looked exactly the same, just bigger.

What is the correct way to increase the bar width and retain the correct bar grouping and distance between groups?


Solution

  • You may try reset r1:

    r1 = np.arange(0,len(bars1)*2,2)
    

    So the entire code is now:

    # libraries
    import numpy as np
    import matplotlib.pyplot as plt
    
    # set width of bar
    barWidth = 0.5
    
    # set height of bar
    bars1 = [12, 30, 1, 8, 22]
    bars2 = [28, 6, 16, 5, 10]
    bars3 = [29, 3, 24, 25, 17]
    
    # Set position of bar on X axis
    r1 = np.arange(0,len(bars1),1) #your original r1
    print (r1)
    r1 = np.arange(0,len(bars1)*2,2) #r1 was reset
    print (r1)
    r2 = [x + barWidth for x in r1]
    r3 = [x + barWidth for x in r2]
    
    # Make the plot
    plt.bar(r1, bars1, color='#7f6d5f', width=barWidth, edgecolor='white',    label='var1')
    plt.bar(r2, bars2, color='#557f2d', width=barWidth, edgecolor='white', label='var2')
    plt.bar(r3, bars3, color='#2d7f5e', width=barWidth, edgecolor='white', label='var3')
    
    
    # Add xticks on the middle of the group bars
    plt.xlabel('group', fontweight='bold')
    plt.xticks([r + barWidth for r in range(0,len(bars1)*2,2)], ['A', 'B', 'C', 'D', 'E'])
    
    # Create legend & Show graphic
    plt.legend()
    plt.show()
    

    Notice that the xticklabels have been reset accordingly too.

    The image of barwidth=0.25:

    enter image description here

    The image of barwidth=0.5:

    enter image description here

    You may freely change 2 to any positive number, just remember to change xticklabels accordingly.