Skip to content

Commit ca4ad07

Browse files
feat(letsplot): implement area-stacked-percent (#2689)
## Implementation: `area-stacked-percent` - letsplot Implements the **letsplot** version of `area-stacked-percent`. **File:** `plots/area-stacked-percent/implementations/letsplot.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20595341903)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 2c1193e commit ca4ad07

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
""" pyplots.ai
2+
area-stacked-percent: 100% Stacked Area Chart
3+
Library: letsplot 4.8.2 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-30
5+
"""
6+
7+
import numpy as np
8+
import pandas as pd
9+
from lets_plot import (
10+
LetsPlot,
11+
aes,
12+
element_blank,
13+
element_line,
14+
element_rect,
15+
element_text,
16+
geom_area,
17+
ggplot,
18+
ggsize,
19+
labs,
20+
scale_fill_manual,
21+
scale_x_continuous,
22+
scale_y_continuous,
23+
theme,
24+
theme_minimal,
25+
)
26+
from lets_plot.export import ggsave
27+
28+
29+
LetsPlot.setup_html()
30+
31+
# Data - Market share evolution over 8 years
32+
np.random.seed(42)
33+
34+
years = list(range(2016, 2024))
35+
36+
# Simulate market share trends (values will be normalized to 100%)
37+
company_a = [40, 38, 42, 45, 48, 52, 55, 58] # Growing leader
38+
company_b = [35, 36, 33, 30, 28, 25, 23, 22] # Declining
39+
company_c = [15, 16, 15, 16, 15, 14, 13, 12] # Stable small player
40+
company_d = [10, 10, 10, 9, 9, 9, 9, 8] # Smallest, slight decline
41+
42+
# Normalize to 100%
43+
totals = [a + b + c + d for a, b, c, d in zip(company_a, company_b, company_c, company_d)]
44+
company_a_pct = [a / t * 100 for a, t in zip(company_a, totals)]
45+
company_b_pct = [b / t * 100 for b, t in zip(company_b, totals)]
46+
company_c_pct = [c / t * 100 for c, t in zip(company_c, totals)]
47+
company_d_pct = [d / t * 100 for d, t in zip(company_d, totals)]
48+
49+
# Create long-format dataframe for lets-plot
50+
df = pd.DataFrame(
51+
{
52+
"Year": years * 4,
53+
"Share": company_a_pct + company_b_pct + company_c_pct + company_d_pct,
54+
"Company": ["Company A"] * 8 + ["Company B"] * 8 + ["Company C"] * 8 + ["Company D"] * 8,
55+
}
56+
)
57+
58+
# Set category order for proper stacking
59+
df["Company"] = pd.Categorical(
60+
df["Company"], categories=["Company D", "Company C", "Company B", "Company A"], ordered=True
61+
)
62+
63+
# Plot
64+
plot = (
65+
ggplot(df, aes(x="Year", y="Share", fill="Company"))
66+
+ geom_area(position="fill", alpha=0.85)
67+
+ scale_fill_manual(values=["#9B59B6", "#2ECC71", "#FFD43B", "#306998"])
68+
+ scale_x_continuous(breaks=list(range(2016, 2024)))
69+
+ scale_y_continuous(format=".0%")
70+
+ labs(x="Year", y="Market Share (%)", title="area-stacked-percent · letsplot · pyplots.ai")
71+
+ theme_minimal()
72+
+ theme(
73+
plot_title=element_text(size=26),
74+
axis_title=element_text(size=22),
75+
axis_text=element_text(size=18),
76+
legend_title=element_text(size=18),
77+
legend_text=element_text(size=16),
78+
panel_grid_major=element_line(color="#DDDDDD", size=0.3),
79+
panel_grid_minor=element_blank(),
80+
panel_background=element_rect(fill="#FAFAFA"),
81+
)
82+
+ ggsize(1600, 900)
83+
)
84+
85+
# Save PNG (scale=3 gives 4800x2700)
86+
ggsave(plot, "plot.png", path=".", scale=3)
87+
88+
# Save HTML for interactivity
89+
ggsave(plot, "plot.html", path=".")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library: letsplot
2+
specification_id: area-stacked-percent
3+
created: '2025-12-30T11:30:04Z'
4+
updated: '2025-12-30T11:36:56Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20595341903
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 4.8.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/area-stacked-percent/letsplot/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/area-stacked-percent/letsplot/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/area-stacked-percent/letsplot/plot.html
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Excellent visual clarity with well-chosen colors that are colorblind-friendly
17+
- Perfect 100% stacked area representation matching the spec exactly
18+
- Realistic and informative market share scenario that clearly shows composition
19+
changes over time
20+
- Clean ggplot2-style code structure using lets-plot grammar of graphics
21+
weaknesses:
22+
- Year labels display with comma separators (2,016 instead of 2016) which looks
23+
unnatural for year values
24+
- The HTML export setup_html() call is unnecessary overhead for PNG-only output

0 commit comments

Comments
 (0)