Skip to content

Commit 414a5ca

Browse files
feat(plotnine): implement bar-sorted (#2563)
## Implementation: `bar-sorted` - plotnine Implements the **plotnine** version of `bar-sorted`. **File:** `plots/bar-sorted/implementations/plotnine.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20593350489)* --------- 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 da0f7f1 commit 414a5ca

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
""" pyplots.ai
2+
bar-sorted: Sorted Bar Chart
3+
Library: plotnine 0.15.2 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-30
5+
"""
6+
7+
import pandas as pd
8+
from plotnine import (
9+
aes,
10+
element_blank,
11+
element_line,
12+
element_text,
13+
geom_bar,
14+
geom_text,
15+
ggplot,
16+
labs,
17+
theme,
18+
theme_minimal,
19+
)
20+
21+
22+
# Data - Monthly sales by product category (sorted descending)
23+
data = {
24+
"category": ["Electronics", "Furniture", "Clothing", "Food & Beverage", "Home & Garden", "Sports", "Books", "Toys"],
25+
"sales": [4850, 3720, 3150, 2890, 2340, 1980, 1450, 920],
26+
}
27+
df = pd.DataFrame(data)
28+
29+
# Sort by sales descending and create ordered categorical for proper bar order
30+
df = df.sort_values("sales", ascending=False)
31+
df["category"] = pd.Categorical(df["category"], categories=df["category"], ordered=True)
32+
33+
# Create plot
34+
plot = (
35+
ggplot(df, aes(x="category", y="sales"))
36+
+ geom_bar(stat="identity", fill="#306998", width=0.7)
37+
+ geom_text(aes(label="sales"), va="bottom", size=12, nudge_y=80, color="#333333")
38+
+ labs(x="Product Category", y="Sales ($ thousands)", title="bar-sorted · plotnine · pyplots.ai")
39+
+ theme_minimal()
40+
+ theme(
41+
figure_size=(16, 9),
42+
text=element_text(size=14),
43+
axis_title=element_text(size=20),
44+
axis_text=element_text(size=16),
45+
axis_text_x=element_text(angle=30, ha="right"),
46+
plot_title=element_text(size=24),
47+
panel_grid_major_x=element_blank(),
48+
panel_grid_major_y=element_line(alpha=0.3),
49+
)
50+
)
51+
52+
# Save
53+
plot.save("plot.png", dpi=300, verbose=False)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
library: plotnine
2+
specification_id: bar-sorted
3+
created: '2025-12-30T09:31:01Z'
4+
updated: '2025-12-30T09:39:05Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20593350489
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 0.15.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/bar-sorted/plotnine/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/bar-sorted/plotnine/plot_thumb.png
12+
preview_html: null
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent use of pd.Categorical to enforce sort order in plotnine
17+
- Value labels above bars improve readability
18+
- Clean ggplot2 grammar of graphics implementation
19+
- Good font sizing for high-resolution output
20+
- Appropriate rotation of x-axis labels to prevent overlap
21+
weaknesses:
22+
- Could highlight top category with different color as spec suggests
23+
- No random seed comment (data is deterministic but convention not followed)

0 commit comments

Comments
 (0)