{"spec_id":"qq-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nqq-basic: Basic Q-Q Plot\nLibrary: seaborn 0.13.2 | Python 3.13.14\nQuality: 92/100 | Updated: 2026-07-24\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\"\nBRAND = \"#009E73\"  # Imprint palette position 1\n\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\n# Data - mixture distribution with right-skewed tail to demonstrate Q-Q deviation\nrng = np.random.default_rng(42)\nsample = np.concatenate([rng.normal(loc=50, scale=10, size=180), rng.normal(loc=75, scale=5, size=20)])\nn = len(sample)\n\n# Theoretical quantiles via Abramowitz & Stegun 26.2.17 rational approximation\np = (np.arange(1, n + 1) - 0.5) / n\nt = np.where(p < 0.5, np.sqrt(-2 * np.log(p)), np.sqrt(-2 * np.log(1 - p)))\nnum = 2.515517 + 0.802853 * t + 0.010328 * t**2\nden = 1 + 1.432788 * t + 0.189269 * t**2 + 0.001308 * t**3\ntheoretical_q = np.where(p < 0.5, -(t - num / den), t - num / den)\nsample_q = np.sort((sample - sample.mean()) / sample.std(ddof=1))\n\n# Simulation envelope: draw many perfectly-normal replicates of size n and let\n# seaborn's lineplot bootstrap a 95% percentile interval around their order\n# statistics. This gives the reference guide real statistical depth (the\n# natural sampling variability a truly normal sample would show) instead of a\n# bare y=x line, and leverages seaborn's own error-bar estimation.\nn_replicates = 300\nreplicates = np.sort(rng.standard_normal(size=(n_replicates, n)), axis=1)\nenvelope_df = pd.DataFrame({\"theoretical\": np.tile(theoretical_q, n_replicates), \"simulated\": replicates.ravel()})\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nsns.lineplot(\n    data=envelope_df,\n    x=\"theoretical\",\n    y=\"simulated\",\n    ax=ax,\n    estimator=\"mean\",\n    errorbar=(\"pi\", 95),\n    color=INK_SOFT,\n    linewidth=1.5,\n    linestyle=\"--\",\n    label=\"Normal reference (95% envelope)\",\n    zorder=1,\n)\n\nsns.scatterplot(\n    x=theoretical_q,\n    y=sample_q,\n    ax=ax,\n    s=90,\n    color=BRAND,\n    alpha=0.75,\n    edgecolor=PAGE_BG,\n    linewidth=0.8,\n    label=\"Sample quantiles\",\n    zorder=2,\n)\n\n# Style\nax.set_xlabel(\"Theoretical Quantiles\", fontsize=10, color=INK)\nax.set_ylabel(\"Sample Quantiles\", fontsize=10, color=INK)\nax.set_title(\"qq-basic · python · seaborn · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\nax.legend(fontsize=8, loc=\"upper left\")\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)\nax.grid(True, alpha=0.10, linewidth=0.8, color=INK)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}