In the previous chapters, we explored line plots, bar plots, histograms, scatter plots, and pie charts. Another powerful visualization tool in Matplotlib is the stack plot. Stack plots are used to show how different components contribute to a whole over time (or another continuous variable). They are particularly useful for displaying cumulative data trends.
📌 What is a Stack Plot?
A stack plot is similar to a line plot, but instead of just drawing one line, multiple datasets are stacked on top of each other. This makes it easy to visualize how different parts add up to a total over time.
👉 For example, if you want to see how different categories (like work, exercise, sleep, and leisure) contribute to your daily routine, a stack plot can represent this clearly.
📊 Basic Stack Plot Example
import matplotlib.pyplot as plt
# Sample data
days = [1, 2, 3, 4, 5]
sleep = [7, 8, 6, 11, 7]
work = [8, 7, 7, 8, 6]
exercise = [1, 2, 1, 2, 1]
leisure = [2, 3, 4, 2, 3]
# Plotting stack plot
plt.stackplot(days, sleep, work, exercise, leisure,
labels=['Sleep', 'Work', 'Exercise', 'Leisure'],
colors=['#6fa8dc', '#f6b26b', '#93c47d', '#e06666'])
# Adding legend and labels
plt.xlabel("Days")
plt.ylabel("Hours")
plt.title("Daily Activities Stack Plot")
plt.legend(loc='upper left')
plt.show()
✅ This will produce a stacked area chart showing how hours are divided between sleep, work, exercise, and leisure across five days.
🎨 Customizing Stack Plots
1. Adding Transparency
You can make layers slightly transparent using the alpha
parameter.
plt.stackplot(days, sleep, work, exercise, leisure,
labels=['Sleep', 'Work', 'Exercise', 'Leisure'],
colors=['#6fa8dc', '#f6b26b', '#93c47d', '#e06666'],
alpha=0.8)
2. Emphasizing a Category
Sometimes you want to highlight a particular category by giving it a brighter or darker color.
plt.stackplot(days, sleep, work, exercise, leisure,
labels=['Sleep', 'Work', 'Exercise', 'Leisure'],
colors=['lightblue', 'orange', 'lightgreen', 'red'])
3. Displaying Cumulative Trends
Stack plots are great for cumulative data. For example, tracking website visitors from different sources over time.
days = [1, 2, 3, 4, 5, 6, 7]
organic = [50, 60, 70, 80, 90, 100, 120]
social = [30, 40, 45, 50, 55, 60, 65]
ads = [20, 25, 30, 35, 40, 50, 55]
plt.stackplot(days, organic, social, ads,
labels=['Organic', 'Social Media', 'Ads'],
colors=['#4caf50', '#2196f3', '#ff9800'])
plt.xlabel("Days")
plt.ylabel("Visitors")
plt.title("Website Visitors by Source")
plt.legend(loc='upper left')
plt.show()
✅ Key Takeaways
- Stack plots are useful for showing how components contribute to a total over time.
- They work best with continuous data (like days, months, years).
- You can customize colors, add transparency (
alpha
), and emphasize categories. - They are ideal for cumulative data analysis like activities, expenses, or traffic sources.
👉 In the next chapter, we will explore Chapter 8 – Subplots in Matplotlib, where you’ll learn how to display multiple visualizations in a single figure for comparison.