{"spec_id":"bar-diverging","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\nbar-diverging: Diverging Bar Chart\nLibrary: pygal 3.1.3 | Python 3.13.15\nQuality: 91/100 | Updated: 2026-08-18\n\"\"\"\n\nimport os\nimport sys\n\n\nif sys.path[0] == os.path.dirname(os.path.abspath(__file__)):\n    sys.path.pop(0)\n\nimport pygal\nfrom pygal.style import Style\n\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\"\n\n# Imprint palette: brand green for positive, matte-red semantic anchor for negative\nIMPRINT = (\"#009E73\", \"#AE3030\")\n\n# Data - Customer satisfaction survey scores by department\n# Positive = satisfied, Negative = dissatisfied (scale -100 to +100)\ndepartments = [\n    \"Customer Support\",\n    \"Product Quality\",\n    \"Shipping Speed\",\n    \"Website Experience\",\n    \"Return Process\",\n    \"Price Value\",\n    \"Mobile App\",\n    \"Documentation\",\n    \"Response Time\",\n    \"Overall Experience\",\n]\n\nscores = [72, 45, -23, 58, -45, 31, -12, -38, 64, 52]\n\n# Sort by value for better pattern recognition\nsorted_data = sorted(zip(departments, scores, strict=True), key=lambda x: x[1])\nsorted_departments = [d[0] for d in sorted_data]\nsorted_scores = [d[1] for d in sorted_data]\n\n# Custom style with theme-adaptive colors and proper font sizing.\n# Title is longer than the 67-char baseline, so its size is scaled down\n# proportionally (see prompts/plot-generator.md \"Title fontsize must scale\").\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=IMPRINT,\n    opacity=\".92\",\n    opacity_hover=\"1\",\n    title_font_size=60,\n    label_font_size=56,\n    major_label_font_size=44,\n    legend_font_size=44,\n    value_font_size=36,\n    value_label_font_size=36,\n    tooltip_font_size=32,\n    stroke_width=2.5,\n)\n\n# Create horizontal bar chart (better for long labels)\nchart = pygal.HorizontalBar(\n    width=3200,\n    height=1800,\n    style=custom_style,\n    title=\"Customer Satisfaction Survey · bar-diverging · python · pygal · anyplot.ai\",\n    x_title=\"Satisfaction Score\",\n    show_legend=True,\n    legend_at_bottom=True,\n    legend_at_bottom_columns=2,\n    show_x_guides=True,\n    show_y_guides=False,\n    print_values=True,\n    print_values_position=\"center\",\n    value_formatter=lambda x: f\"{x:+.0f}\" if x else \"\",\n    range=(-100, 100),\n    margin=40,\n    spacing=16,\n    truncate_label=-1,\n)\n\n# Add data with color based on positive/negative\n# Separate positive and negative for legend clarity\npositive_scores = [s if s >= 0 else None for s in sorted_scores]\nnegative_scores = [s if s < 0 else None for s in sorted_scores]\n\nchart.add(\"Satisfied\", positive_scores)\nchart.add(\"Dissatisfied\", negative_scores)\n\n# Set category labels\nchart.x_labels = sorted_departments\n\n# Save outputs with theme-suffixed filenames\nchart.render_to_png(f\"plot-{THEME}.png\")\nwith open(f\"plot-{THEME}.html\", \"wb\") as f:\n    f.write(chart.render())\n"}