{"spec_id":"bar-permutation-importance","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nbar-permutation-importance: Permutation Feature Importance Plot\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    coord_flip,\n    element_line,\n    element_rect,\n    element_text,\n    geom_col,\n    geom_errorbar,\n    geom_hline,\n    ggplot,\n    labs,\n    scale_fill_cmap,\n    theme,\n    theme_minimal,\n)\n\n\n# Theme tokens (see prompts/default-style-guide.md \"Background\" + \"Theme-adaptive Chrome\")\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\"\n\n# Data - Simulating permutation importance results from a random forest model\nnp.random.seed(42)\n\n# Feature names representing a customer churn prediction model\nfeatures = [\n    \"Contract Length\",\n    \"Monthly Charges\",\n    \"Total Charges\",\n    \"Tenure (months)\",\n    \"Tech Support Calls\",\n    \"Payment Method\",\n    \"Internet Service Type\",\n    \"Online Security\",\n    \"Streaming Services\",\n    \"Paperless Billing\",\n    \"Number of Dependents\",\n    \"Senior Citizen Status\",\n    \"Partner Status\",\n    \"Phone Service\",\n    \"Multiple Lines\",\n]\n\n# Generate realistic importance values (higher for known predictive features)\nbase_importances = np.array(\n    [0.15, 0.12, 0.10, 0.09, 0.07, 0.05, 0.04, 0.035, 0.03, 0.025, 0.02, 0.015, 0.01, 0.005, -0.002]\n)\nimportance_means = base_importances + np.random.normal(0, 0.005, len(features))\nimportance_stds = np.abs(np.random.normal(0.01, 0.005, len(features)))\n\n# Create DataFrame and sort by importance\ndf = pd.DataFrame({\"feature\": features, \"importance_mean\": importance_means, \"importance_std\": importance_stds})\ndf = df.sort_values(\"importance_mean\", ascending=True).reset_index(drop=True)\n\n# Create ordered categorical for proper sorting in plot\ndf[\"feature\"] = pd.Categorical(df[\"feature\"], categories=df[\"feature\"], ordered=True)\n\n# Calculate error bar positions (ymin/ymax because coord_flip swaps axes)\ndf[\"ymin\"] = df[\"importance_mean\"] - df[\"importance_std\"]\ndf[\"ymax\"] = df[\"importance_mean\"] + df[\"importance_std\"]\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"feature\", y=\"importance_mean\", fill=\"importance_mean\"))\n    + geom_col(width=0.7)\n    + geom_errorbar(aes(ymin=\"ymin\", ymax=\"ymax\"), width=0.3, color=INK_SOFT, size=0.8)\n    + geom_hline(yintercept=0, linetype=\"dashed\", color=INK_SOFT, size=0.7)\n    + coord_flip()\n    + scale_fill_cmap(cmap_name=\"viridis\", name=\"Importance Score\")\n    + labs(x=\"Feature\", y=\"Mean Decrease in Model Score\", title=\"bar-permutation-importance · plotnine · anyplot.ai\")\n    + theme_minimal()\n    + theme(\n        figure_size=(16, 9),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major_y=element_line(alpha=0),\n        panel_grid_minor=element_line(alpha=0),\n        axis_line=element_line(color=INK_SOFT, size=0.3),\n        plot_title=element_text(size=24, weight=\"bold\", color=INK),\n        axis_title_x=element_text(size=20, color=INK),\n        axis_title_y=element_text(size=20, color=INK),\n        axis_text_x=element_text(size=16, color=INK_SOFT),\n        axis_text_y=element_text(size=16, color=INK_SOFT),\n        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n        legend_title=element_text(size=16, color=INK),\n        legend_text=element_text(size=14, color=INK_SOFT),\n    )\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=300, verbose=False)\n"}