Grouped Barplot

logo of a chart:Bar

A grouped barplot is used when you have several groups, and subgroups of these groups. The example in this post shows how to build a grouped barplor using the bar() function of matplotlib library.

Grouped barplot

You can plot a grouped barplot using the bar() function of matplotlib. The following example displays 5 different groups with their 3 variables. In order to do that, the values and positions of variables are passed to 3 bar() functions.

Note that if you want to turn the graph into a stacked area barplot, you can check the following post.

import numpy as np
import matplotlib.pyplot as plt

# Data
barWidth = 0.25
bars1 = [12, 30, 1, 8, 22]
bars2 = [28, 6, 16, 5, 10]
bars3 = [29, 3, 24, 25, 17]

# Bar positions
r = np.arange(len(bars1))
r2 = r + barWidth
r3 = r2 + barWidth

# Plotting
fig, ax = plt.subplots(dpi=300)
ax.bar(r, bars1, color='#7f6d5f', width=barWidth, edgecolor='white', label='var1')
ax.bar(r2, bars2, color='#557f2d', width=barWidth, edgecolor='white', label='var2')
ax.bar(r3, bars3, color='#2d7f5e', width=barWidth, edgecolor='white', label='var3')

# Xticks
ax.set_xlabel('group', fontweight='bold')
ax.set_xticks(r + barWidth)
ax.set_xticklabels(['A', 'B', 'C', 'D', 'E'])

# Legend and show
ax.legend()
plt.show()

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!