Skip to content

Commit eba9400

Browse files
feat(seaborn): implement elbow-curve (#2351)
## Implementation: `elbow-curve` - seaborn Implements the **seaborn** version of `elbow-curve`. **File:** `plots/elbow-curve/implementations/seaborn.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20528202504)* --------- 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 71ae7e5 commit eba9400

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
""" pyplots.ai
2+
elbow-curve: Elbow Curve for K-Means Clustering
3+
Library: seaborn 0.13.2 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-26
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
import seaborn as sns
10+
11+
12+
# Set seaborn style
13+
sns.set_style("whitegrid")
14+
15+
# Simulate realistic elbow curve data
16+
# Inertia decreases with more clusters, with an "elbow" at k=4
17+
np.random.seed(42)
18+
k_values = list(range(1, 11))
19+
20+
# Realistic inertia values showing typical elbow pattern
21+
# High inertia at k=1, sharp decrease until k=4, then diminishing returns
22+
inertias = [
23+
2800, # k=1
24+
1650, # k=2
25+
950, # k=3
26+
520, # k=4 - elbow point
27+
450, # k=5
28+
400, # k=6
29+
365, # k=7
30+
340, # k=8
31+
320, # k=9
32+
305, # k=10
33+
]
34+
35+
# Add small noise for realism
36+
noise = np.random.uniform(-10, 10, len(inertias))
37+
inertias = [max(0, i + n) for i, n in zip(inertias, noise, strict=True)]
38+
39+
# Create figure
40+
fig, ax = plt.subplots(figsize=(16, 9))
41+
42+
# Plot the elbow curve using seaborn lineplot
43+
sns.lineplot(
44+
x=k_values,
45+
y=inertias,
46+
ax=ax,
47+
color="#306998",
48+
linewidth=3,
49+
marker="o",
50+
markersize=15,
51+
markerfacecolor="#FFD43B",
52+
markeredgecolor="#306998",
53+
markeredgewidth=2,
54+
)
55+
56+
# Annotate the elbow point (k=4)
57+
elbow_k = 4
58+
elbow_inertia = inertias[elbow_k - 1]
59+
ax.annotate(
60+
f"Elbow Point (k={elbow_k})",
61+
xy=(elbow_k, elbow_inertia),
62+
xytext=(elbow_k + 2, elbow_inertia + 400),
63+
fontsize=18,
64+
arrowprops={"arrowstyle": "->", "color": "#306998", "lw": 2},
65+
color="#306998",
66+
fontweight="bold",
67+
)
68+
69+
# Labels and styling
70+
ax.set_xlabel("Number of Clusters (k)", fontsize=20)
71+
ax.set_ylabel("Inertia (Within-Cluster Sum of Squares)", fontsize=20)
72+
ax.set_title("elbow-curve · seaborn · pyplots.ai", fontsize=24)
73+
ax.tick_params(axis="both", labelsize=16)
74+
ax.set_xticks(k_values)
75+
76+
# Subtle grid
77+
ax.grid(True, alpha=0.3, linestyle="--")
78+
79+
plt.tight_layout()
80+
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
library: seaborn
2+
specification_id: elbow-curve
3+
created: '2025-12-26T19:36:40Z'
4+
updated: '2025-12-26T19:42:53Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20528202504
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 0.13.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/seaborn/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/seaborn/plot_thumb.png
12+
preview_html: null
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Clear elbow point annotation with arrow makes the key insight immediately visible
17+
- Excellent color scheme with Python-themed blue (#306998) and yellow (#FFD43B)
18+
markers
19+
- Proper font sizing throughout (24pt title, 20pt labels, 16pt ticks)
20+
- Realistic inertia values showing typical K-means clustering behavior
21+
- Well-structured KISS code following project guidelines
22+
weaknesses:
23+
- Uses sns.lineplot which is basic; could leverage seaborn statistical features
24+
more distinctively
25+
- Y-axis label could include units in parentheses format even if unitless (e.g.,
26+
Inertia (WCSS))

0 commit comments

Comments
 (0)