{"spec_id":"bar-feature-importance","library":"makie","language":"julia","code":"# anyplot.ai\n# bar-feature-importance: Feature Importance Bar Chart\n# Library: makie 0.21.9 | Julia 1.11.9\n# Quality: 94/100 | Created: 2026-09-02\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 INK      = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\n\n# Imprint sequential colormap (brand green -> blue) for continuous importance\nconst ANYPLOT_SEQ = cgrad([colorant\"#009E73\", colorant\"#4467A3\"])\n\n# --- Data -----------------------------------------------------------------\n# Feature importances from a random forest customer-churn model\nfeature_names = [\n    \"Contract Type\", \"Tenure (Months)\", \"Monthly Charges\", \"Tech Support\",\n    \"Total Charges\", \"Internet Service\", \"Online Security\", \"Payment Method\",\n    \"Paperless Billing\", \"Device Protection\", \"Streaming TV\", \"Multiple Lines\",\n    \"Senior Citizen\", \"Partner\",\n]\nn_features = length(feature_names)\n\nraw_importance = [0.184, 0.151, 0.132, 0.098, 0.087, 0.071, 0.062, 0.051,\n                   0.042, 0.036, 0.029, 0.022, 0.019, 0.016]\nimportance = raw_importance ./ sum(raw_importance)\nstd_dev = importance .* (0.12 .+ 0.10 .* rand(n_features))\n\n# Sort ascending so the highest importance lands at the top of the chart\norder = sortperm(importance)\nfeature_names = feature_names[order]\nimportance = importance[order]\nstd_dev = std_dev[order]\npositions = 1:n_features\n\n# Bolder outline on the top driver (highest importance, last row) as a callout\nstroke_widths = [i == n_features ? 2.5 : 0.0 for i in 1:n_features]\n\n# --- Plot -------------------------------------------------------------------\nfig = Figure(\n    size            = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title             = \"bar-feature-importance · julia · makie · anyplot.ai\",\n    titlesize         = 24,\n    titlecolor        = INK,\n    xlabel            = \"Relative Importance\",\n    xlabelsize        = 14,\n    xlabelcolor       = INK,\n    ylabelcolor       = INK,\n    xticklabelsize    = 12,\n    yticklabelsize    = 12,\n    xticklabelcolor   = INK_SOFT,\n    yticklabelcolor   = INK_SOFT,\n    xtickformat       = xs -> [string(round(Int, x * 100), \"%\") for x in xs],\n    backgroundcolor   = PAGE_BG,\n    topspinevisible   = false,\n    rightspinevisible = false,\n    leftspinecolor    = INK_SOFT,\n    bottomspinecolor  = INK_SOFT,\n    xgridcolor        = RGBAf(INK.r, INK.g, INK.b, 0.15),\n    ygridvisible      = false,\n    yticks            = (positions, feature_names),\n)\n\nbarplot!(\n    ax, positions, importance;\n    direction   = :x,\n    color       = importance,\n    colormap    = ANYPLOT_SEQ,\n    colorrange  = (minimum(importance), maximum(importance)),\n    strokewidth = stroke_widths,\n    strokecolor = INK,\n)\n\nerrorbars!(\n    ax, importance, positions, std_dev;\n    direction    = :x,\n    color        = INK_SOFT,\n    whiskerwidth = 8,\n    linewidth    = 1.5,\n)\n\ntext!(\n    ax, importance .+ std_dev .+ 0.010, positions;\n    text     = [string(round(v * 100, digits = 1), \"%\") for v in importance],\n    align    = (:left, :center),\n    color    = INK,\n    fontsize = 12,\n)\n\nxlims!(ax, 0, maximum(importance .+ std_dev) * 1.15)\n\nColorbar(\n    fig[1, 2];\n    colormap       = ANYPLOT_SEQ,\n    colorrange     = (minimum(importance), maximum(importance)),\n    label          = \"Relative Importance\",\n    labelcolor     = INK,\n    ticklabelcolor = INK_SOFT,\n    tickcolor      = INK_SOFT,\n    tickformat     = xs -> [string(round(Int, x * 100), \"%\") for x in xs],\n    width          = 18,\n)\ncolsize!(fig.layout, 2, Fixed(90))\n\n# --- Save -------------------------------------------------------------------\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}