1

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?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user2109254
  • 1,709
  • 2
  • 30
  • 49

1 Answers1

1

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.

tianlinhe
  • 991
  • 1
  • 6
  • 15
  • thanks for the response. When I run you code, I see no visual difference from the original. I am trying to get wider bars, but keep the grouping and the distance between the groups. – user2109254 Apr 13 '20 at 08:46
  • I just updated my answer, I think the reason of no difference lies in the scaling: because the new r1 and new barwidth are both twice of the original, so the enlargement effect is exactly cancelled out . Try a number larger than 2 (e.g.`r1 = np.arange(0,len(bars1)*3,3)` , I think it will work! – tianlinhe Apr 13 '20 at 08:50