0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Create Grouped Bar Chart in Matplotlib: Simple Guide

To create a grouped bar chart in matplotlib, use plt.bar() multiple times with adjusted x positions for each group. Set the width of bars and shift the x positions to place bars side by side for each category.
๐Ÿ“

Syntax

Use plt.bar() for each group of bars. Adjust the x positions by adding an offset to separate groups. Set width to control bar thickness.

  • x: positions on x-axis
  • height: bar heights
  • width: bar width
  • label: legend label
python
plt.bar(x, height, width=bar_width, label='Group Label')
๐Ÿ’ป

Example

This example shows how to create a grouped bar chart with two groups for three categories. Bars are placed side by side by shifting x positions.

python
import matplotlib.pyplot as plt
import numpy as np

categories = ['A', 'B', 'C']
values1 = [5, 7, 4]
values2 = [6, 8, 5]

x = np.arange(len(categories))  # positions for categories
bar_width = 0.35

plt.bar(x, values1, width=bar_width, label='Group 1')
plt.bar(x + bar_width, values2, width=bar_width, label='Group 2')

plt.xticks(x + bar_width / 2, categories)
plt.xlabel('Category')
plt.ylabel('Value')
plt.title('Grouped Bar Chart Example')
plt.legend()
plt.show()
Output
A grouped bar chart with categories A, B, C on x-axis and two bars side by side for each category labeled Group 1 and Group 2.
โš ๏ธ

Common Pitfalls

Common mistakes include overlapping bars by not shifting x positions, using incorrect bar width, or forgetting to adjust x-ticks for labels.

Always shift x positions by bar width to avoid overlap and set x-ticks in the middle of grouped bars for clear labels.

python
import matplotlib.pyplot as plt
import numpy as np

categories = ['A', 'B', 'C']
values1 = [5, 7, 4]
values2 = [6, 8, 5]

x = np.arange(len(categories))
bar_width = 0.35

# Wrong: bars overlap because x positions are the same
plt.bar(x, values1, width=bar_width, label='Group 1')
plt.bar(x, values2, width=bar_width, label='Group 2')  # Overlaps

plt.xticks(x, categories)
plt.legend()
plt.show()

# Right: shift second group by bar_width
plt.bar(x, values1, width=bar_width, label='Group 1')
plt.bar(x + bar_width, values2, width=bar_width, label='Group 2')
plt.xticks(x + bar_width / 2, categories)
plt.legend()
plt.show()
Output
First plot shows overlapping bars; second plot shows properly grouped bars side by side.
๐Ÿ“Š

Quick Reference

StepDescription
1Create x positions with numpy.arange for categories
2Set bar width (e.g., 0.35)
3Plot first group bars at x positions
4Plot second group bars at x + bar_width positions
5Set x-ticks at x + bar_width/2 for centered labels
6Add labels, title, and legend
โœ…

Key Takeaways

Use plt.bar() multiple times with shifted x positions to create grouped bars.
Set bar width carefully to avoid overlap and keep bars visually balanced.
Adjust x-ticks to be centered between grouped bars for clear category labels.
Common mistake is overlapping bars by not shifting x positions properly.
Label your groups and add a legend for easy interpretation.