{"spec_id":"bar-permutation-importance","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\nbar-permutation-importance: Permutation Feature Importance Plot\nLibrary: pygal 3.1.0 | Python 3.13.13\nQuality: 86/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\nimport sys\n\nimport numpy as np\n\n\n# Temporarily remove current directory from path to avoid name collision with pygal module\n_cwd = sys.path[0] if sys.path[0] else \".\"\nif _cwd in sys.path:\n    sys.path.remove(_cwd)\n\nimport pygal\nfrom pygal.style import Style\n\n\n# Restore path\nsys.path.insert(0, _cwd)\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Data - Simulated permutation importance results\nnp.random.seed(42)\n\nfeatures = [\n    \"Year Built\",\n    \"Bathrooms\",\n    \"Garage Size\",\n    \"Lot Area\",\n    \"Bedrooms\",\n    \"Basement Area\",\n    \"Total Rooms\",\n    \"Living Area\",\n    \"Neighborhood\",\n    \"Overall Quality\",\n]\n\nimportance_mean = np.array([0.002, 0.008, 0.015, 0.024, 0.032, 0.048, 0.067, 0.095, 0.128, 0.185])\nimportance_std = np.array([0.003, 0.005, 0.008, 0.011, 0.014, 0.018, 0.022, 0.028, 0.035, 0.042])\n\n# Generate viridis color gradient for importance values\n# Inline color generation without helper function\nmin_imp = importance_mean.min()\nmax_imp = importance_mean.max()\nimp_range = max_imp - min_imp if max_imp != min_imp else 1.0\nviridis_stops = [(0.0, 68, 1, 84), (0.25, 58, 82, 139), (0.5, 32, 144, 140), (0.75, 94, 201, 97), (1.0, 253, 231, 36)]\n\nbar_colors = []\nfor imp in importance_mean:\n    t = (imp - min_imp) / imp_range\n    for j in range(len(viridis_stops) - 1):\n        t0, r0, g0, b0 = viridis_stops[j]\n        t1, r1, g1, b1 = viridis_stops[j + 1]\n        if t0 <= t <= t1:\n            seg_t = (t - t0) / (t1 - t0)\n            r = int(r0 + (r1 - r0) * seg_t)\n            g = int(g0 + (g1 - g0) * seg_t)\n            b = int(b0 + (b1 - b0) * seg_t)\n            bar_colors.append(f\"#{r:02x}{g:02x}{b:02x}\")\n            break\n    else:\n        bar_colors.append(\"#fde724\")\n\n# Custom style with theme-adaptive colors and large fonts\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_SOFT,\n    colors=tuple(bar_colors),\n    title_font_size=28,\n    label_font_size=22,\n    major_label_font_size=18,\n    legend_font_size=16,\n    value_font_size=14,\n    stroke_width=3,\n)\n\n# Create horizontal bar chart\nchart = pygal.HorizontalBar(\n    width=4800,\n    height=2700,\n    style=custom_style,\n    title=\"bar-permutation-importance · pygal · anyplot.ai\",\n    x_title=\"Mean Decrease in R² Score\",\n    show_legend=False,\n    print_values=True,\n    print_values_position=\"top\",\n    show_y_guides=False,\n    show_x_guides=True,\n    range=(0, importance_mean.max() + importance_std.max() + 0.02),\n    margin_bottom=120,\n    margin_left=360,\n    margin_right=80,\n    margin_top=80,\n)\n\n# Set feature labels\nchart.x_labels = features\n\n# Add mean importance bars with viridis gradient colors\nchart.add(\n    \"Importance\",\n    [\n        {\"value\": mean, \"color\": color, \"xlink\": {\"href\": \"#\"}, \"label\": f\"Mean: {mean:.3f} ± {std:.3f}\"}\n        for mean, std, color in zip(importance_mean, importance_std, bar_colors, strict=True)\n    ],\n    formatter=lambda x: f\"{x:.3f}\" if x else \"\",\n)\n\n# Save outputs\nchart.render_to_file(f\"plot-{THEME}.html\")\nchart.render_to_png(f\"plot-{THEME}.png\")\n"}