{"spec_id":"qq-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nqq-basic: Basic Q-Q Plot\nLibrary: plotnine 0.15.7 | Python 3.13.14\nQuality: 90/100 | Updated: 2026-07-24\n\"\"\"\n\nimport os\nimport sys\n\nimport numpy as np\nimport pandas as pd\n\n\n# Avoid shadowing the plotnine library when this file is run directly\n_cwd = os.getcwd()\nsys.path = [p for p in sys.path if os.path.abspath(p) != _cwd]\n\nfrom plotnine import (\n    aes,\n    annotate,\n    element_line,\n    element_rect,\n    element_text,\n    ggplot,\n    labs,\n    stat_qq,\n    stat_qq_line,\n    theme,\n    theme_minimal,\n)\nfrom scipy import stats\n\n\n# Theme tokens (Imprint palette)\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nBRAND = \"#009E73\"\nAMBER = \"#DDCC77\"\n\n# Data - assembly-line cycle times (seconds). Most parts finish near the\n# 48s target, but a rework subset (equipment slowdown / re-machining) runs\n# slower, producing a heavy right tail that departs from the normality a\n# Six Sigma control chart would otherwise assume.\nnp.random.seed(42)\ncycle_time = np.concatenate([np.random.randn(80) * 6 + 48, np.random.randn(20) * 8 + 68])\ndf = pd.DataFrame({\"cycle_time\": cycle_time})\n\n# Locate the tail-departure cluster (for the callout) using the same\n# plotting-position convention as stat_qq/stat_qq_line\n(theo_q, samp_q), (slope, intercept, _r) = stats.probplot(cycle_time, dist=\"norm\")\nfitted = slope * theo_q + intercept\ntail_mask = (theo_q > 0.8) & (samp_q - fitted > (samp_q - fitted).std())\ncallout_x = theo_q[tail_mask].mean()\ncallout_y = samp_q[tail_mask].max()\n\nplot = (\n    ggplot(df, aes(sample=\"cycle_time\"))\n    + annotate(\n        \"rect\",\n        xmin=0.8,\n        xmax=theo_q.max() + 0.15,\n        ymin=fitted[theo_q > 0.8].min(),\n        ymax=samp_q.max() + 3,\n        fill=AMBER,\n        alpha=0.10,\n    )\n    + stat_qq_line(color=INK_SOFT, size=1.2, linetype=\"dashed\")\n    + stat_qq(color=BRAND, alpha=0.55, size=2.2)\n    + annotate(\n        \"segment\",\n        x=callout_x - 0.65,\n        y=callout_y + 4,\n        xend=callout_x - 0.1,\n        yend=callout_y + 0.5,\n        color=INK_SOFT,\n        size=0.6,\n    )\n    + annotate(\n        \"text\",\n        x=callout_x - 0.7,\n        y=callout_y + 4.5,\n        label=\"Rework subset: heavy right tail\",\n        color=INK_SOFT,\n        size=7,\n        ha=\"right\",\n    )\n    + labs(\n        x=\"Theoretical Quantiles (Standard Normal)\",\n        y=\"Sample Quantiles (Cycle Time, seconds)\",\n        title=\"qq-basic · python · plotnine · anyplot.ai\",\n    )\n    + theme_minimal()\n    + theme(\n        figure_size=(8, 4.5),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_line(color=INK, size=0.3, alpha=0.10),\n        panel_grid_minor=element_line(color=INK, size=0.2, alpha=0.05),\n        axis_title=element_text(color=INK, size=10),\n        axis_text=element_text(color=INK_SOFT, size=8),\n        plot_title=element_text(color=INK, size=12),\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"}