{"spec_id":"bar-diverging","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nbar-diverging: Diverging Bar Chart\nLibrary: matplotlib 3.11.1 | Python 3.13.15\nQuality: 93/100 | Updated: 2026-08-18\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.patches import Patch\n\n\n# Theme tokens\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 - Product satisfaction survey scores (-100 to +100)\ncategories = [\n    \"Customer Support\",\n    \"Product Quality\",\n    \"Pricing\",\n    \"Website Experience\",\n    \"Delivery Speed\",\n    \"Return Policy\",\n    \"Mobile App\",\n    \"Product Range\",\n    \"Payment Options\",\n    \"Brand Trust\",\n    \"Sustainability\",\n    \"Loyalty Program\",\n]\n\n# Net satisfaction scores (positive = satisfied, negative = dissatisfied)\nvalues = np.array([45, 72, -38, 28, -15, 55, -52, 33, 61, 85, -8, 18])\n\n# Sort by value for better pattern recognition\nsorted_indices = np.argsort(values)\ncategories_sorted = [categories[i] for i in sorted_indices]\nvalues_sorted = values[sorted_indices]\n\n# Imprint palette: brand green for positive (sentiment positive -> green),\n# matte-red semantic anchor for negative (sentiment negative -> red)\ncolors = [\"#009E73\" if v >= 0 else \"#AE3030\" for v in values_sorted]\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Create horizontal bars\ny_pos = np.arange(len(categories_sorted))\nax.barh(y_pos, values_sorted, color=colors, height=0.7, edgecolor=INK_SOFT, linewidth=0.4)\n\n# Add vertical line at zero\nax.axvline(x=0, color=INK_SOFT, linewidth=1, alpha=0.8)\n\n# Styling\nax.set_yticks(y_pos)\nax.set_yticklabels(categories_sorted, fontsize=8, color=INK_SOFT)\nax.set_xlabel(\"Net Satisfaction Score\", fontsize=10, color=INK)\nax.set_title(\"bar-diverging · python · matplotlib · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"x\", labelsize=8, colors=INK_SOFT)\n\n# Grid on x-axis only, subtle\nax.xaxis.grid(True, alpha=0.15, linewidth=0.4, color=INK_SOFT)\nax.set_axisbelow(True)\n\n# Add value labels at the end of each bar\nfor val, y in zip(values_sorted, y_pos, strict=True):\n    offset = 3 if val >= 0 else -3\n    ha = \"left\" if val >= 0 else \"right\"\n    ax.text(val + offset, y, f\"{val:+d}\", va=\"center\", ha=ha, fontsize=7, color=INK, fontweight=\"bold\")\n\n# Set x-axis limits with padding\nmax_abs = max(abs(values_sorted.min()), abs(values_sorted.max()))\nax.set_xlim(-max_abs - 25, max_abs + 25)\n\n# Remove top and right spines for cleaner look\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_color(INK_SOFT)\n\n# Callout annotations on the extremes — a matplotlib-distinctive touch (curved\n# connectionstyle arrow + themed bbox) that raises the chart's storytelling\n# beyond the bare sort-and-color-split baseline.\nbest_idx = int(np.argmax(values_sorted))\nworst_idx = int(np.argmin(values_sorted))\n\nax.annotate(\n    \"Strongest driver\",\n    xy=(0, y_pos[best_idx]),\n    xytext=(-max_abs * 0.45, y_pos[best_idx]),\n    fontsize=7,\n    color=INK,\n    ha=\"center\",\n    va=\"center\",\n    arrowprops={\"arrowstyle\": \"->\", \"connectionstyle\": \"arc3,rad=0.15\", \"color\": INK_SOFT, \"linewidth\": 0.9},\n    bbox={\"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"linewidth\": 0.4, \"boxstyle\": \"round,pad=0.35\"},\n)\n\nax.annotate(\n    \"Biggest pain point\",\n    xy=(0, y_pos[worst_idx]),\n    xytext=(max_abs * 0.4, y_pos[worst_idx] + 1.6),\n    fontsize=7,\n    color=INK,\n    ha=\"center\",\n    va=\"center\",\n    arrowprops={\"arrowstyle\": \"->\", \"connectionstyle\": \"arc3,rad=-0.15\", \"color\": INK_SOFT, \"linewidth\": 0.9},\n    bbox={\"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"linewidth\": 0.4, \"boxstyle\": \"round,pad=0.35\"},\n)\n\n# Add legend\nlegend_elements = [\n    Patch(facecolor=\"#009E73\", edgecolor=INK_SOFT, label=\"Positive (Satisfied)\"),\n    Patch(facecolor=\"#AE3030\", edgecolor=INK_SOFT, label=\"Negative (Dissatisfied)\"),\n]\nleg = ax.legend(handles=legend_elements, loc=\"lower right\", fontsize=8)\nif leg:\n    leg.get_frame().set_facecolor(ELEVATED_BG)\n    leg.get_frame().set_edgecolor(INK_SOFT)\n    leg.get_frame().set_linewidth(0.4)\n    plt.setp(leg.get_texts(), color=INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}