Skip to content

Commit 1f30b53

Browse files
feat(pygal): implement line-confidence (#2251)
## Implementation: `line-confidence` - pygal Implements the **pygal** version of `line-confidence`. **File:** `plots/line-confidence/implementations/pygal.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20525435948)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent c5ff702 commit 1f30b53

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
""" pyplots.ai
2+
line-confidence: Line Plot with Confidence Interval
3+
Library: pygal 3.1.0 | Python 3.13.11
4+
Quality: 58/100 | Created: 2025-12-26
5+
"""
6+
7+
import numpy as np
8+
import pygal
9+
from pygal.style import Style
10+
11+
12+
# Data - Model predictions with 95% confidence interval
13+
np.random.seed(42)
14+
15+
# Time points (e.g., weeks)
16+
x = np.linspace(0, 12, 50)
17+
18+
# Central prediction: exponential growth pattern (e.g., user growth model)
19+
y_center = 1000 + 500 * (1 - np.exp(-0.3 * x)) + np.random.randn(50) * 20
20+
# Smooth the center line
21+
y_center = np.convolve(y_center, np.ones(5) / 5, mode="same")
22+
y_center[0:2] = y_center[2] # Fix edge effects from convolution
23+
y_center[-2:] = y_center[-3]
24+
25+
# Confidence interval widens over time (increasing uncertainty in predictions)
26+
uncertainty = 30 + 15 * x
27+
y_lower = y_center - uncertainty
28+
y_upper = y_center + uncertainty
29+
30+
# Custom style for 4800x2700 canvas
31+
custom_style = Style(
32+
background="white",
33+
plot_background="white",
34+
foreground="#333333",
35+
foreground_strong="#333333",
36+
foreground_subtle="#666666",
37+
guide_stroke_color="#cccccc",
38+
colors=(
39+
"rgba(48, 105, 152, 0.25)", # Semi-transparent blue for confidence band
40+
"#306998", # Solid dark blue for predicted mean line
41+
),
42+
opacity="1",
43+
opacity_hover="1",
44+
title_font_size=60,
45+
label_font_size=42,
46+
major_label_font_size=42,
47+
legend_font_size=42,
48+
value_font_size=36,
49+
)
50+
51+
# Create XY chart
52+
chart = pygal.XY(
53+
style=custom_style,
54+
width=4800,
55+
height=2700,
56+
title="line-confidence · pygal · pyplots.ai",
57+
x_title="Time (weeks)",
58+
y_title="Predicted Users",
59+
show_dots=False,
60+
show_x_guides=True,
61+
show_y_guides=True,
62+
fill=True,
63+
legend_at_bottom=True,
64+
truncate_legend=-1,
65+
range=(float(y_lower.min() - 50), float(y_upper.max() + 50)),
66+
)
67+
68+
# Create confidence band as a closed polygon:
69+
# Trace upper bound left-to-right, then lower bound right-to-left
70+
# This forms a closed shape that pygal can fill
71+
confidence_band = []
72+
73+
# Forward pass: upper bound (left to right)
74+
for xi, yi in zip(x, y_upper, strict=True):
75+
confidence_band.append((float(xi), float(yi)))
76+
77+
# Backward pass: lower bound (right to left) - closes the polygon
78+
for xi, yi in zip(reversed(x), reversed(y_lower), strict=True):
79+
confidence_band.append((float(xi), float(yi)))
80+
81+
# Center line data
82+
center_data = [(float(xi), float(yi)) for xi, yi in zip(x, y_center, strict=True)]
83+
84+
# Add confidence band (filled polygon)
85+
chart.add("95% Confidence Interval", confidence_band, show_dots=False, stroke=False)
86+
87+
# Add center line (solid stroke, no fill)
88+
chart.add("Predicted Mean", center_data, fill=False, stroke=True, show_dots=False, stroke_style={"width": 6})
89+
90+
# Save outputs
91+
chart.render_to_png("plot.png")
92+
chart.render_to_file("plot.html")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
library: pygal
2+
specification_id: line-confidence
3+
created: '2025-12-26T16:13:44Z'
4+
updated: '2025-12-26T16:36:56Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20525435948
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/line-confidence/pygal/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/line-confidence/pygal/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/line-confidence/pygal/plot.html
13+
quality_score: 58
14+
review:
15+
strengths:
16+
- Clean KISS code structure with proper random seed for reproducibility
17+
- Correct title format following pyplots.ai convention
18+
- Realistic user growth prediction scenario with appropriate data scale
19+
- Good use of pygal XY chart and custom Style configuration
20+
- Proper axis labels and grid configuration
21+
weaknesses:
22+
- Confidence interval band is nearly invisible - the filled polygon approach with
23+
rgba opacity renders as an almost imperceptible thin region rather than a clearly
24+
visible shaded band
25+
- Legend claims 95% Confidence Interval but users cannot actually see the band,
26+
making the visualization misleading
27+
- Pygal fill mechanism for polygons may not be suitable for confidence bands - library
28+
limitation for this visualization type

0 commit comments

Comments
 (0)