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

import numpy as np
import pandas as pd
from plotnine import (
aes,
annotate,
element_line,
element_text,
geom_line,
geom_point,
geom_vline,
ggplot,
labs,
theme,
theme_minimal,
)


# Data - Simulate realistic K-means inertia values
# Inertia decreases as k increases, with diminishing returns after optimal k
np.random.seed(42)

k_values = list(range(1, 11))

# Simulate inertia values that show clear elbow at k=4
# Formula: base decay curve + noise
base_inertias = [1000, 500, 280, 150, 120, 100, 85, 75, 68, 62]
noise = np.random.uniform(-5, 5, len(k_values))
inertias = [max(10, base + n) for base, n in zip(base_inertias, noise, strict=True)]

# Create DataFrame for plotting
df = pd.DataFrame({"k": k_values, "inertia": inertias})

# Optimal k (elbow point)
optimal_k = 4

# Plot
plot = (
ggplot(df, aes(x="k", y="inertia"))
+ geom_line(color="#306998", size=2, alpha=0.9)
+ geom_point(color="#306998", size=5, alpha=1.0)
+ geom_vline(xintercept=optimal_k, linetype="dashed", color="#FFD43B", size=1.5, alpha=0.8)
+ annotate(
"text",
x=optimal_k + 0.5,
y=inertias[optimal_k - 1] + 80,
label=f"Optimal k = {optimal_k}",
size=14,
color="#FFD43B",
ha="left",
fontweight="bold",
)
+ labs(
title="elbow-curve · plotnine · pyplots.ai",
x="Number of Clusters (k)",
y="Inertia (Within-Cluster Sum of Squares)",
)
+ theme_minimal()
+ theme(
figure_size=(16, 9),
plot_title=element_text(size=24, weight="bold"),
axis_title=element_text(size=20),
axis_text=element_text(size=16),
panel_grid_major=element_line(color="#CCCCCC", size=0.5, alpha=0.3),
)
)

# Save
plot.save("plot.png", dpi=300, verbose=False)
28 changes: 28 additions & 0 deletions plots/elbow-curve/metadata/plotnine.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
library: plotnine
specification_id: elbow-curve
created: '2025-12-26T19:37:41Z'
updated: '2025-12-26T19:44:27Z'
generated_by: claude-opus-4-5-20251101
workflow_run: 20528204090
issue: 0
python_version: 3.13.11
library_version: 0.15.2
preview_url: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/plotnine/plot.png
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/elbow-curve/plotnine/plot_thumb.png
preview_html: null
quality_score: 91
review:
strengths:
- Clear visual hierarchy with excellent text sizing at all levels (title, axis labels,
ticks)
- 'Effective use of plotnine grammar of graphics: layered geoms, annotations, and
theme customization'
- The yellow dashed vertical line with annotation clearly highlights the optimal
k value
- Clean, professional appearance using theme_minimal with subtle grid
- Good color contrast between the blue data line and yellow annotation
weaknesses:
- X-axis shows decimal values (2.5, 5.0, 7.5, 10.0) instead of integers (1, 2, 3,
..., 10) which would be more appropriate for discrete k values
- The realistic context is somewhat generic - could use a more specific domain scenario
in comments