{"spec_id":"violin-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nviolin-basic: Basic Violin Plot\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 88/100 | Updated: 2026-05-29\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    annotate,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_violin,\n    ggplot,\n    labs,\n    scale_fill_manual,\n    stat_summary,\n    theme,\n    theme_minimal,\n)\n\n\n# Theme tokens (Imprint palette — theme-adaptive chrome)\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# Imprint categorical palette — positions 1–4 in canonical order\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n\n# Data — annual salary distributions across 4 departments\nnp.random.seed(42)\n\nrecords = []\n\n# Engineering: right-skewed (many mid-range, few senior high earners)\nsalaries_eng = np.concatenate([np.random.normal(95000, 12000, 180), np.random.normal(148000, 7000, 40)])\nrecords.extend([(\"Engineering\", s) for s in salaries_eng])\n\n# Design: bimodal (junior / senior pay split)\nsalaries_design = np.concatenate([np.random.normal(70000, 7000, 100), np.random.normal(108000, 7000, 100)])\nrecords.extend([(\"Design\", s) for s in salaries_design])\n\n# Marketing: tight normal (narrow pay band)\nsalaries_mkt = np.random.normal(82000, 5000, 200)\nrecords.extend([(\"Marketing\", s) for s in salaries_mkt])\n\n# Sales: wide spread (base + commission-driven variance)\nsalaries_sales = np.random.normal(90000, 22000, 200)\nrecords.extend([(\"Sales\", s) for s in salaries_sales])\n\ndf = pd.DataFrame(records, columns=[\"department\", \"salary\"])\ndf[\"salary\"] = df[\"salary\"].clip(35000, 200000)\ndf[\"department\"] = pd.Categorical(\n    df[\"department\"], categories=[\"Engineering\", \"Design\", \"Marketing\", \"Sales\"], ordered=True\n)\n\ndepartments = [\"Engineering\", \"Design\", \"Marketing\", \"Sales\"]\npalette = dict(zip(departments, IMPRINT_PALETTE, strict=True))\n\n# Plot\ntitle = \"violin-basic · python · plotnine · anyplot.ai\"\n\nplot = (\n    ggplot(df, aes(x=\"department\", y=\"salary\", fill=\"department\"))\n    + geom_violin(draw_quantiles=[0.25, 0.5, 0.75], alpha=0.82, color=INK_SOFT, size=0.6, trim=False)\n    + stat_summary(geom=\"point\", fun_y=np.mean, shape=\"D\", size=2.5, color=INK, fill=INK)\n    + scale_fill_manual(values=palette)\n    + labs(x=\"Department\", y=\"Annual Salary (USD)\", title=title)\n    + annotate(\"text\", x=2, y=148000, label=\"Bimodal:\\njunior/senior split\", size=2.8, color=INK_SOFT)\n    + theme_minimal()\n    + theme(\n        figure_size=(8, 4.5),\n        text=element_text(size=9, color=INK),\n        axis_title=element_text(size=10, color=INK),\n        axis_text=element_text(size=8, color=INK_SOFT),\n        plot_title=element_text(size=12, color=INK),\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_line=element_line(color=INK_SOFT),\n        panel_border=element_blank(),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n    )\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\", verbose=False)\n"}