{"spec_id":"violin-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nviolin-basic: Basic Violin Plot\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-29\n\"\"\"\n\nimport os\n\nimport matplotlib.patheffects as pe\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\n# Theme tokens — Imprint palette chrome layer\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→4 for four categories\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\nANYPLOT_AMBER = \"#DDCC77\"  # semantic anchor — used for median line emphasis\n\n# Data — test scores (0-100) across four schools with distinct distribution shapes\nnp.random.seed(42)\ncategories = [\"Lincoln HS\", \"Roosevelt Acad.\", \"Jefferson HS\", \"Hamilton Prep\"]\ndata = [\n    np.clip(np.random.normal(75, 10, 150), 0, 100),  # Lincoln: normal, centered ~75\n    np.clip(np.random.normal(85, 6, 150), 0, 100),  # Roosevelt: high, tight cluster\n    np.clip(np.random.normal(62, 15, 150), 0, 100),  # Jefferson: lower, wide spread\n    np.clip(\n        np.concatenate([np.random.normal(70, 5, 80), np.random.normal(88, 4, 70)]), 0, 100\n    ),  # Hamilton: bimodal (two subgroups)\n]\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nparts = ax.violinplot(\n    data,\n    positions=range(len(categories)),\n    quantiles=[[0.25, 0.5, 0.75]] * len(categories),\n    showmeans=False,\n    showmedians=False,\n    showextrema=False,\n    bw_method=0.3,\n    widths=0.75,\n)\n\n# Style each violin body with Imprint palette colors\nfor i, pc in enumerate(parts[\"bodies\"]):\n    pc.set_facecolor(IMPRINT_PALETTE[i])\n    pc.set_edgecolor(INK_SOFT)\n    pc.set_alpha(0.8)\n    pc.set_linewidth(1.5)\n\n# Quantile lines — white Q1/Q3, amber median, path effects for legibility against colored bodies\nq_colors = [\"white\", ANYPLOT_AMBER, \"white\"] * len(categories)\nq_widths = [2.0, 3.5, 2.0] * len(categories)\nparts[\"cquantiles\"].set_colors(q_colors)\nparts[\"cquantiles\"].set_linewidths(q_widths)\nparts[\"cquantiles\"].set_path_effects([pe.Stroke(linewidth=5, foreground=\"black\", alpha=0.3), pe.Normal()])\n\n# Style\ntitle = \"violin-basic · python · matplotlib · anyplot.ai\"\ntitle_fontsize = max(8, round(12 * 67 / len(title))) if len(title) > 67 else 12\n\nax.set_xticks(range(len(categories)))\nax.set_xticklabels(categories)\nax.set_xlabel(\"School\", fontsize=10, color=INK)\nax.set_ylabel(\"Test Score (points)\", fontsize=10, color=INK)\nax.set_title(title, fontsize=title_fontsize, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\n\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\n\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\n\n# Annotation highlighting Hamilton Prep's bimodal distribution\nax.annotate(\n    \"Two distinct\\nperformance groups\",\n    xy=(3, 75),\n    xytext=(3, 42),\n    fontsize=8,\n    color=\"#BD8233\",\n    fontstyle=\"italic\",\n    ha=\"center\",\n    arrowprops={\"arrowstyle\": \"->\", \"color\": \"#BD8233\", \"lw\": 1.5},\n)\n\nfig.subplots_adjust(left=0.08, right=0.97, top=0.92, bottom=0.13)\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}