Skip to content

Commit e6a27ba

Browse files
feat(pygal): implement histogram-stacked (#2627)
## Implementation: `histogram-stacked` - pygal Implements the **pygal** version of `histogram-stacked`. **File:** `plots/histogram-stacked/implementations/pygal.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20594562097)* --------- 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 6862fb1 commit e6a27ba

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
""" pyplots.ai
2+
histogram-stacked: Stacked Histogram
3+
Library: pygal 3.1.0 | Python 3.13.11
4+
Quality: 91/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 - Three groups with different distributions (plant growth measurements in cm)
13+
np.random.seed(42)
14+
group_a = np.random.normal(25, 5, 150) # Shade-grown plants
15+
group_b = np.random.normal(35, 7, 180) # Partial-sun plants
16+
group_c = np.random.normal(45, 6, 120) # Full-sun plants
17+
18+
# Define bin edges
19+
bin_edges = np.linspace(5, 65, 13) # 12 bins from 5 to 65 cm
20+
21+
# Compute histogram counts for each group
22+
counts_a, _ = np.histogram(group_a, bins=bin_edges)
23+
counts_b, _ = np.histogram(group_b, bins=bin_edges)
24+
counts_c, _ = np.histogram(group_c, bins=bin_edges)
25+
26+
# Create bin labels (center of each bin)
27+
bin_labels = [f"{int(bin_edges[i])}-{int(bin_edges[i + 1])}" for i in range(len(bin_edges) - 1)]
28+
29+
# Custom style for pyplots.ai
30+
custom_style = Style(
31+
background="white",
32+
plot_background="white",
33+
foreground="#333333",
34+
foreground_strong="#333333",
35+
foreground_subtle="#666666",
36+
colors=("#306998", "#FFD43B", "#4CAF50"), # Python Blue, Yellow, Green
37+
title_font_size=60,
38+
label_font_size=36,
39+
major_label_font_size=32,
40+
legend_font_size=36,
41+
value_font_size=28,
42+
tooltip_font_size=28,
43+
stroke_width=2,
44+
)
45+
46+
# Create stacked bar chart (pygal Bar with stacked data simulates stacked histogram)
47+
chart = pygal.StackedBar(
48+
width=4800,
49+
height=2700,
50+
style=custom_style,
51+
title="histogram-stacked \u00b7 pygal \u00b7 pyplots.ai",
52+
x_title="Plant Height (cm)",
53+
y_title="Frequency",
54+
show_y_guides=True,
55+
show_x_guides=False,
56+
legend_at_bottom=False,
57+
legend_box_size=30,
58+
margin=50,
59+
spacing=10,
60+
truncate_legend=-1,
61+
)
62+
63+
# Set x-axis labels
64+
chart.x_labels = bin_labels
65+
66+
# Add stacked data
67+
chart.add("Shade-grown", counts_a.tolist())
68+
chart.add("Partial-sun", counts_b.tolist())
69+
chart.add("Full-sun", counts_c.tolist())
70+
71+
# Save outputs
72+
chart.render_to_png("plot.png")
73+
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: histogram-stacked
3+
created: '2025-12-30T10:38:59Z'
4+
updated: '2025-12-30T10:48:54Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20594562097
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/histogram-stacked/pygal/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/histogram-stacked/pygal/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/histogram-stacked/pygal/plot.html
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Excellent implementation of stacked histogram concept using pygal StackedBar
17+
- Clean, well-organized code following KISS principles
18+
- Realistic plant growth scenario with meaningful group labels
19+
- Proper title format and axis labels with units
20+
- Good color palette with sufficient contrast between groups
21+
- Appropriate use of custom Style for consistent theming
22+
weaknesses:
23+
- Legend position in top-left corner could be moved to avoid proximity to plot area
24+
- Could leverage more pygal-specific features like custom tooltips or value labels

0 commit comments

Comments
 (0)