A grouped barplot is used when you have several groups, and subgroups into these groups. Here is a method to make them using the matplotlib library.
Note that you can easily turn it as a stacked area barplot, where each subgroups are displayed one on top of each other.
# 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()
Pingback: Analysis of Facebook Engagement of conservationist NGOs: The case of WWF – Lynn's
There is a little error in your code.
Either r2 or r3 should be equals to [x – barWidth for x in r1]
For now, the two of them are equals to [x + barWidth for x in r1], which will cause them to stack on top of each other.
You’re wrong, your method actually create the error you mention.
How to add percentages on top of these bars?
Can you add vale labels on the top of bars?
Can you add error bars on the bars. could you help me with this.
You can add error bars by giving the argument ‘yerr = ‘ in the function that calls the bar plot. You will have to give it a list of error values to plot.
This saved me a lot of time. Thanks for sharing!