Skip to content

Commit b5852ad

Browse files
feat(plotly): implement bar-sorted (#2559)
## Implementation: `bar-sorted` - plotly Implements the **plotly** version of `bar-sorted`. **File:** `plots/bar-sorted/implementations/plotly.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20593348157)* --------- 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 8f14eb8 commit b5852ad

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
""" pyplots.ai
2+
bar-sorted: Sorted Bar Chart
3+
Library: plotly 6.5.0 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-30
5+
"""
6+
7+
import numpy as np
8+
import plotly.graph_objects as go
9+
10+
11+
# Data - Monthly sales by product category (realistic business scenario)
12+
np.random.seed(42)
13+
categories = [
14+
"Electronics",
15+
"Clothing",
16+
"Home & Garden",
17+
"Sports",
18+
"Books",
19+
"Toys",
20+
"Beauty",
21+
"Food & Grocery",
22+
"Automotive",
23+
"Office Supplies",
24+
]
25+
values = np.array([450, 380, 320, 280, 250, 220, 195, 170, 140, 95])
26+
27+
# Sort by value descending
28+
sorted_indices = np.argsort(values)[::-1]
29+
sorted_categories = [categories[i] for i in sorted_indices]
30+
sorted_values = values[sorted_indices]
31+
32+
# Create figure with horizontal bars (better for labels)
33+
fig = go.Figure()
34+
35+
fig.add_trace(
36+
go.Bar(
37+
y=sorted_categories,
38+
x=sorted_values,
39+
orientation="h",
40+
marker=dict(
41+
color="#306998", # Python Blue
42+
line=dict(color="#1e4d6b", width=1.5),
43+
),
44+
text=sorted_values,
45+
textposition="outside",
46+
textfont=dict(size=18),
47+
)
48+
)
49+
50+
# Layout for 4800x2700 px
51+
fig.update_layout(
52+
title=dict(text="bar-sorted · plotly · pyplots.ai", font=dict(size=32), x=0.5, xanchor="center"),
53+
xaxis=dict(
54+
title=dict(text="Sales (thousands USD)", font=dict(size=24)),
55+
tickfont=dict(size=18),
56+
gridcolor="rgba(0,0,0,0.1)",
57+
gridwidth=1,
58+
showgrid=True,
59+
range=[0, max(sorted_values) * 1.15], # Extra space for labels
60+
),
61+
yaxis=dict(
62+
title=dict(text="Product Category", font=dict(size=24)),
63+
tickfont=dict(size=18),
64+
autorange="reversed", # Largest at top
65+
),
66+
template="plotly_white",
67+
margin=dict(l=180, r=80, t=100, b=80),
68+
bargap=0.25,
69+
)
70+
71+
# Save as PNG (4800x2700 using scale=3)
72+
fig.write_image("plot.png", width=1600, height=900, scale=3)
73+
74+
# Save interactive HTML
75+
fig.write_html("plot.html", include_plotlyjs="cdn")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library: plotly
2+
specification_id: bar-sorted
3+
created: '2025-12-30T09:30:41Z'
4+
updated: '2025-12-30T09:37:56Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20593348157
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 6.5.0
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/bar-sorted/plotly/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/bar-sorted/plotly/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/bar-sorted/plotly/plot.html
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent horizontal bar orientation choice for category labels readability
17+
- Clean sorting implementation with numpy argsort
18+
- Professional appearance with Python Blue color scheme and subtle borders
19+
- Proper use of Plotly graph_objects for fine-grained control
20+
- Value labels positioned outside bars improve readability
21+
- Correct title format following pyplots.ai standards
22+
- Both PNG and HTML outputs generated for maximum utility
23+
weaknesses:
24+
- 'Minor: Grid lines could be slightly more subtle (current alpha 0.1 is good but
25+
gridwidth=1 makes them slightly prominent)'

0 commit comments

Comments
 (0)