Saturday, 31 January 2026

๐Ÿ“Š Day 5: Stacked Bar Chart in Python

 

๐Ÿ“Š Day 5: Stacked Bar Chart in Python 


๐Ÿ” What is a Stacked Bar Chart?

A stacked bar chart displays bars on top of each other instead of side by side.

Each bar represents:

  • The total value of a category

  • The contribution of each sub-category within that total

This makes it easy to see both:
✔ Overall totals
✔ Individual contributions


✅ When Should You Use a Stacked Bar Chart?

Use a stacked bar chart when:

  • You want to show part-to-whole relationships

  • Total value and composition both matter

  • Comparing how components change across categories

Real-world examples:

  • Product-wise sales per year

  • Department-wise expenses

  • Male vs female population by year


๐Ÿ“Š Example Dataset

Let’s visualize yearly sales of two products:

YearProduct AProduct B
20225040
20237060
20249075

Each bar shows total yearly sales, while colors show Product A and Product B contributions.


๐Ÿง  Python Code: Stacked Bar Chart Using Matplotlib

import matplotlib.pyplot as plt import numpy as np years = ['2022', '2023', '2024']
product_a = [50, 70, 90] product_b = [40, 60, 75] x = np.arange(len(years)) plt.bar(x, product_a, label='Product A') plt.bar(x, product_b, bottom=product_a, label='Product B')
plt.xlabel('Year') plt.ylabel('Sales') plt.title('Yearly Sales (Stacked Bar Chart)') plt.xticks(x, years)
plt.legend()

plt.show()

๐Ÿงฉ Code Explanation (Simple)

  • plt.bar(x, product_a) → creates the base bars

  • bottom=product_a → stacks Product B on top of Product A

  • legend() → identifies each product

  • Total bar height = Product A + Product B


๐Ÿ“Š Stacked Bar Chart vs Grouped Bar Chart

Stacked Bar ChartGrouped Bar Chart

Shows composition   Shows comparison
 Highlights totals        Highlights differences
 Parts stacked              Bars side by side

๐Ÿ”‘ Key Takeaways

✔ Best for showing total + breakdown
✔ Useful for composition analysis
✔ Easy to understand trends in parts

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (190) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (8) BI (10) Books (262) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (299) Cybersecurity (29) data (1) Data Analysis (25) Data Analytics (18) data management (15) Data Science (256) Data Strucures (15) Deep Learning (106) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (18) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (54) Git (9) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (229) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (13) PHP (20) Projects (32) Python (1246) Python Coding Challenge (992) Python Mistakes (43) Python Quiz (406) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (46) Udemy (17) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)