{"spec_id":"bar-grouped","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nbar-grouped: Grouped Bar Chart\nLibrary: letsplot 4.11.0 | Python 3.13.14\nQuality: 86/100 | Updated: 2026-08-05\n\"\"\"\n\nimport os\n\nimport pandas as pd\nfrom lets_plot import *\n\n\nLetsPlot.setup_html()\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Imprint palette (positions 1-3)\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\n\n# Data - Quarterly sales by product category\ncategories = [\"Q1\", \"Q2\", \"Q3\", \"Q4\"]\nproducts = [\"Electronics\", \"Clothing\", \"Home & Garden\"]\n\ndata = {\n    \"Quarter\": categories * 3,\n    \"Product\": [\"Electronics\"] * 4 + [\"Clothing\"] * 4 + [\"Home & Garden\"] * 4,\n    \"Revenue\": [\n        # Electronics - strong growth\n        145,\n        168,\n        192,\n        235,\n        # Clothing - seasonal pattern\n        98,\n        112,\n        87,\n        142,\n        # Home & Garden - spring/summer peak\n        67,\n        95,\n        108,\n        72,\n    ],\n}\n\ndf = pd.DataFrame(data)\ndf[\"Label\"] = df[\"Revenue\"].apply(lambda v: f\"${v}K\")\n\n# Theme-adaptive chrome\nanyplot_theme = theme(\n    plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_border=element_blank(),\n    panel_grid_major_y=element_line(color=INK_MUTED, size=0.3),\n    panel_grid_major_x=element_blank(),\n    panel_grid_minor=element_blank(),\n    axis_title=element_text(size=12, color=INK),\n    axis_text=element_text(size=10, color=INK_SOFT),\n    axis_line_x=element_line(color=INK_SOFT, size=0.5),\n    axis_line_y=element_line(color=INK_SOFT, size=0.5),\n    plot_title=element_text(size=16, color=INK, face=\"bold\"),\n    legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n    legend_text=element_text(size=10, color=INK_SOFT),\n    legend_title=element_text(size=11, color=INK),\n    legend_position=\"right\",\n)\n\n# Distinctive lets-plot feature: rich, formatted native tooltips (beyond\n# just emitting an interactive HTML file) - each bar reports its quarter,\n# product line and exact revenue with a \"$\" prefix and \"K\" suffix.\nrevenue_tooltips = layer_tooltips().line(\"@Product\").line(\"Quarter|@Quarter\").line(\"Revenue|$@Revenue K\")\n\n# Plot - Grouped bar chart with direct value labels for a clearer data story\nplot = (\n    ggplot(df, aes(x=\"Quarter\", y=\"Revenue\", fill=\"Product\"))\n    + geom_bar(stat=\"identity\", position=\"dodge\", width=0.7, alpha=0.9, tooltips=revenue_tooltips)\n    + geom_text(\n        aes(label=\"Label\", group=\"Product\"), position=position_dodge(width=0.7), vjust=-0.5, size=3.2, color=INK_SOFT\n    )\n    + scale_fill_manual(values=IMPRINT)\n    + scale_y_continuous(format=\"${.0f}K\", expand=[0.1, 0])\n    + labs(\n        x=\"Quarter\",\n        y=\"Revenue ($ thousands)\",\n        title=\"bar-grouped · python · letsplot · anyplot.ai\",\n        fill=\"Product Category\",\n    )\n    + anyplot_theme\n    + ggsize(800, 450)\n)\n\n# Save as PNG and HTML\nggsave(plot, f\"plot-{THEME}.png\", path=\".\", scale=4)\nggsave(plot, f\"plot-{THEME}.html\", path=\".\")\n"}