|
| 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") |
0 commit comments