Skip to content

Commit baa1264

Browse files
feat(letsplot): implement elbow-curve (#2353)
## Implementation: `elbow-curve` - letsplot Implements the **letsplot** version of `elbow-curve`. **File:** `plots/elbow-curve/implementations/letsplot.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20528205832)* --------- 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 91ef306 commit baa1264

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
""" pyplots.ai
2+
elbow-curve: Elbow Curve for K-Means Clustering
3+
Library: letsplot 4.8.2 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-26
5+
"""
6+
7+
import numpy as np
8+
import pandas as pd
9+
from lets_plot import *
10+
11+
12+
LetsPlot.setup_html()
13+
14+
# Simulated K-means inertia values showing typical elbow curve pattern
15+
# Data represents clustering analysis on customer segmentation dataset
16+
np.random.seed(42)
17+
18+
k_values = list(range(1, 11))
19+
20+
# Realistic inertia values that show clear elbow at k=4
21+
# Inertia decreases sharply until k=4, then diminishing returns
22+
inertias = [
23+
12500, # k=1: all points in one cluster
24+
6800, # k=2: significant drop
25+
3900, # k=3: still improving
26+
2100, # k=4: elbow point (optimal)
27+
1800, # k=5: diminishing returns start
28+
1550, # k=6
29+
1380, # k=7
30+
1250, # k=8
31+
1150, # k=9
32+
1080, # k=10
33+
]
34+
35+
# Create DataFrame for plotting
36+
df = pd.DataFrame({"k": k_values, "Inertia": inertias})
37+
38+
# Optimal k (elbow point)
39+
optimal_k = 4
40+
41+
# Create elbow curve plot
42+
plot = (
43+
ggplot(df, aes(x="k", y="Inertia"))
44+
+ geom_line(size=2, color="#306998")
45+
+ geom_point(size=6, color="#306998", alpha=0.9)
46+
+ geom_point(data=df[df["k"] == optimal_k], mapping=aes(x="k", y="Inertia"), size=10, color="#FFD43B", shape=18)
47+
+ geom_vline(xintercept=optimal_k, linetype="dashed", color="#FFD43B", size=1.5, alpha=0.7)
48+
+ labs(
49+
title="elbow-curve · letsplot · pyplots.ai",
50+
x="Number of Clusters (k)",
51+
y="Inertia (Within-Cluster Sum of Squares)",
52+
)
53+
+ scale_x_continuous(breaks=k_values)
54+
+ theme_minimal()
55+
+ theme(
56+
plot_title=element_text(size=28, face="bold"),
57+
axis_title=element_text(size=22),
58+
axis_text=element_text(size=18),
59+
panel_grid_major=element_line(color="#CCCCCC", size=0.5),
60+
panel_grid_minor=element_blank(),
61+
)
62+
+ ggsize(1600, 900)
63+
)
64+
65+
# Save as PNG (scale 3x for 4800x2700)
66+
ggsave(plot, "plot.png", path=".", scale=3)
67+
68+
# Save interactive HTML
69+
ggsave(plot, "plot.html", path=".")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
library: letsplot
2+
specification_id: elbow-curve
3+
created: '2025-12-26T19:36:50Z'
4+
updated: '2025-12-26T19:43:18Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20528205832
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 4.8.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/letsplot/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/letsplot/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/letsplot/plot.html
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent visual clarity with properly scaled text elements for 4800x2700 resolution
17+
- Clear elbow point highlighting using contrasting yellow diamond marker and vertical
18+
dashed line
19+
- Proper implementation of lets-plot grammar of graphics with layered geoms
20+
- Clean KISS code structure following project guidelines
21+
- Correct title format and descriptive axis labels
22+
weaknesses:
23+
- No legend or annotation text explaining what the yellow marker/line represents
24+
(the optimal k value)
25+
- Could use lets-plot interactive features like tooltips showing exact values on
26+
hover
27+
- Grid lines could be slightly more subtle (alpha currently at 0.5, could be 0.3)

0 commit comments

Comments
 (0)