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
71 changes: 71 additions & 0 deletions plots/elbow-curve/implementations/pygal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
""" pyplots.ai
elbow-curve: Elbow Curve for K-Means Clustering
Library: pygal 3.1.0 | Python 3.13.11
Quality: 91/100 | Created: 2025-12-26
"""

import pygal
from pygal.style import Style


# Simulated K-means inertia data showing clear elbow at k=4
# Represents clustering analysis on customer segmentation dataset
k_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
inertias = [4200, 2100, 1200, 680, 580, 510, 460, 420, 390, 365]

# Custom style for pyplots
custom_style = Style(
background="white",
plot_background="white",
foreground="#333333",
foreground_strong="#333333",
foreground_subtle="#666666",
colors=("#306998", "#FFD43B", "#E74C3C"),
title_font_size=48,
label_font_size=36,
major_label_font_size=32,
legend_font_size=32,
value_font_size=24,
stroke_width=4,
value_label_font_size=24,
tooltip_font_size=24,
)

# Create XY chart for line plot with markers
chart = pygal.XY(
width=4800,
height=2700,
style=custom_style,
title="elbow-curve · pygal · pyplots.ai",
x_title="Number of Clusters (k)",
y_title="Inertia (Within-cluster Sum of Squares)",
show_legend=True,
legend_at_bottom=True,
legend_at_bottom_columns=2,
show_x_guides=False,
show_y_guides=True,
dots_size=12,
stroke_style={"width": 4, "linecap": "round", "linejoin": "round"},
truncate_legend=-1,
x_labels=k_values,
range=(0, max(inertias) * 1.05),
include_x_axis=True,
explicit_size=True,
margin=50,
spacing=30,
)

# Prepare data as (x, y) tuples
elbow_data = [(k, inertia) for k, inertia in zip(k_values, inertias, strict=True)]

# Add elbow curve data
chart.add("Inertia", elbow_data, stroke_style={"width": 4})

# Highlight optimal elbow point (k=4 based on the generated data)
elbow_k = 4
elbow_inertia = inertias[elbow_k - 1]
chart.add("Optimal k", [(elbow_k, elbow_inertia)], dots_size=20)

# Save as PNG and HTML
chart.render_to_png("plot.png")
chart.render_to_file("plot.html")
25 changes: 25 additions & 0 deletions plots/elbow-curve/metadata/pygal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
library: pygal
specification_id: elbow-curve
created: '2025-12-26T19:57:22Z'
updated: '2025-12-26T19:59:31Z'
generated_by: claude-opus-4-5-20251101
workflow_run: 20528466148
issue: 0
python_version: 3.13.11
library_version: 3.1.0
preview_url: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/pygal/plot.png
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/pygal/plot_thumb.png
preview_html: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/pygal/plot.html
quality_score: 91
review:
strengths:
- Clear visualization of the elbow curve with excellent readability
- Optimal k value (elbow point) is highlighted with a distinct larger yellow marker
- Proper title format and descriptive axis labels with full context
- Clean KISS code structure following pygal best practices
- Good color contrast between the main line and elbow highlight
- Data realistically demonstrates the diminishing returns pattern
weaknesses:
- Legend positioned quite far from the plot area at the bottom
- Could use pygal built-in annotation or tooltip features to enhance the elbow point
indication