Pandas Area Plots: Cumulative Trends, Stacked Areas and Options

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.

Create a basic stacked area plot 🔝

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
Pandas DataFrame stacked area chart
Default stacked area chart with Sales, Cost, and Profit

Compare stacked vs unstacked areas with alpha transparency 🔝

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 colors, line styles, and dimensions 🔝

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()
Font size option
fontsize=20 (custom axis label scale)
Custom color palette
color=my_colors (custom series palette)
Area plot boundary style 1
Boundary line style (:)
Area plot boundary style 2
Boundary line style (.-)

Configure gridlines and legend display 🔝

Toggle gridlines using grid=True or grid=False to assist readers in estimating cumulative values at a glance.

grid=True area plot
grid=True
grid=False area plot
grid=False

Dual-scale comparison with secondary_y 🔝

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.

secondary_y=True
secondary_y=True (dual scale axes)
secondary_y=False
secondary_y=False (single compressed axis)
mark_right=True
mark_right=True (labels right axis series in legend)
mark_right=False
mark_right=False

Logarithmic scaling on X-axis with logx 🔝

If horizontal categories or time intervals grow exponentially, apply logarithmic scaling with logx=True.

logx=True option
logx=True logarithmic scaling along X-axis

Rotate axis labels with rot 🔝

When dates or long category names crowd the horizontal axis, rotate them with rot=45 or rot=90.

rot=45 tick rotation
rot=45 (angled tick labels)
rot=180 tick rotation
rot=180 (demonstration)

Save charts to file using savefig() 🔝

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()

Practical workflow: quarterly department expense tracking 🔝

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

Exercise: multi-channel revenue accumulation 🔝

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.

Exercise solution 🔝

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

Practice in Google Colab 🔝

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.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer