Area Plot Gallery¶
The area plot is used for visualizing data distributions over time or across categories using filled area charts. It's ideal for showing trends and comparisons between different groups by stacking or overlaying areas.
Area plots excel at:
- Time series visualization: Show trends in metrics over time (e.g., revenue by month)
- Stacked area charts: Compare contributions of different groups over time
- Category-based distributions: Visualize data distributions across categories
In [ ]:
Copied!
import matplotlib.pyplot as plt
import pandas as pd
from pyretailscience.plots import area
import matplotlib.pyplot as plt
import pandas as pd
from pyretailscience.plots import area
In [ ]:
Copied!
# Create sample daily revenue data
# fmt: off
daily_revenue = pd.DataFrame(
{
"revenue": [45000, 52000, 48000, 61000, 67000, 58000, 63000, 71000, 68000, 75000, 82000, 79000, 85000, 88000],
},
index=pd.date_range("2024-01-01", periods=14, freq="D"),
)
# fmt: on
ax = area.plot(
daily_revenue,
value_col="revenue",
title="Daily Revenue Trend",
x_label="Date",
y_label="Revenue ($)",
)
plt.show()
# Create sample daily revenue data
# fmt: off
daily_revenue = pd.DataFrame(
{
"revenue": [45000, 52000, 48000, 61000, 67000, 58000, 63000, 71000, 68000, 75000, 82000, 79000, 85000, 88000],
},
index=pd.date_range("2024-01-01", periods=14, freq="D"),
)
# fmt: on
ax = area.plot(
daily_revenue,
value_col="revenue",
title="Daily Revenue Trend",
x_label="Date",
y_label="Revenue ($)",
)
plt.show()
In [ ]:
Copied!
# Create sample data with explicit month column
# fmt: off
monthly_sales = pd.DataFrame(
{
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
"sales": [125000, 145000, 138000, 162000, 175000, 168000],
},
)
# fmt: on
ax = area.plot(
monthly_sales,
x_col="month",
value_col="sales",
title="Monthly Sales Performance",
x_label="Month",
y_label="Sales ($)",
)
plt.show()
# Create sample data with explicit month column
# fmt: off
monthly_sales = pd.DataFrame(
{
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
"sales": [125000, 145000, 138000, 162000, 175000, 168000],
},
)
# fmt: on
ax = area.plot(
monthly_sales,
x_col="month",
value_col="sales",
title="Monthly Sales Performance",
x_label="Month",
y_label="Sales ($)",
)
plt.show()
Multiple Areas with group_col¶
Create separate areas for each category using the group_col parameter. This automatically creates a legend and uses different colors for each group.
In [ ]:
Copied!
# Create sample data with multiple product categories
# fmt: off
category_data = pd.DataFrame(
{
"week": [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5],
"category": ["Electronics", "Apparel", "Home"] * 5,
"revenue": [35000, 22000, 18000, 38000, 25000, 19000, 41000, 28000, 21000, 44000, 31000, 23000, 47000, 33000, 25000],
},
)
# fmt: on
ax = area.plot(
category_data,
x_col="week",
value_col="revenue",
group_col="category",
title="Weekly Revenue by Product Category",
x_label="Week",
y_label="Revenue ($)",
legend_title="Product Category",
move_legend_outside=True,
)
plt.show()
# Create sample data with multiple product categories
# fmt: off
category_data = pd.DataFrame(
{
"week": [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5],
"category": ["Electronics", "Apparel", "Home"] * 5,
"revenue": [35000, 22000, 18000, 38000, 25000, 19000, 41000, 28000, 21000, 44000, 31000, 23000, 47000, 33000, 25000],
},
)
# fmt: on
ax = area.plot(
category_data,
x_col="week",
value_col="revenue",
group_col="category",
title="Weekly Revenue by Product Category",
x_label="Week",
y_label="Revenue ($)",
legend_title="Product Category",
move_legend_outside=True,
)
plt.show()
Multiple Value Columns¶
Plot multiple metrics by passing a list to value_col. Note: This cannot be combined with group_col.
In [ ]:
Copied!
# Create sample data with multiple metrics
# fmt: off
metrics_data = pd.DataFrame(
{
"quarter": ["Q1", "Q2", "Q3", "Q4"],
"Online Sales": [180000, 195000, 210000, 225000],
"Store Sales": [320000, 315000, 285000, 340000],
"Wholesale": [95000, 105000, 115000, 108000],
},
)
# fmt: on
ax = area.plot(
metrics_data,
x_col="quarter",
value_col=["Online Sales", "Store Sales", "Wholesale"],
title="Quarterly Sales by Channel",
x_label="Quarter",
y_label="Sales ($)",
legend_title="Sales Channel",
move_legend_outside=True,
)
plt.show()
# Create sample data with multiple metrics
# fmt: off
metrics_data = pd.DataFrame(
{
"quarter": ["Q1", "Q2", "Q3", "Q4"],
"Online Sales": [180000, 195000, 210000, 225000],
"Store Sales": [320000, 315000, 285000, 340000],
"Wholesale": [95000, 105000, 115000, 108000],
},
)
# fmt: on
ax = area.plot(
metrics_data,
x_col="quarter",
value_col=["Online Sales", "Store Sales", "Wholesale"],
title="Quarterly Sales by Channel",
x_label="Quarter",
y_label="Sales ($)",
legend_title="Sales Channel",
move_legend_outside=True,
)
plt.show()
In [ ]:
Copied!
# Create sample data for stacked areas
# fmt: off
stacked_data = pd.DataFrame(
{
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
"New Customers": [1200, 1350, 1180, 1420, 1580, 1650],
"Returning Customers": [2800, 3100, 2950, 3200, 3450, 3300],
},
)
# fmt: on
ax = area.plot(
stacked_data,
x_col="month",
value_col=["New Customers", "Returning Customers"],
title="Customer Acquisition: Stacked View",
x_label="Month",
y_label="Number of Customers",
legend_title="Customer Type",
stacked=True,
move_legend_outside=True,
)
plt.show()
# Create sample data for stacked areas
# fmt: off
stacked_data = pd.DataFrame(
{
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
"New Customers": [1200, 1350, 1180, 1420, 1580, 1650],
"Returning Customers": [2800, 3100, 2950, 3200, 3450, 3300],
},
)
# fmt: on
ax = area.plot(
stacked_data,
x_col="month",
value_col=["New Customers", "Returning Customers"],
title="Customer Acquisition: Stacked View",
x_label="Month",
y_label="Number of Customers",
legend_title="Customer Type",
stacked=True,
move_legend_outside=True,
)
plt.show()
Plotting a Pandas Series¶
You can also plot a pandas Series directly, which automatically uses the Series name and index.
In [ ]:
Copied!
# Create a pandas Series
# fmt: off
customer_growth = pd.Series(
[2500, 2650, 2800, 3100, 3350, 3600, 3800],
index=["Week 1", "Week 2", "Week 3", "Week 4", "Week 5", "Week 6", "Week 7"],
name="Active Customers",
)
# fmt: on
ax = area.plot(
customer_growth,
value_col="Active Customers",
title="Weekly Active Customer Growth",
x_label="Week",
y_label="Number of Customers",
)
plt.show()
# Create a pandas Series
# fmt: off
customer_growth = pd.Series(
[2500, 2650, 2800, 3100, 3350, 3600, 3800],
index=["Week 1", "Week 2", "Week 3", "Week 4", "Week 5", "Week 6", "Week 7"],
name="Active Customers",
)
# fmt: on
ax = area.plot(
customer_growth,
value_col="Active Customers",
title="Weekly Active Customer Growth",
x_label="Week",
y_label="Number of Customers",
)
plt.show()
Advanced Styling Options¶
Customize the appearance with transparency, source attribution, and legend positioning.
In [ ]:
Copied!
# Create sample regional data
# fmt: off
regional_data = pd.DataFrame(
{
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
"North Region": [85000, 92000, 88000, 95000, 102000, 98000],
"South Region": [76000, 83000, 79000, 86000, 91000, 87000],
"West Region": [68000, 74000, 71000, 78000, 83000, 80000],
},
)
# fmt: on
ax = area.plot(
regional_data,
x_col="month",
value_col=["North Region", "South Region", "West Region"],
title="Regional Sales Performance",
x_label="Month",
y_label="Sales ($)",
legend_title="Region",
move_legend_outside=True,
source_text="Source: Internal Sales Database",
alpha=0.8,
)
plt.show()
# Create sample regional data
# fmt: off
regional_data = pd.DataFrame(
{
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
"North Region": [85000, 92000, 88000, 95000, 102000, 98000],
"South Region": [76000, 83000, 79000, 86000, 91000, 87000],
"West Region": [68000, 74000, 71000, 78000, 83000, 80000],
},
)
# fmt: on
ax = area.plot(
regional_data,
x_col="month",
value_col=["North Region", "South Region", "West Region"],
title="Regional Sales Performance",
x_label="Month",
y_label="Sales ($)",
legend_title="Region",
move_legend_outside=True,
source_text="Source: Internal Sales Database",
alpha=0.8,
)
plt.show()