Use DataFrame.plot.area() (or df.plot(kind='area')) to visualise cumulative totals, part-to-whole contributions, and multi-series volume over time. Learn how to control stacked vs unstacked areas, apply transparency with alpha, customise color palettes, manage secondary axes, and export high-resolution chart images.
By default, DataFrame.plot.area() stacks numeric columns on top of each other (stacked=True). Each line represents the cumulative sum of the preceding layers. All values should normally be positive for meaningful stacking.
import pandas as pd
import matplotlib.pyplot as plt
my_dict = {
"Sale": [80, 90, 75, 80, 85, 80, 75],
"Cost": [50, 40, 55, 45, 65, 60, 55],
"Profit": [8, 12, 13, 10, 9, 12, 13]
}
df = pd.DataFrame(data=my_dict)
print(df)
# Plot default stacked area chart
df.plot.area(title="Area Plot: Sales, Cost and Profit", figsize=(7, 4))
plt.show()
Expected console output:
Sale Cost Profit
0 80 50 8
1 90 40 12
2 75 55 13
3 80 45 10
4 85 65 9
5 80 60 12
6 75 55 13
To plot areas starting from the common zero baseline instead of stacking them, pass stacked=False. When unstacking, always supply an alpha value (e.g. alpha=0.4) so that foreground layers do not hide background layers.
# Unstacked area chart with transparency
df.plot.area(stacked=False, alpha=0.4, title="Unstacked Area Comparison", figsize=(7, 4))
plt.show()
Customise your chart using options for dimensions (figsize), font size (fontsize), line styles (style), and explicit color palettes (color):
# Custom RGB color tuples per series
my_colors = [(0.9, 0.4, 0.3), (0.1, 0.8, 0.1), (0.1, 0.5, 0.9)]
df.plot.area(title="Custom Palette Area Plot", color=my_colors, figsize=(6, 3))
plt.show()
Toggle gridlines using grid=True or grid=False to assist readers in estimating cumulative values at a glance.
When comparing measures of vastly different orders of magnitude, use secondary_y=True with mark_right=True to establish a dedicated right-hand Y-axis.
If horizontal categories or time intervals grow exponentially, apply logarithmic scaling with logx=True.
When dates or long category names crowd the horizontal axis, rotate them with rot=45 or rot=90.
Save your generated area chart to a high-resolution image using plt.savefig() before calling plt.show():
df.plot.area(title="Sales Performance", figsize=(7, 4))
plt.tight_layout()
plt.savefig("sales_area_plot.png", dpi=300)
print("Saved chart as sales_area_plot.png")
plt.close()
Here is an end-to-end practical workflow: track cumulative operating expenses across three departments over four quarters and validate calculations with assertions.
import pandas as pd
import matplotlib.pyplot as plt
dept_exp = pd.DataFrame({
"Quarter": ["Q1", "Q2", "Q3", "Q4"],
"Engineering": [45000, 52000, 58000, 64000],
"Marketing": [20000, 28000, 35000, 42000],
"Operations": [15000, 16000, 17500, 19000]
})
# Assert data integrity
assert len(dept_exp) == 4
total_expenses = dept_exp[["Engineering", "Marketing", "Operations"]].sum().sum()
assert total_expenses == 411500
print("Total Operating Expenses:", total_expenses)
# Plot stacked area with custom Quarter x-axis
ax = dept_exp.plot.area(
x="Quarter",
y=["Engineering", "Marketing", "Operations"],
title="Annual Cumulative Department Expenses",
figsize=(7, 4),
grid=True
)
ax.set_ylabel("Expenditure ($)")
plt.show()
Expected console output:
Total Operating Expenses: 411500
Create a DataFrame with 4 months (Jan, Feb, Mar, Apr) and three revenue streams: Hardware ([12000, 15000, 18000, 22000]), Software ([8000, 9500, 11000, 14000]), and Services ([5000, 6000, 7500, 9000]). Plot a stacked area chart with Month on the X-axis, enable gridlines, title the plot "Q1-Q2 Revenue Streams", and calculate total revenue across all streams.
import pandas as pd
import matplotlib.pyplot as plt
streams = pd.DataFrame({
"Month": ["Jan", "Feb", "Mar", "Apr"],
"Hardware": [12000, 15000, 18000, 22000],
"Software": [8000, 9500, 11000, 14000],
"Services": [5000, 6000, 7500, 9000]
})
total_rev = streams[["Hardware", "Software", "Services"]].sum().sum()
assert total_rev == 137000
print("Total Revenue:", total_rev)
streams.plot.area(
x="Month",
y=["Hardware", "Software", "Services"],
title="Q1-Q2 Revenue Streams",
grid=True,
figsize=(6, 3)
)
plt.show()
Expected console output:
Total Revenue: 137000
Open in Google Colab View on GitHub
Run the area chart examples interactively, test stacked vs unstacked alpha configurations, and verify the practice exercise directly in Google Colab. All sample datasets are embedded directly in the notebook.
Continue exploring related charting and analysis techniques:
Reference: Pandas DataFrame.plot.area official documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.