Skip to content

Commit f9a7581

Browse files
feat(pygal): implement elbow-curve (#2372)
## Implementation: `elbow-curve` - pygal Implements the **pygal** version of `elbow-curve`. **File:** `plots/elbow-curve/implementations/pygal.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20528466148)* --------- 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 9d19fe8 commit f9a7581

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
""" pyplots.ai
2+
elbow-curve: Elbow Curve for K-Means Clustering
3+
Library: pygal 3.1.0 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-26
5+
"""
6+
7+
import pygal
8+
from pygal.style import Style
9+
10+
11+
# Simulated K-means inertia data showing clear elbow at k=4
12+
# Represents clustering analysis on customer segmentation dataset
13+
k_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
14+
inertias = [4200, 2100, 1200, 680, 580, 510, 460, 420, 390, 365]
15+
16+
# Custom style for pyplots
17+
custom_style = Style(
18+
background="white",
19+
plot_background="white",
20+
foreground="#333333",
21+
foreground_strong="#333333",
22+
foreground_subtle="#666666",
23+
colors=("#306998", "#FFD43B", "#E74C3C"),
24+
title_font_size=48,
25+
label_font_size=36,
26+
major_label_font_size=32,
27+
legend_font_size=32,
28+
value_font_size=24,
29+
stroke_width=4,
30+
value_label_font_size=24,
31+
tooltip_font_size=24,
32+
)
33+
34+
# Create XY chart for line plot with markers
35+
chart = pygal.XY(
36+
width=4800,
37+
height=2700,
38+
style=custom_style,
39+
title="elbow-curve · pygal · pyplots.ai",
40+
x_title="Number of Clusters (k)",
41+
y_title="Inertia (Within-cluster Sum of Squares)",
42+
show_legend=True,
43+
legend_at_bottom=True,
44+
legend_at_bottom_columns=2,
45+
show_x_guides=False,
46+
show_y_guides=True,
47+
dots_size=12,
48+
stroke_style={"width": 4, "linecap": "round", "linejoin": "round"},
49+
truncate_legend=-1,
50+
x_labels=k_values,
51+
range=(0, max(inertias) * 1.05),
52+
include_x_axis=True,
53+
explicit_size=True,
54+
margin=50,
55+
spacing=30,
56+
)
57+
58+
# Prepare data as (x, y) tuples
59+
elbow_data = [(k, inertia) for k, inertia in zip(k_values, inertias, strict=True)]
60+
61+
# Add elbow curve data
62+
chart.add("Inertia", elbow_data, stroke_style={"width": 4})
63+
64+
# Highlight optimal elbow point (k=4 based on the generated data)
65+
elbow_k = 4
66+
elbow_inertia = inertias[elbow_k - 1]
67+
chart.add("Optimal k", [(elbow_k, elbow_inertia)], dots_size=20)
68+
69+
# Save as PNG and HTML
70+
chart.render_to_png("plot.png")
71+
chart.render_to_file("plot.html")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library: pygal
2+
specification_id: elbow-curve
3+
created: '2025-12-26T19:57:22Z'
4+
updated: '2025-12-26T19:59:31Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20528466148
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/elbow-curve/pygal/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/pygal/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/pygal/plot.html
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Clear visualization of the elbow curve with excellent readability
17+
- Optimal k value (elbow point) is highlighted with a distinct larger yellow marker
18+
- Proper title format and descriptive axis labels with full context
19+
- Clean KISS code structure following pygal best practices
20+
- Good color contrast between the main line and elbow highlight
21+
- Data realistically demonstrates the diminishing returns pattern
22+
weaknesses:
23+
- Legend positioned quite far from the plot area at the bottom
24+
- Could use pygal built-in annotation or tooltip features to enhance the elbow point
25+
indication

0 commit comments

Comments
 (0)