Skip to content

Commit 7db0c40

Browse files
feat(pygal): implement bar-categorical (#2678)
## Implementation: `bar-categorical` - pygal Implements the **pygal** version of `bar-categorical`. **File:** `plots/bar-categorical/implementations/pygal.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20595338218)* --------- 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 24538e3 commit 7db0c40

2 files changed

Lines changed: 99 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-categorical: Categorical Count Bar Chart
3+
Library: pygal 3.1.0 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-30
5+
"""
6+
7+
import numpy as np
8+
import pygal
9+
from pygal.style import Style
10+
11+
12+
# Data - Raw categorical values (counts computed automatically)
13+
np.random.seed(42)
14+
categories = ["Laptop", "Smartphone", "Tablet", "Desktop", "Smartwatch", "Headphones"]
15+
weights = [0.25, 0.30, 0.15, 0.10, 0.12, 0.08] # Probability weights for realistic distribution
16+
raw_data = np.random.choice(categories, size=500, p=weights)
17+
18+
# Count frequencies
19+
unique, counts = np.unique(raw_data, return_counts=True)
20+
category_counts = dict(zip(unique, counts, strict=True))
21+
22+
# Sort by count descending for better visualization
23+
sorted_items = sorted(category_counts.items(), key=lambda x: x[1], reverse=True)
24+
sorted_categories = [item[0] for item in sorted_items]
25+
sorted_counts = [item[1] for item in sorted_items]
26+
27+
# Custom style for 4800x2700 canvas
28+
custom_style = Style(
29+
background="white",
30+
plot_background="white",
31+
foreground="#333333",
32+
foreground_strong="#333333",
33+
foreground_subtle="#666666",
34+
colors=("#306998", "#FFD43B", "#4B8BBE", "#FFE873", "#306998", "#FFD43B"),
35+
title_font_size=72,
36+
label_font_size=48,
37+
major_label_font_size=42,
38+
legend_font_size=42,
39+
value_font_size=36,
40+
value_label_font_size=36,
41+
tooltip_font_size=36,
42+
stroke_width=2,
43+
opacity=0.9,
44+
opacity_hover=1.0,
45+
)
46+
47+
# Create chart
48+
chart = pygal.Bar(
49+
width=4800,
50+
height=2700,
51+
title="bar-categorical · pygal · pyplots.ai",
52+
x_title="Product Category",
53+
y_title="Count (Frequency)",
54+
style=custom_style,
55+
show_legend=False,
56+
show_y_guides=True,
57+
show_x_guides=False,
58+
print_values=True,
59+
print_values_position="top",
60+
value_formatter=lambda x: str(int(x)),
61+
margin=60,
62+
spacing=40,
63+
truncate_label=-1,
64+
x_label_rotation=0,
65+
)
66+
67+
# Set x-axis labels
68+
chart.x_labels = sorted_categories
69+
70+
# Add data - single series with counts
71+
chart.add("Count", sorted_counts)
72+
73+
# Save as PNG and HTML
74+
chart.render_to_png("plot.png")
75+
chart.render_to_file("plot.html")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
library: pygal
2+
specification_id: bar-categorical
3+
created: '2025-12-30T11:23:45Z'
4+
updated: '2025-12-30T11:36:09Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20595338218
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 3.1.0
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/bar-categorical/pygal/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/bar-categorical/pygal/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/bar-categorical/pygal/plot.html
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent text legibility with well-scaled font sizes for the 4800x2700 canvas
17+
- Clean sorted descending layout makes frequency comparison intuitive
18+
- Realistic electronics product category scenario with weighted probability distribution
19+
- Value labels on top of each bar enhance readability without cluttering
20+
- Proper KISS code structure with reproducible random seed
21+
weaknesses:
22+
- Could use more distinctive pygal features (e.g., custom tooltips with percentage,
23+
value_formatter showing both count and percentage)
24+
- Color variety could be added to distinguish high vs low frequency categories

0 commit comments

Comments
 (0)