{"spec_id":"boxen-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nboxen-basic: Basic Boxen Plot (Letter-Value Plot)\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-17\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 = [\n    \"#009E73\",  # bluish green\n    \"#C475FD\",  # vermillion\n    \"#4467A3\",  # blue\n    \"#BD8233\",  # reddish purple\n]\n\n# Data - Gene expression levels across tumor and normal samples\nnp.random.seed(42)\n\ngenes = [\"TP53\", \"BRCA1\", \"MYC\", \"EGFR\"]\nn_per_group = 4000\n\ndata = []\nfor _i, gene in enumerate(genes):\n    if gene == \"TP53\":\n        # Tumor suppressor - bimodal (wild-type vs mutant expression)\n        values = np.concatenate(\n            [\n                np.random.normal(loc=6.5, scale=0.8, size=int(n_per_group * 0.6)),\n                np.random.normal(loc=2.0, scale=0.5, size=int(n_per_group * 0.4)),\n            ]\n        )\n    elif gene == \"BRCA1\":\n        # Breast cancer susceptibility - right-skewed, occasional high expression\n        values = np.concatenate(\n            [\n                np.random.exponential(scale=3.5, size=int(n_per_group * 0.85)),\n                np.random.uniform(15, 25, size=int(n_per_group * 0.15)),\n            ]\n        )\n    elif gene == \"MYC\":\n        # Oncogene - highly variable, long tail\n        values = np.random.lognormal(mean=2.0, sigma=1.2, size=n_per_group)\n    else:  # EGFR\n        # Growth receptor - relatively symmetric with moderate spread\n        values = np.random.normal(loc=8.0, scale=2.0, size=n_per_group)\n\n    # Ensure positive expression values\n    values = np.clip(values, 0.1, None)\n    data.extend([(gene, v) for v in values])\n\ndf = pd.DataFrame(data, columns=[\"Gene\", \"Expression Level (log2)\"])\n\n# Configure seaborn theme\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    },\n)\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\n\nsns.boxenplot(\n    data=df,\n    x=\"Gene\",\n    y=\"Expression Level (log2)\",\n    hue=\"Gene\",\n    palette=IMPRINT,\n    legend=False,\n    width=0.6,\n    linewidth=1.5,\n    ax=ax,\n)\n\n# Styling\nax.set_title(\"boxen-basic · seaborn · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK, pad=20)\nax.set_xlabel(\"Gene\", fontsize=20, color=INK)\nax.set_ylabel(\"Expression Level (log2)\", fontsize=20, color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Grid\nax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK_SOFT)\n\n# Remove top and right spines\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    ax.spines[spine].set_linewidth(1.0)\n\n# Legend explaining quantile levels\nlegend_text = \"Nested boxes represent letter values (quartiles, eighths, sixteenths, etc.)\"\nax.text(\n    0.5, -0.15, legend_text, transform=ax.transAxes, ha=\"center\", va=\"top\", fontsize=14, color=INK_SOFT, style=\"italic\"\n)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}