๐ Day 4: Grouped Bar Chart in Python
๐ What is a Grouped Bar Chart?
A grouped bar chart (also called a clustered bar chart) is used to compare multiple values within the same category.
Instead of one bar per category, you see multiple bars placed side by side for easy comparison.
✅ When Should You Use a Grouped Bar Chart?
Use a grouped bar chart when:
-
You have two or more sub-categories
-
You want to compare values within and across categories
-
Exact comparison between groups is important
Real-world examples:
-
Sales of multiple products across years
-
Marks of boys vs girls in each class
-
Revenue comparison of companies per quarter
๐ Example Dataset
Let’s compare sales of two products over different years:
| Year | Product A | Product B |
|---|---|---|
| 2022 | 50 | 40 |
| 2023 | 70 | 60 |
| 2024 | 90 | 75 |
๐ง Python Code: Grouped Bar Chart Using Matplotlib
๐งฉ Code Explanation (Simple)
np.arange() → creates x-axis positions
width → controls bar thickness and spacing
x - width/2 & x + width/2 → place bars side by side
legend() → explains which bar belongs to which category
๐ Grouped Bar Chart vs Stacked Bar Chart
| Grouped Bar Chart | Stacked Bar Chart |
|---|---|
| Bars are side-by-side | Bars are stacked |
| Easy comparison | Shows composition |
| Best for exact values | Best for proportions |
๐ Key Takeaways
✔ Used to compare multiple values per category
✔ Bars are placed side by side
✔ Ideal for detailed comparisons
.png)

0 Comments:
Post a Comment