{"spec_id":"elbow-curve","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nelbow-curve: Elbow Curve for K-Means Clustering\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-10\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nBRAND = \"#009E73\"\nACCENT = \"#C475FD\"\n\n# Configure seaborn theme with theme-adaptive colors\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Simulate realistic elbow curve data\nnp.random.seed(42)\nk_values = list(range(1, 11))\n\n# Realistic inertia values showing typical elbow pattern\ninertias = [\n    2800,  # k=1\n    1650,  # k=2\n    950,  # k=3\n    520,  # k=4 - elbow point\n    450,  # k=5\n    400,  # k=6\n    365,  # k=7\n    340,  # k=8\n    320,  # k=9\n    305,  # k=10\n]\n\n# Add small noise for realism\nnoise = np.random.uniform(-10, 10, len(inertias))\ninertias = [max(0, i + n) for i, n in zip(inertias, noise, strict=True)]\n\n# Create figure and plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Plot the elbow curve with seaborn\nsns.lineplot(\n    x=k_values,\n    y=inertias,\n    ax=ax,\n    color=BRAND,\n    linewidth=3.5,\n    marker=\"o\",\n    markersize=16,\n    markerfacecolor=ACCENT,\n    markeredgecolor=BRAND,\n    markeredgewidth=2.5,\n)\n\n# Annotate the elbow point (k=4)\nelbow_k = 4\nelbow_inertia = inertias[elbow_k - 1]\nax.annotate(\n    f\"Elbow Point (k={elbow_k})\",\n    xy=(elbow_k, elbow_inertia),\n    xytext=(elbow_k + 2, elbow_inertia + 400),\n    fontsize=18,\n    arrowprops={\"arrowstyle\": \"->\", \"color\": INK, \"lw\": 2},\n    color=INK,\n    fontweight=\"bold\",\n)\n\n# Labels and styling\nax.set_xlabel(\"Number of Clusters (k)\", fontsize=20, color=INK)\nax.set_ylabel(\"Inertia (Within-Cluster Sum of Squares)\", fontsize=20, color=INK)\nax.set_title(\"elbow-curve · seaborn · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\nax.set_xticks(k_values)\n\n# Spine styling\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor spine in (\"left\", \"bottom\"):\n    ax.spines[spine].set_color(INK_SOFT)\n\n# Subtle y-axis grid\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}