No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Stacked Bar Plot with Python

by Khaleel O.
July 27, 2021
in Python
Reading Time: 4 mins read
A A
Stacked Bar Plot Python
Stacked Bar Plot Python

Now, we will create a Stacked Bar Plot with Python. Be sure to check out our previous tutorial on bar graphs with matplotib for a good foundation before proceeding.

First, here is our code:

import matplotlib.pyplot as plt
import pandas as pd
  
d = {'CLASS_TYPE': ['A','B','C','D','E'], 'MEN': [20,25,30,19,38], 'WOMEN': [21,31,55,22,39]}

df = pd.DataFrame(data=d)


classType = df["CLASS_TYPE"]
men_count = df["MEN"]
women_count = df["WOMEN"]
width = 0.35       # the width of the bars

fig, ax = plt.subplots()

ax = fig.add_axes([0,0,1.5,1.5])

ax.bar(classType, men_count, width, label='Men')
ax.bar(classType , women_count, width, bottom=men_count, label='Women')

ax.set_ylabel('Count')
ax.set_xlabel('Classes')
ax.set_title('Count of Classes By Gender')
ax.legend()

for x,y1,y2 in zip(classType,women_count,men_count):

    label = "{:.2f}".format(y1)

    plt.annotate(label, # label text
                 (x,y1), #  The point (x, y) to annotate
                 textcoords="offset points", # offset (in points) from the xy value
                 xytext=(0,6), # position (x, y) to place the text at. 
                 ha='center') # horizontal alignment is center in this case
    
    label = "{:.2f}".format(y2)

    plt.annotate(label, 
                 (x,y2), 
                 textcoords="offset points", 
                 xytext=(0,-14), 
                 ha='center') 

    label = "{:.2f}".format(y1+y2)

    plt.annotate(label, 
                 (x,y1+y2), 
                 textcoords="offset points",
                 xytext=(0,9), 
                 ha='center') 

plt.show()

Let’s explain what’s going on here:

  1. A stacked bar chart or stacked bar graph, is a bar graph that is used to identify and compare parts of the whole. The bars are stacked one on top of the other and each bar in a stack is part of the whole. In this case, we are going to look at counts of MEN and WOMEN who belong to one of 5 classes in a sample dataset. When complete, our stacked bar will have two parts one for MEN and one for WOMEN.
  2. We declare our dataframe df with our sample dataset. The first column lists 5 classes (A,B,C,D,E) and is called CLASS_TYPE, the second column shows how many MEN belong to each class and the third shows how many WOMEN belong to each class.
  3. To create our plot, we create 3 separate lists from the dataframe columns. The classType list is a list of our 5 class categories. The men_count is a list of the counts of men that belong to each class. The women_count is a list of counts of women that belong to each class.
  4. plt.subplots() creates a figure and a grid of subplots with a single call.
  5. fig.add_axes([0,0,1.5,1.5]) adds axes to the figure and is used to change the relative size of the plot.
  6. ax.bar() is the method we use to build our plot. In the first call we supply the classType, the men_count, width and the label as parameters. In the second call, we supply the same except that we add the women_count and the parameter bottom which is the y coordinate(s) of the bars bases. Because we are doing a stacked bar plot, we have to supply this parameter so that we know the order of the parts of the stack. Our stack categories are MEN and WOMEN and we have specified on this occasion that MEN will be at the bottom of the stack.
  7. We set the x and y axis labels, the bar plot title and the legend. The legend will have two items: WOMEN and MEN. This is because they will be named after the column names for our stack categories MEN and WOMEN. The bar will have a separate color for each.
  8. The FOR loop simply adds the data labels to our plot, also known as an annotation. The plt.annotate() method simple adds the count of MEN, WOMEN and the total of both for each class/category, to the bar plot.
  9. When we see the graph we see that it is a stacked bar graph. The “whole” is the sum of WOMEN and MEN for each category. The whole is of course made of two parts: WOMEN and MEN. The stacked bar graph will show a bar divided into two parts: one for MEN and one for WOMEN. These parts are stacked on top each other.

Following is an image of our plot:

Stacked Bar Plot Python
Stacked Bar Plot with Data Labels

We hope this tutorial helped. See the full source code HERE and Python Notebook HERE. Good luck 👌👌👌.

Tags: bar graphbar plotmatplotlib
Previous Post

Plot Bar Graph with Python and matplotlib

Next Post

Plotly Bar Chart in Python with Code

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy