{"spec_id":"bar-feature-importance","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nbar-feature-importance: Feature Importance Bar Chart\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 72/100 | Updated: 2026-05-10\n\"\"\"\n\nfrom bokeh.io import export_png, save\nfrom bokeh.models import ColumnDataSource, LabelSet, LinearColorMapper\nfrom bokeh.palettes import Blues9\nfrom bokeh.plotting import figure\nfrom bokeh.resources import CDN\n\n\n# Data: Feature importances from a classification model\nfeatures = [\n    \"Income\",\n    \"Credit Score\",\n    \"Age\",\n    \"Employment Years\",\n    \"Debt Ratio\",\n    \"Num Accounts\",\n    \"Loan Amount\",\n    \"Education Level\",\n    \"Num Inquiries\",\n    \"Home Ownership\",\n    \"Payment History\",\n    \"Account Balance\",\n]\nimportances = [0.185, 0.162, 0.124, 0.098, 0.089, 0.076, 0.068, 0.058, 0.052, 0.041, 0.032, 0.015]\n\n# Sort by importance (highest at top when plotted)\nsorted_pairs = sorted(zip(features, importances, strict=True), key=lambda x: x[1])\nfeatures_sorted = [p[0] for p in sorted_pairs]\nimportances_sorted = [p[1] for p in sorted_pairs]\n\n# Create data source\nsource = ColumnDataSource(\n    data={\n        \"features\": features_sorted,\n        \"importances\": importances_sorted,\n        \"labels\": [f\"{imp:.3f}\" for imp in importances_sorted],\n        \"label_x\": [imp + 0.008 for imp in importances_sorted],  # offset for label placement\n    }\n)\n\n# Color mapper for gradient effect (light to dark based on importance)\ncolor_mapper = LinearColorMapper(palette=list(reversed(Blues9)), low=min(importances), high=max(importances))\n\n# Create figure with categorical y-axis\np = figure(\n    width=4800,\n    height=2700,\n    y_range=features_sorted,\n    x_range=(0, max(importances) * 1.15),\n    title=\"bar-feature-importance · bokeh · pyplots.ai\",\n    x_axis_label=\"Importance Score\",\n    toolbar_location=None,\n)\n\n# Draw horizontal bars with color gradient\np.hbar(\n    y=\"features\",\n    right=\"importances\",\n    height=0.7,\n    source=source,\n    fill_color={\"field\": \"importances\", \"transform\": color_mapper},\n    line_color=\"#306998\",\n    line_width=2,\n)\n\n# Add value labels at end of bars\nlabels = LabelSet(\n    x=\"label_x\",\n    y=\"features\",\n    text=\"labels\",\n    source=source,\n    text_font_size=\"18pt\",\n    text_color=\"#306998\",\n    text_baseline=\"middle\",\n)\np.add_layout(labels)\n\n# Styling for large canvas\np.title.text_font_size = \"32pt\"\np.title.text_color = \"#306998\"\np.xaxis.axis_label_text_font_size = \"24pt\"\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\n\n# Grid styling (subtle)\np.xgrid.grid_line_color = \"#cccccc\"\np.xgrid.grid_line_alpha = 0.3\np.xgrid.grid_line_dash = \"dashed\"\np.ygrid.grid_line_color = None\n\n# Axis styling\np.xaxis.axis_line_color = \"#666666\"\np.yaxis.axis_line_color = \"#666666\"\np.xaxis.major_tick_line_color = \"#666666\"\np.yaxis.major_tick_line_color = \"#666666\"\n\n# Background\np.background_fill_color = \"#fafafa\"\np.border_fill_color = \"white\"\np.outline_line_color = None\n\n# Save outputs\nexport_png(p, filename=\"plot.png\")\nsave(p, filename=\"plot.html\", resources=CDN, title=\"bar-feature-importance · bokeh · pyplots.ai\")\n"}