{"spec_id":"violin-swarm","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nviolin-swarm: Violin Plot with Overlaid Swarm Points\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\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\"\n\n# Okabe-Ito palette - first series always #009E73\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n\n# Data - Gene expression levels (arbitrary units) across cell types\nnp.random.seed(42)\n\ncell_types = [\"Neuron\", \"Astrocyte\", \"Oligodendrocyte\", \"Microglia\"]\nn_per_type = 60\n\ndata = []\nfor cell_type in cell_types:\n    # Different distributions per cell type\n    if cell_type == \"Neuron\":\n        values = np.random.normal(85, 12, n_per_type)\n    elif cell_type == \"Astrocyte\":\n        values = np.random.normal(72, 15, n_per_type)\n    elif cell_type == \"Oligodendrocyte\":\n        # Bimodal distribution\n        values = np.concatenate([np.random.normal(55, 10, n_per_type // 2), np.random.normal(92, 8, n_per_type // 2)])\n    else:  # Microglia\n        values = np.random.normal(65, 14, n_per_type)\n\n    for v in values:\n        data.append({\"Cell Type\": cell_type, \"Expression Level\": v})\n\ndf = pd.DataFrame(data)\n\n# Plot\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\nfig, ax = plt.subplots(figsize=(16, 9))\n\n# Violin plot with transparency\nsns.violinplot(\n    data=df,\n    x=\"Cell Type\",\n    y=\"Expression Level\",\n    hue=\"Cell Type\",\n    palette=IMPRINT,\n    alpha=0.4,\n    inner=None,\n    legend=False,\n    ax=ax,\n)\n\n# Swarm plot overlay with darker shades\ndarker_palette = [\"#006B50\", \"#A63F00\", \"#004D7A\", \"#8B5A7C\"]\nsns.swarmplot(\n    data=df, x=\"Cell Type\", y=\"Expression Level\", hue=\"Cell Type\", palette=darker_palette, size=6, legend=False, ax=ax\n)\n\n# Styling\nax.set_title(\"violin-swarm · Python · seaborn · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.set_xlabel(\"Cell Type\", fontsize=20, color=INK)\nax.set_ylabel(\"Expression Level\", fontsize=20, color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\nax.grid(True, axis=\"y\", alpha=0.15, linewidth=0.8)\n\n# Spine styling\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor spine in [\"left\", \"bottom\"]:\n    ax.spines[spine].set_color(INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}