{"spec_id":"density-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\ndensity-basic: Basic Density Plot\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-30\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove script dir from sys.path to avoid shadowing the plotnine package\n_script_dir = os.path.dirname(os.path.abspath(__file__))\nfor _p in [_script_dir, \"\", \".\"]:\n    while _p in sys.path:\n        sys.path.remove(_p)\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    after_stat,\n    annotate,\n    coord_cartesian,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_area,\n    geom_line,\n    geom_rug,\n    geom_vline,\n    ggplot,\n    labs,\n    scale_x_continuous,\n    scale_y_continuous,\n    theme,\n    theme_minimal,\n)\n\n\n# Theme tokens (Imprint palette — see prompts/default-style-guide.md)\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\"\nBRAND = \"#009E73\"  # Imprint position 1 — always first series\n\n# Data — bimodal test score distribution (150 main + 50 high achievers)\nnp.random.seed(42)\ntest_scores = np.concatenate([np.random.normal(72, 10, 150), np.random.normal(88, 5, 50)])\ntest_scores = np.clip(test_scores, 0, 100)\n\ndf = pd.DataFrame({\"score\": test_scores})\n\n# Title with length-aware font sizing\ntitle = \"density-basic · python · plotnine · anyplot.ai\"\nn = len(title)\nratio = 67 / n if n > 67 else 1.0\ntitle_fs = max(8, round(12 * ratio))\n\n# Plot — layered density with bimodal emphasis\nplot = (\n    ggplot(df, aes(x=\"score\"))\n    + geom_area(aes(y=after_stat(\"density\")), stat=\"density\", fill=BRAND, alpha=0.3, color=\"none\")\n    + geom_line(aes(y=after_stat(\"density\")), stat=\"density\", color=BRAND, size=1.8)\n    + geom_vline(xintercept=72, linetype=\"dashed\", color=INK_SOFT, size=0.9, alpha=0.75)\n    + geom_vline(xintercept=88, linetype=\"dashed\", color=INK_SOFT, size=0.9, alpha=0.75)\n    + geom_rug(color=INK_MUTED, alpha=0.5, size=0.6)\n    + annotate(\"text\", x=72, y=0.034, label=\"Main Group (μ ≈ 72)\", size=3.5, color=INK)\n    + annotate(\"text\", x=89, y=0.029, label=\"High Achievers (μ ≈ 88)\", size=3.5, color=INK)\n    + labs(x=\"Test Score (points)\", y=\"Probability Density\", title=title)\n    + scale_x_continuous(breaks=range(45, 101, 10))\n    + scale_y_continuous(expand=(0, 0, 0.2, 0))\n    + coord_cartesian(xlim=(45, 102))\n    + theme_minimal()\n    + theme(\n        figure_size=(8, 4.5),\n        text=element_text(size=7, color=INK_SOFT),\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=title_fs, color=INK),\n        legend_text=element_text(size=8, color=INK_SOFT),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\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        panel_border=element_blank(),\n        axis_line=element_line(color=INK_SOFT),\n    )\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\", verbose=False)\n"}