Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions plots/histogram-stacked/implementations/pygal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
""" pyplots.ai
histogram-stacked: Stacked Histogram
Library: pygal 3.1.0 | Python 3.13.11
Quality: 91/100 | Created: 2025-12-30
"""

import numpy as np
import pygal
from pygal.style import Style


# Data - Three groups with different distributions (plant growth measurements in cm)
np.random.seed(42)
group_a = np.random.normal(25, 5, 150) # Shade-grown plants
group_b = np.random.normal(35, 7, 180) # Partial-sun plants
group_c = np.random.normal(45, 6, 120) # Full-sun plants

# Define bin edges
bin_edges = np.linspace(5, 65, 13) # 12 bins from 5 to 65 cm

# Compute histogram counts for each group
counts_a, _ = np.histogram(group_a, bins=bin_edges)
counts_b, _ = np.histogram(group_b, bins=bin_edges)
counts_c, _ = np.histogram(group_c, bins=bin_edges)

# Create bin labels (center of each bin)
bin_labels = [f"{int(bin_edges[i])}-{int(bin_edges[i + 1])}" for i in range(len(bin_edges) - 1)]

# Custom style for pyplots.ai
custom_style = Style(
background="white",
plot_background="white",
foreground="#333333",
foreground_strong="#333333",
foreground_subtle="#666666",
colors=("#306998", "#FFD43B", "#4CAF50"), # Python Blue, Yellow, Green
title_font_size=60,
label_font_size=36,
major_label_font_size=32,
legend_font_size=36,
value_font_size=28,
tooltip_font_size=28,
stroke_width=2,
)

# Create stacked bar chart (pygal Bar with stacked data simulates stacked histogram)
chart = pygal.StackedBar(
width=4800,
height=2700,
style=custom_style,
title="histogram-stacked \u00b7 pygal \u00b7 pyplots.ai",
x_title="Plant Height (cm)",
y_title="Frequency",
show_y_guides=True,
show_x_guides=False,
legend_at_bottom=False,
legend_box_size=30,
margin=50,
spacing=10,
truncate_legend=-1,
)

# Set x-axis labels
chart.x_labels = bin_labels

# Add stacked data
chart.add("Shade-grown", counts_a.tolist())
chart.add("Partial-sun", counts_b.tolist())
chart.add("Full-sun", counts_c.tolist())

# Save outputs
chart.render_to_png("plot.png")
chart.render_to_file("plot.html")
24 changes: 24 additions & 0 deletions plots/histogram-stacked/metadata/pygal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
library: pygal
specification_id: histogram-stacked
created: '2025-12-30T10:38:59Z'
updated: '2025-12-30T10:48:54Z'
generated_by: claude-opus-4-5-20251101
workflow_run: 20594562097
issue: 0
python_version: 3.13.11
library_version: 3.1.0
preview_url: https://storage.googleapis.com/pyplots-images/plots/histogram-stacked/pygal/plot.png
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/histogram-stacked/pygal/plot_thumb.png
preview_html: https://storage.googleapis.com/pyplots-images/plots/histogram-stacked/pygal/plot.html
quality_score: 91
review:
strengths:
- Excellent implementation of stacked histogram concept using pygal StackedBar
- Clean, well-organized code following KISS principles
- Realistic plant growth scenario with meaningful group labels
- Proper title format and axis labels with units
- Good color palette with sufficient contrast between groups
- Appropriate use of custom Style for consistent theming
weaknesses:
- Legend position in top-left corner could be moved to avoid proximity to plot area
- Could leverage more pygal-specific features like custom tooltips or value labels