{"spec_id":"line-impurity-comparison","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nline-impurity-comparison: Gini Impurity vs Entropy Comparison\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-29\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove this script's directory from sys.path so \"matplotlib\" resolves to the\n# installed package rather than this file (which shares its name).\n_here = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p) != _here and p != \"\"]\n\nimport matplotlib.patheffects as pe\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\n# Theme tokens (Imprint palette — see prompts/default-style-guide.md)\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\nIMPRINT_PALETTE = [\n    \"#009E73\",  # brand green — always first series\n    \"#C475FD\",  # lavender\n    \"#4467A3\",  # blue\n    \"#BD8233\",  # ochre\n    \"#AE3030\",  # matte red\n    \"#2ABCCD\",  # cyan\n    \"#954477\",  # rose\n    \"#99B314\",  # lime\n]\n\n# Data\np = np.linspace(0, 1, 200)\ngini = 2 * p * (1 - p)\n\nentropy = np.zeros_like(p)\nmask = (p > 0) & (p < 1)\nentropy[mask] = -p[mask] * np.log2(p[mask]) - (1 - p[mask]) * np.log2(1 - p[mask])\n\ngini_at_half = 2 * 0.5 * 0.5  # 0.5 at p=0.5\nentropy_at_half = 1.0  # 1.0 at p=0.5\n\n# Plot\ntitle = \"line-impurity-comparison · python · matplotlib · anyplot.ai\"\ntitle_fontsize = max(8, round(12 * 67 / len(title))) if len(title) > 67 else 12\n\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Shaded difference region between curves\nax.fill_between(p, gini, entropy, alpha=0.07, color=INK_MUTED)\n\nhalo = [pe.Stroke(linewidth=4, foreground=PAGE_BG), pe.Normal()]\n\nax.plot(p, gini, linewidth=2.5, color=IMPRINT_PALETTE[0], label=\"Gini Impurity:  $2p(1-p)$\", path_effects=halo)\nax.plot(\n    p,\n    entropy,\n    linewidth=2.5,\n    color=IMPRINT_PALETTE[1],\n    linestyle=\"--\",\n    label=r\"Entropy (norm.):  $-p\\,\\log_2 p - (1{-}p)\\,\\log_2(1{-}p)$\",\n    path_effects=halo,\n)\n\n# Maxima markers at p=0.5\nax.plot(\n    0.5,\n    gini_at_half,\n    \"o\",\n    color=IMPRINT_PALETTE[0],\n    markersize=8,\n    zorder=5,\n    markeredgecolor=PAGE_BG,\n    markeredgewidth=1.5,\n)\nax.plot(\n    0.5,\n    entropy_at_half,\n    \"o\",\n    color=IMPRINT_PALETTE[1],\n    markersize=8,\n    zorder=5,\n    markeredgecolor=PAGE_BG,\n    markeredgewidth=1.5,\n)\n\n# Annotations — spec requests annotating the maximum impurity at p=0.5\nax.annotate(\n    \"Entropy max = 1.0\\n$p = 0.5$\",\n    xy=(0.5, entropy_at_half),\n    xytext=(0.15, 0.86),\n    fontsize=7,\n    color=IMPRINT_PALETTE[1],\n    fontweight=\"medium\",\n    arrowprops={\"arrowstyle\": \"->\", \"color\": IMPRINT_PALETTE[1], \"lw\": 1.2, \"connectionstyle\": \"arc3,rad=-0.15\"},\n    ha=\"center\",\n    va=\"top\",\n)\nax.annotate(\n    \"Gini max = 0.5\\n$p = 0.5$\",\n    xy=(0.5, gini_at_half),\n    xytext=(0.78, 0.40),\n    fontsize=7,\n    color=IMPRINT_PALETTE[0],\n    fontweight=\"medium\",\n    arrowprops={\"arrowstyle\": \"->\", \"color\": IMPRINT_PALETTE[0], \"lw\": 1.2, \"connectionstyle\": \"arc3,rad=0.15\"},\n    ha=\"center\",\n    va=\"bottom\",\n)\n\n# Reference line at maximum impurity point\nax.axvline(x=0.5, color=INK_MUTED, linestyle=\":\", linewidth=1.0, zorder=1)\nax.text(0.5, 0.04, \"Maximum impurity\", fontsize=7, color=INK_MUTED, ha=\"center\", va=\"bottom\", fontstyle=\"italic\")\n\n# Style\nax.set_xlabel(\"Probability of Class 1 ($p$)\", fontsize=10, color=INK)\nax.set_ylabel(\"Impurity (normalized)\", fontsize=10, color=INK)\nax.set_title(title, fontsize=title_fontsize, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_color(INK_SOFT)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\nax.xaxis.grid(True, alpha=0.08, linewidth=0.5, color=INK)\nax.set_xlim(-0.02, 1.02)\nax.set_ylim(-0.03, 1.08)\n\nleg = ax.legend(fontsize=8, loc=\"upper left\")\nif leg:\n    leg.get_frame().set_facecolor(ELEVATED_BG)\n    leg.get_frame().set_edgecolor(INK_SOFT)\n    plt.setp(leg.get_texts(), color=INK_SOFT)\n\nfig.subplots_adjust(left=0.09, right=0.97, top=0.93, bottom=0.13)\n\n# Save\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}