{"spec_id":"box-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nbox-basic: Basic Box Plot\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 87/100 | Updated: 2026-05-28\n\"\"\"\n\nimport os\nimport sys\n\n\n# Prevent this file from shadowing the plotnine package (script is named plotnine.py)\nsys.path = [p for p in sys.path if os.path.abspath(p or \".\") != os.path.dirname(os.path.abspath(__file__))]\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    annotate,\n    coord_cartesian,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_boxplot,\n    ggplot,\n    labs,\n    scale_fill_manual,\n    scale_y_continuous,\n    stat_summary,\n    theme,\n    theme_minimal,\n)\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\n# Data\nnp.random.seed(42)\ncategories = [\"Engineering\", \"Marketing\", \"Sales\", \"Support\", \"Research\"]\nrecords = []\n\nfor cat in categories:\n    n = np.random.randint(60, 120)\n    if cat == \"Engineering\":\n        values = np.random.normal(95000, 15000, n)\n    elif cat == \"Marketing\":\n        values = np.random.normal(75000, 12000, n)\n    elif cat == \"Sales\":\n        base = np.random.normal(68000, 18000, n)\n        outliers = np.random.normal(135000, 6000, 4)\n        values = np.concatenate([base, outliers])\n    elif cat == \"Support\":\n        values = np.random.normal(55000, 8000, n)\n    else:  # Research\n        values = np.random.normal(85000, 20000, n)\n    records.extend({\"department\": cat, \"salary\": v} for v in values)\n\ndf = pd.DataFrame(records)\ndept_order = [\"Support\", \"Marketing\", \"Sales\", \"Research\", \"Engineering\"]\ndf[\"department\"] = pd.Categorical(df[\"department\"], categories=dept_order, ordered=True)\n\nmedians = df.groupby(\"department\", observed=True)[\"salary\"].median()\neng_median = medians[\"Engineering\"]\nsup_median = medians[\"Support\"]\ngap = eng_median - sup_median\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"department\", y=\"salary\", fill=\"department\"))\n    + geom_boxplot(\n        outlier_size=2.5, outlier_alpha=0.65, outlier_colour=INK_SOFT, size=0.5, alpha=0.85, width=0.6, color=INK_SOFT\n    )\n    + stat_summary(fun_y=np.median, geom=\"point\", size=4, shape=\"D\", color=INK, fill=INK)\n    + scale_fill_manual(values=IMPRINT_PALETTE[:5])\n    + scale_y_continuous(labels=lambda vals: [f\"${v / 1000:.0f}k\" for v in vals], breaks=range(20000, 160001, 20000))\n    + coord_cartesian(ylim=(12000, 160000))\n    # Gap annotation — bracket style with ticks at Support (x=1) and Engineering (x=5)\n    + annotate(\n        \"text\",\n        x=3,\n        y=157000,\n        label=f\"Engineering earns ${gap / 1000:.0f}k more than Support\",\n        color=INK,\n        size=4.0,\n        ha=\"center\",\n        fontweight=\"bold\",\n    )\n    + annotate(\"segment\", x=1, xend=5, y=152500, yend=152500, color=INK_SOFT, size=0.6, alpha=0.65)\n    + annotate(\"segment\", x=1, xend=1, y=149000, yend=152500, color=INK_SOFT, size=0.6, alpha=0.65)\n    + annotate(\"segment\", x=5, xend=5, y=149000, yend=152500, color=INK_SOFT, size=0.6, alpha=0.65)\n    # Senior hires: diagonal connector from Sales outlier cluster to label\n    + annotate(\n        \"label\",\n        x=4.1,\n        y=143000,\n        label=\"Senior hires\\nabove market rate\",\n        size=4.0,\n        color=INK,\n        fill=ELEVATED_BG,\n        alpha=0.92,\n        label_size=0,\n        ha=\"center\",\n    )\n    + annotate(\"segment\", x=3.08, xend=4.0, y=136500, yend=141000, color=INK_SOFT, size=0.5, alpha=0.65)\n    # Support tight distribution insight\n    + annotate(\n        \"text\",\n        x=1,\n        y=24000,\n        label=\"Narrow spread\\n(σ ≈ $8k)\",\n        color=INK_MUTED,\n        size=3.8,\n        ha=\"center\",\n        fontstyle=\"italic\",\n    )\n    + labs(x=\"Department\", y=\"Salary ($)\", title=\"box-basic · python · plotnine · anyplot.ai\")\n    + theme_minimal()\n    + theme(\n        figure_size=(8, 4.5),\n        text=element_text(size=7, color=INK),\n        plot_title=element_text(size=12, color=INK),\n        axis_title=element_text(size=10, color=INK),\n        axis_text=element_text(size=8, color=INK_SOFT),\n        legend_position=\"none\",\n        panel_grid_major_x=element_blank(),\n        panel_grid_minor=element_blank(),\n        panel_grid_major_y=element_line(color=INK, size=0.3, alpha=0.15),\n        axis_ticks_major_x=element_blank(),\n        axis_ticks_major_y=element_blank(),\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    )\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\", verbose=False)\n"}