A stacked bar plot is a chart that shows the total value of each category while showing each child component in the same column. Simply put, each column value is the sum of the values of the smaller components, where each part of the column represents a contribution to that whole.

The goal of this chart is to compare the total value of each category's components as well as the value of each component that contributes to that category.

Differentiate it from the the Clustered Bar Plot, which compare different categories within each of the main categories.

import matplotlib.pyplot as plt
import numpy as np

# Sample data
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values1 = np.array([10, 20, 15, 25])
values2 = np.array([5, 12, 8, 10])
values3 = np.array([7, 8, 10, 15])

# Plotting the first set of bars
plt.bar(categories, values1, color='skyblue', label='Series 1')
# Plotting the second set of bars on top of the first
plt.bar(categories, values2, bottom=values1, color='coral', label='Series 2')
# Plotting the third set of bars on top of the first two
plt.bar(categories, values3, bottom=values1 + values2, color='lightgreen', label='Series 3')

# Adding labels, title, and legend
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Stacked Bar Plot Example')
plt.legend()

# Display the plot
plt.show()

Output:

Figure_1.png