Python:Matplotlib .stackplot()
The .stackplot() method in Matplotlib creates stacked area plots (also known as stacked area charts) that display multiple datasets as vertically stacked areas. Each area represents the cumulative contribution of different categories to a total, making it ideal for visualizing proportional relationships as those relationships change over time.
Syntax
matplotlib.pyplot.stackplot(x, *args, labels=(), colors=None, hatch=None, baseline='zero', data=None, **kwargs)
Parameters:
x: Array-like. The x-coordinates of the data points.*args: One or more array-like sequences representing the y-values for each stack layer.labels: List of strings, optional. Labels for each stack layer (used in legends).colors: List of colors or color specifications for each stack layer.hatch: String or sequence, optional. Hatching patterns applied to the filled areas.baseline: String, defines the baseline for stacking:'zero'(default): Stack from y = 0.'sym': Symmetric stacking around zero.'wiggle': Minimizes slope changes between layers.'weighted_wiggle': Weighted version of the wiggle baseline.
data: Object with labeled data (e.g., dict or DataFrame). Optional data source.**kwargs: Additional keyword arguments passed toPolyCollection(e.g.,alphafor transparency).
Return value:
Returns a list of PolyCollection objects, one for each stack layer.
Example 1: Visualizing Monthly Sales by Category
This example shows a stacked area chart of sales data across product categories over time:
import matplotlib.pyplot as plt# Sample datamonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']electronics = [20, 25, 30, 28, 35, 40]clothing = [15, 18, 22, 25, 20, 30]books = [10, 12, 15, 18, 22, 25]# Create stacked area plotplt.stackplot(months, electronics, clothing, books,labels=['Electronics', 'Clothing', 'Books'],colors=['#ff9999', '#66b3ff', '#99ff99'],alpha=0.8)plt.xlabel('Month')plt.ylabel('Sales (in thousands)')plt.title('Monthly Sales by Category')plt.legend(loc='upper left')plt.show()
This creates a stacked area chart where each colored area represents a product category’s contribution to total sales:

Example 2: Stacked Areas with Multiple Series
This example demonstrates stacking multiple data series using numeric values:
import matplotlib.pyplot as pltimport numpy as np# Create sample datax = [1, 2, 3, 4, 5]y1 = [1, 2, 3, 2, 1]y2 = [2, 3, 2, 4, 3]y3 = [1, 1, 2, 1, 2]# Create stackplotplt.stackplot(x, y1, y2, y3,labels=['Series A', 'Series B', 'Series C'],colors=['lightcoral', 'lightblue', 'lightgreen'],alpha=0.8)plt.xlabel('X Values')plt.ylabel('Y Values')plt.title('Basic Stackplot Example')plt.legend(loc='upper left')plt.grid(True, alpha=0.3)plt.show()
This creates a basic stacked area plot with three data series, demonstrating the fundamental structure of stackplot visualizations:

All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Matplotlib on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours