The stacked bar graph is a modified version of the bar chart. Such charts are used to visualize values of subgroups within a group of values. It can be plotted by using bar() function of the matplotlib module.

The syntax for plotting a stacked bar graph in Python is:, width=0.8bottom=y1*align='center'data=None**kwargs) 

A simple example of stacked bar chart in Python is given below.

import matplotlib.pyplot as plt

# Data values
year = [2014, 2015, 2016, 2017, 2018, 2019]  
men =  [39, 77, 98, 54, 28, 15]  
women = [ 3, 10, 13, 56, 39, 14]  

# Setting the figure size
plt.figure(figsize=(10, 8))

# Plotting the bar chart for men
p1 =, men, color="#fba500")

# Plotting the second bar chart on top of the previous one
p2 =, women, bottom = men, color="#3792cb")

# Setting the title and labels
plt.title("Sample Stacked bar chart")
plt.ylabel('Number of Students Graduated')

# Setting the legend
plt.legend((p1[0], p2[0]), ('Men', 'Women'))

# Display the plot 
stacked bar chart in Python

In Conclusion

Which graphs do you use commonly for data visualization in Python? Let us know in the comments below. You may like this tutorial on other kinds of plots in Python: Data Visualization in Python (With Tutorials).


Please enter your comment!
Please enter your name here