{"spec_id":"line-impurity-comparison","library":"makie","language":"julia","code":"# anyplot.ai\n# line-impurity-comparison: Gini Impurity vs Entropy Comparison\n# Library: makie 0.22.10 | Julia 1.11.9\n# Quality: 87/100 | Created: 2026-05-29\n\nusing CairoMakie\nusing Colors\nusing Random\n\nRandom.seed!(42)\n\n# Theme tokens\nconst THEME       = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG     = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst ELEVATED_BG = THEME == \"light\" ? colorant\"#FFFDF6\" : colorant\"#242420\"\nconst INK         = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT    = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\nconst INK_MUTED   = THEME == \"light\" ? colorant\"#6B6A63\" : colorant\"#A8A79F\"\n\nconst IMPRINT_PALETTE = [\n    colorant\"#009E73\",\n    colorant\"#C475FD\",\n    colorant\"#4467A3\",\n    colorant\"#BD8233\",\n    colorant\"#AE3030\",\n    colorant\"#2ABCCD\",\n    colorant\"#954477\",\n    colorant\"#99B314\",\n]\n\n# Data — probability range, Gini impurity, and normalized binary entropy\np = range(0.0, 1.0; length = 100)\n\ngini_raw = 2 .* p .* (1 .- p)\ngini     = gini_raw ./ maximum(gini_raw)\n\nentropy_raw = [q <= 0.0 || q >= 1.0 ? 0.0 : -q * log2(q) - (1 - q) * log2(1 - q) for q in p]\nentropy     = entropy_raw ./ maximum(entropy_raw)\n\n# Plot\ntitle_str = \"Gini vs Entropy · line-impurity-comparison · julia · makie · anyplot.ai\"\ntitle_n   = length(title_str)\ntitle_sz  = title_n > 67 ? round(Int, 20 * 67 / title_n) : 20\n\nfig = Figure(\n    size            = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title              = title_str,\n    titlesize          = title_sz,\n    titlecolor         = INK,\n    xlabel             = \"Probability p\",\n    ylabel             = \"Impurity (normalized to [0, 1])\",\n    xlabelsize         = 14,\n    ylabelsize         = 14,\n    xticklabelsize     = 12,\n    yticklabelsize     = 12,\n    xlabelcolor        = INK,\n    ylabelcolor        = INK,\n    xticklabelcolor    = INK_SOFT,\n    yticklabelcolor    = INK_SOFT,\n    xtickcolor         = INK_SOFT,\n    ytickcolor         = INK_SOFT,\n    backgroundcolor    = PAGE_BG,\n    topspinevisible    = false,\n    rightspinevisible  = false,\n    leftspinecolor     = INK_SOFT,\n    bottomspinecolor   = INK_SOFT,\n    xgridvisible       = false,\n    ygridvisible       = true,\n    ygridcolor         = RGBAf(INK.r, INK.g, INK.b, 0.15),\n    xminorgridvisible  = false,\n    yminorgridvisible  = false,\n)\n\nband!(ax, collect(p), gini, entropy;\n    color = (IMPRINT_PALETTE[3], 0.12))\n\nlines!(ax, collect(p), gini;\n    color     = IMPRINT_PALETTE[1],\n    linewidth = 3.0,\n    label     = \"Gini:  2p(1−p)  [normalized]\")\n\nlines!(ax, collect(p), entropy;\n    color     = IMPRINT_PALETTE[2],\n    linewidth = 3.0,\n    linestyle = :dash,\n    label     = \"Entropy:  −p log₂p − (1−p) log₂(1−p)\")\n\n# Annotate maximum impurity point at p = 0.5 (spec requirement)\nvlines!(ax, [0.5];\n    color     = INK_MUTED,\n    linewidth = 1.5,\n    linestyle = :dot)\n\ntext!(ax, 0.52, 0.97;\n    text     = \"max at p = 0.5\",\n    color    = INK_MUTED,\n    fontsize = 13,\n    align    = (:left, :top))\n\nLegend(fig[1, 2], ax;\n    framecolor      = RGBAf(INK_SOFT.r, INK_SOFT.g, INK_SOFT.b, 0.4f0),\n    backgroundcolor = ELEVATED_BG,\n    labelcolor      = INK)\n\n# Save\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}