Sometimes, you don't just want to compare total amounts across categories; you also want to see how those totals are composed. Imagine comparing sales figures across different months. A standard bar chart shows the total sales each month. But what if you want to see the contribution of different product lines (e.g., electronics, clothing, home goods) to the total sales within each month? This is where stacking plots becomes useful. Stacking allows you to visualize parts of a whole, layered on top of each other within a single bar or area.
Stacked bar charts are a direct extension of the standard bar charts you learned about earlier using plt.bar()
. Instead of placing bars side-by-side, you stack segments representing different subgroups one on top of the other. The total height of each bar still represents the overall total for that category, but the segments show the relative contribution of each subgroup.
To create a stacked bar chart in Matplotlib, you plot multiple datasets using plt.bar()
, but you use the bottom
argument for subsequent datasets. The bottom
argument specifies the y-coordinate(s) where the bottom of the new bars should start. For the second dataset you plot, you set bottom
equal to the values of the first dataset. For the third dataset, you set bottom
equal to the sum of the first and second datasets, and so on.
Let's look at an example. Suppose we have sales data for three product categories (A, B, C) across four quarters (Q1, Q2, Q3, Q4).
import matplotlib.pyplot as plt
import numpy as np
# Sample data: Sales per quarter for 3 product categories
categories = ['Q1', 'Q2', 'Q3', 'Q4']
sales_A = np.array([150, 200, 180, 220])
sales_B = np.array([250, 180, 300, 280])
sales_C = np.array([100, 120, 150, 100])
# Calculate the bottom positions for B and C
bottom_B = sales_A
bottom_C = sales_A + sales_B # Sum of A and B
# Create the figure and axes
fig, ax = plt.subplots(figsize=(8, 5)) # Adjust figure size for better readability
# Plot the bars for each category, stacking them
ax.bar(categories, sales_A, label='Product A', color='#4dabf7') # Blue
ax.bar(categories, sales_B, label='Product B', bottom=bottom_B, color='#69db7c') # Green
ax.bar(categories, sales_C, label='Product C', bottom=bottom_C, color='#ff922b') # Orange
# Add labels and title
ax.set_xlabel('Quarter')
ax.set_ylabel('Total Sales')
ax.set_title('Quarterly Sales by Product Category')
ax.legend() # Display the legend to identify categories
# Improve layout and display the plot
plt.tight_layout()
plt.show()
Stacked bar chart showing sales contributions of three products across four quarters. Each segment's height represents its sales, and the total bar height represents the total sales for that quarter.
In this code, we first plot sales_A
. Then, we plot sales_B
specifying bottom=sales_A
, so the bars for Product B start where the bars for Product A end. Finally, we plot sales_C
with bottom=sales_A + sales_B
. The label
argument is important for identifying which color corresponds to which product category when ax.legend()
is called.
Similar to stacked bar charts, stacked area charts show how parts contribute to a whole, but they are typically used over a continuous axis, like time. Instead of bars, continuous areas are stacked on top of each other. Matplotlib provides the plt.stackplot()
function for this purpose. While useful for showing trends in composition over time, they share a limitation with stacked bar charts: it can be difficult to accurately judge the size or trend of segments that are not at the bottom. We won't go into detail here, but be aware of this alternative for continuous data.
Stacked plots are effective when:
However, be cautious. Comparing the exact size of segments can be challenging, especially for segments not resting on the baseline. If the primary goal is to compare the individual subgroups across the main categories, separate or grouped bar charts might be clearer.
© 2025 ApX Machine Learning