{"spec_id":"bar-feature-importance","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nbar-feature-importance: Feature Importance Bar Chart\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-10\n\"\"\"\n\nimport os\n\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    coord_flip,\n    element_line,\n    element_rect,\n    element_text,\n    geom_bar,\n    geom_errorbar,\n    geom_text,\n    ggplot,\n    guides,\n    labs,\n    scale_fill_cmap,\n    scale_y_continuous,\n    theme,\n    theme_minimal,\n)\n\n\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 - Feature importances from a Random Forest model for house price prediction\nfeatures = [\n    \"Overall Quality\",\n    \"Living Area (sqft)\",\n    \"Garage Cars\",\n    \"Basement Area (sqft)\",\n    \"Year Built\",\n    \"Full Bathrooms\",\n    \"Total Rooms\",\n    \"Fireplaces\",\n    \"Year Remodeled\",\n    \"Lot Area (sqft)\",\n    \"Kitchen Quality\",\n    \"Garage Area (sqft)\",\n    \"Pool Area\",\n    \"Bedrooms\",\n    \"Porch Area (sqft)\",\n]\n\nimportances = [0.285, 0.198, 0.124, 0.089, 0.072, 0.058, 0.043, 0.032, 0.028, 0.024, 0.019, 0.014, 0.008, 0.004, 0.002]\n\n# Standard deviations for error bars (from ensemble variability)\nstds = [0.018, 0.015, 0.012, 0.009, 0.008, 0.007, 0.006, 0.005, 0.004, 0.004, 0.003, 0.003, 0.002, 0.001, 0.001]\n\ndf = pd.DataFrame({\"feature\": features, \"importance\": importances, \"std\": stds})\n\n# Sort by importance and create ordered categorical for proper display\ndf = df.sort_values(\"importance\", ascending=True)\ndf[\"feature\"] = pd.Categorical(df[\"feature\"], categories=df[\"feature\"], ordered=True)\n\n# Create plot\nplot = (\n    ggplot(df, aes(x=\"feature\", y=\"importance\", fill=\"importance\"))\n    + geom_bar(stat=\"identity\", width=0.7)\n    + geom_errorbar(aes(ymin=\"importance - std\", ymax=\"importance + std\"), width=0.3, color=INK_SOFT, size=0.8)\n    + geom_text(aes(label=\"importance\"), format_string=\"{:.3f}\", nudge_y=0.025, size=12, color=INK, ha=\"left\")\n    + coord_flip()\n    + scale_fill_cmap(cmap_name=\"viridis\")\n    + guides(fill=False)\n    + scale_y_continuous(expand=(0, 0, 0.15, 0))\n    + labs(title=\"bar-feature-importance · plotnine · anyplot.ai\", x=\"Feature\", y=\"Importance Score\")\n    + theme_minimal()\n    + theme(\n        figure_size=(16, 9),\n        text=element_text(size=14, color=INK_SOFT),\n        axis_title=element_text(size=20, color=INK),\n        axis_text=element_text(size=16, color=INK_SOFT),\n        axis_text_y=element_text(size=14, color=INK_SOFT),\n        plot_title=element_text(size=24, color=INK),\n        panel_grid_major_y=element_line(color=INK, size=0.2, alpha=0),\n        panel_grid_minor=element_line(alpha=0),\n        panel_grid_major_x=element_line(color=INK, size=0.3, alpha=0.15),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n    )\n)\n\n# Save plot\nplot.save(f\"plot-{THEME}.png\", dpi=300, verbose=False)\n"}