Skip to content

Commit 9664882

Browse files
feat(pygal): implement histogram-density (#2456)
## Implementation: `histogram-density` - pygal Implements the **pygal** version of `histogram-density`. **File:** `plots/histogram-density/implementations/pygal.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20584330628)* --------- 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 72363f4 commit 9664882

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
""" pyplots.ai
2+
histogram-density: Density Histogram
3+
Library: pygal 3.1.0 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-29
5+
"""
6+
7+
import numpy as np
8+
import pygal
9+
from pygal.style import Style
10+
11+
12+
# Data - Test scores with a realistic bimodal distribution
13+
np.random.seed(42)
14+
# Create bimodal distribution (two groups of students)
15+
scores_group1 = np.random.normal(loc=65, scale=10, size=150)
16+
scores_group2 = np.random.normal(loc=82, scale=8, size=100)
17+
scores = np.concatenate([scores_group1, scores_group2])
18+
scores = np.clip(scores, 0, 100) # Clip to valid score range
19+
20+
# Calculate histogram bins and density values
21+
n_bins = 25
22+
counts, bin_edges = np.histogram(scores, bins=n_bins, density=True)
23+
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
24+
25+
# Custom style for pyplots
26+
custom_style = Style(
27+
background="white",
28+
plot_background="white",
29+
foreground="#333333",
30+
foreground_strong="#333333",
31+
foreground_subtle="#666666",
32+
colors=("#306998",), # Python Blue
33+
title_font_size=48,
34+
label_font_size=36,
35+
major_label_font_size=32,
36+
legend_font_size=32,
37+
value_font_size=28,
38+
stroke_width=0,
39+
opacity=0.85,
40+
)
41+
42+
# Create histogram chart
43+
chart = pygal.Bar(
44+
width=4800,
45+
height=2700,
46+
style=custom_style,
47+
title="histogram-density · pygal · pyplots.ai",
48+
x_title="Test Score",
49+
y_title="Density (Probability per Unit)",
50+
show_legend=False,
51+
show_x_guides=False,
52+
show_y_guides=True,
53+
x_label_rotation=0,
54+
margin=60,
55+
spacing=1,
56+
print_values=False,
57+
)
58+
59+
# Create bar labels from bin centers (show every 5th label for clarity)
60+
chart.x_labels = [f"{int(bc)}" if i % 5 == 0 else "" for i, bc in enumerate(bin_centers)]
61+
62+
# Add density histogram data
63+
chart.add("Density", [{"value": float(c), "label": f"Score: {int(bin_centers[i])}"} for i, c in enumerate(counts)])
64+
65+
# Save outputs
66+
chart.render_to_file("plot.html")
67+
chart.render_to_png("plot.png")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
library: pygal
2+
specification_id: histogram-density
3+
created: '2025-12-29T22:47:43Z'
4+
updated: '2025-12-29T22:54:23Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20584330628
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-density/pygal/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/histogram-density/pygal/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/histogram-density/pygal/plot.html
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Excellent bimodal distribution data that clearly demonstrates density histogram
17+
concept
18+
- Clean, readable code following KISS principles with proper random seed
19+
- Well-formatted title following the required convention
20+
- Good use of pygal Style class for comprehensive customization
21+
- Appropriate axis labels with descriptive units
22+
- Smart x-label spacing (every 5th label) prevents overlap
23+
weaknesses:
24+
- Spec suggests adding a reference line or theoretical PDF overlay for context,
25+
which is not implemented
26+
- Could use pygal native Histogram chart type instead of Bar chart for more semantic
27+
correctness

0 commit comments

Comments
 (0)