{"spec_id":"bar-feature-importance","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\nbar-feature-importance: Feature Importance Bar Chart\nLibrary: pygal 3.1.0 | Python 3.13.13\nQuality: 88/100 | Updated: 2026-05-10\n\"\"\"\n\nimport colorsys\nimport os\nimport sys\n\n\n# Avoid shadowing the pygal package by temporarily removing this directory from path during import\n_original_path = sys.path.copy()\nsys.path = [\n    p\n    for p in sys.path\n    if not p.endswith((\"implementations/python\", \"plots/bar-feature-importance/implementations/python\"))\n]\n\ntry:\n    import pygal\n    from pygal.style import Style\nfinally:\n    sys.path = _original_path\n\n# Theme tokens from prompts/default-style-guide.md\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nBRAND = \"#009E73\"\n\n# Data - Feature importances from a customer churn prediction model\nfeatures = [\n    \"ContractLength\",\n    \"MonthlyCharges\",\n    \"TotalCharges\",\n    \"InternetServiceType\",\n    \"TenureMonths\",\n    \"OnlineSecurity\",\n    \"TechSupport\",\n    \"AutomaticPayment\",\n    \"DeviceProtection\",\n    \"StreamingService\",\n    \"PhoneService\",\n]\nimportances = [0.287, 0.231, 0.154, 0.112, 0.087, 0.062, 0.038, 0.019, 0.008, 0.003, 0.001]\n\n# Sort by importance (ascending for pygal HorizontalBar bottom-to-top rendering)\nsorted_pairs = sorted(zip(features, importances, strict=True), key=lambda x: x[1], reverse=False)\nsorted_features = [p[0] for p in sorted_pairs]\nsorted_importances = [p[1] for p in sorted_pairs]\n\n# Color gradient: low to high importance using HSL-based interpolation (more perceptually uniform)\nmin_imp = min(sorted_importances)\nmax_imp = max(sorted_importances)\n\ncolors_list = []\nfor imp in sorted_importances:\n    ratio = (imp - min_imp) / (max_imp - min_imp) if max_imp > min_imp else 0.5\n    # HSL interpolation: light cyan (h=190, s=35%, l=75%) → brand green (#009E73: h=160, s=100%, l=23%)\n    h_start, s_start, light_start = 190, 0.35, 0.75\n    h_end, s_end, light_end = 160, 1.0, 0.23\n    h = (h_start + ratio * (h_end - h_start)) / 360\n    s = s_start + ratio * (s_end - s_start)\n    light = light_start + ratio * (light_end - light_start)\n    r, g, b = colorsys.hls_to_rgb(h, light, s)\n    colors_list.append(f\"#{int(r * 255):02x}{int(g * 255):02x}{int(b * 255):02x}\")\n\n# Custom style for large canvas\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_MUTED,\n    colors=(BRAND,),\n    font_family=\"sans-serif\",\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=16,\n    stroke_width=2,\n)\n\n# Create horizontal bar chart\nchart = pygal.HorizontalBar(\n    width=4800,\n    height=2700,\n    style=custom_style,\n    title=\"bar-feature-importance · pygal · anyplot.ai\",\n    x_title=\"Importance Score\",\n    show_legend=False,\n    show_y_guides=False,\n    show_x_guides=True,\n    print_values=True,\n    print_values_position=\"top\",\n    value_formatter=lambda x: f\"{x:.3f}\",\n    range=(0, max_imp * 1.1),\n    margin=80,\n    spacing=6,\n    truncate_label=-1,\n)\n\n# Set feature names as y-axis labels\nchart.x_labels = sorted_features\n\n# Add data with per-bar colors\nbar_data = [{\"value\": imp, \"color\": color} for imp, color in zip(sorted_importances, colors_list, strict=True)]\nchart.add(\"Importance\", bar_data)\n\n# Save outputs for both themes\nchart.render_to_file(f\"plot-{THEME}.html\")\nchart.render_to_png(f\"plot-{THEME}.png\")\n"}