{"spec_id":"count-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\ncount-basic: Basic Count Plot\nLibrary: bokeh 3.9.2 | Python 3.13.14\nQuality: 91/100 | Updated: 2026-08-11\n\"\"\"\n\nimport os\nimport sys\nimport time\nfrom pathlib import Path\n\n\nsys.path = [p for p in sys.path if \"implementations\" not in p]\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, HoverTool, LabelSet\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\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\"\nBRAND = \"#009E73\"  # Imprint palette position 1\nAMBER = \"#DDCC77\"  # callout accent, outside the categorical pool\n\n# Data - order counts across product categories in an e-commerce store\nnp.random.seed(7)\norders = np.random.choice(\n    [\"Electronics\", \"Clothing\", \"Home & Garden\", \"Books\", \"Sports\", \"Toys\", \"Beauty\", \"Groceries\"],\n    size=500,\n    p=[0.22, 0.18, 0.15, 0.13, 0.10, 0.08, 0.08, 0.06],\n)\n\n# Count occurrences\ncategories, counts = np.unique(orders, return_counts=True)\n# Sort by count descending for better readability\nsorted_indices = np.argsort(-counts)\ncategories = categories[sorted_indices].tolist()\ncounts = counts[sorted_indices].tolist()\ntotal_orders = sum(counts)\nleading_share = counts[0] / total_orders * 100\n\n# Create data source (explicit per-bar fill color list, never a bare scalar,\n# so every bar unambiguously resolves to the same brand green)\nsource = ColumnDataSource(\n    data={\n        \"category\": categories,\n        \"count\": counts,\n        \"label\": [str(c) for c in counts],\n        \"share\": [c / total_orders * 100 for c in counts],\n        \"fill_color\": [BRAND] * len(categories),\n        \"callout\": [f\"{leading_share:.0f}% of all orders\"] + [\"\"] * (len(categories) - 1),\n    }\n)\n\n# Create figure with categorical x-axis\n# `min_border_*` reserve room for the larger 34-42pt chrome so nothing\n# clips at the edges of the rendered PNG.\np = figure(\n    x_range=categories,\n    width=3200,\n    height=1800,\n    title=\"count-basic · python · bokeh · anyplot.ai\",\n    x_axis_label=\"Product Category\",\n    y_axis_label=\"Number of Orders\",\n    toolbar_location=None,  # avoids the ~30-50px toolbar shrinking the PNG below 3200x1800\n    min_border_bottom=160,\n    min_border_left=200,\n    min_border_top=110,\n    min_border_right=50,\n)\n\n# Plot bars with brand green — fill_color/line_color set explicitly (not the\n# \"color=\" shorthand) so every bar resolves the same fill unambiguously.\nbars = p.vbar(\n    x=\"category\",\n    top=\"count\",\n    source=source,\n    width=0.7,\n    fill_color=\"fill_color\",\n    fill_alpha=0.85,\n    line_color=INK_SOFT,\n    line_width=2,\n    nonselection_fill_color=\"fill_color\",\n    nonselection_fill_alpha=0.85,\n    nonselection_line_color=INK_SOFT,\n)\n\n# Hover detail beyond the bare default — exact count plus each category's\n# share of the 500-order sample, without altering the static PNG (the chart\n# stays a basic count plot; only the interactive HTML gains this detail).\np.add_tools(\n    HoverTool(\n        renderers=[bars], tooltips=[(\"Category\", \"@category\"), (\"Orders\", \"@count\"), (\"Share of total\", \"@share{0.0}%\")]\n    )\n)\n\n# Add count labels above bars\nlabels = LabelSet(\n    x=\"category\",\n    y=\"count\",\n    text=\"label\",\n    source=source,\n    text_align=\"center\",\n    text_baseline=\"bottom\",\n    y_offset=10,\n    text_font_size=\"32pt\",\n    text_color=INK_SOFT,\n)\np.add_layout(labels)\n\n# Focal callout on the leading category — surfaces its share of the total\n# order volume, adding a storytelling cue beyond plain descending sort.\n# Driven off the same categorical source as `labels` (via the \"callout\"\n# column, blank for every row but the leader) rather than a standalone\n# `Label`, since bokeh 3.9.2's Label.x rejects a bare categorical factor.\nleading_callout = LabelSet(\n    x=\"category\",\n    y=\"count\",\n    text=\"callout\",\n    source=source,\n    text_align=\"center\",\n    text_baseline=\"bottom\",\n    y_offset=52,\n    text_font_size=\"26pt\",\n    text_font_style=\"italic\",\n    text_color=AMBER,\n)\np.add_layout(leading_callout)\n\n# Style the plot — sizes per prompts/library/bokeh.md \"Sizing for 3200x1800\"\np.title.text_font_size = \"50pt\"\np.title.align = \"center\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"42pt\"\np.yaxis.axis_label_text_font_size = \"42pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_font_size = \"34pt\"\np.yaxis.major_label_text_font_size = \"34pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\n\n# Grid styling\np.xgrid.grid_line_color = None\np.ygrid.grid_line_color = INK\np.ygrid.grid_line_alpha = 0.10\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\n\n# Axis and background\np.xaxis.major_label_orientation = 0.5\np.y_range.start = 0\np.y_range.end = max(counts) * 1.15\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\n# No full rectangular outline — rely on the left/bottom axis lines for an\n# L-shaped frame, matching the style guide's default spine treatment.\np.outline_line_color = None\n\n# Save files in script directory\nscript_dir = Path(__file__).parent\nhtml_path = script_dir / f\"plot-{THEME}.html\"\npng_path = script_dir / f\"plot-{THEME}.png\"\n\noutput_file(str(html_path))\nsave(p)\n\n# Screenshot with headless Chrome\nW, H = 3200, 1800\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{html_path.resolve()}\")\n# Pin the viewport exactly via CDP — headless Chrome's --window-size sets the\n# OUTER window, which still reserves a phantom title-bar height even headless.\ndriver.execute_cdp_cmd(\n    \"Emulation.setDeviceMetricsOverride\", {\"width\": W, \"height\": H, \"deviceScaleFactor\": 1, \"mobile\": False}\n)\ntime.sleep(3)\ndriver.save_screenshot(str(png_path))\ndriver.quit()\n"}