{"spec_id":"pp-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\npp-basic: Probability-Probability (P-P) Plot\nLibrary: matplotlib 3.11.0 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-06-16\n\"\"\"\n\nimport os\nimport sys\n\n\n# This file is named matplotlib.py; drop the script dir from sys.path so\n# `import matplotlib` resolves to the installed package, not this module.\n_script_dir = os.path.abspath(os.path.dirname(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p or os.getcwd()) != _script_dir]\nsys.modules.pop(\"matplotlib\", None)\n\nimport matplotlib.pyplot as plt\nimport matplotlib.ticker as mticker\nimport numpy as np\nfrom scipy.stats import norm\n\n\n# Theme-adaptive chrome (see prompts/default-style-guide.md \"Theme-adaptive Chrome\")\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 palette position 1 — always the first series\n\n# Data — manufacturing quality control: bolt tensile strength (MPa)\n# A small secondary-supplier batch creates a heavier upper tail, so the\n# sample departs from normality in the classic P-P S-shape.\nnp.random.seed(42)\nsample_size = 200\nprimary_batch = np.random.normal(loc=840, scale=35, size=160)\nsecondary_batch = np.random.normal(loc=910, scale=28, size=40)\ntensile_strength = np.concatenate([primary_batch, secondary_batch])\n\nobserved_sorted = np.sort(tensile_strength)\nempirical_cdf = np.arange(1, sample_size + 1) / (sample_size + 1)\n\nmu, sigma = observed_sorted.mean(), observed_sorted.std(ddof=0)\ntheoretical_cdf = norm.cdf((observed_sorted - mu) / sigma)\n\n# Plot — square canvas keeps the 45-degree diagonal meaningful (→ 2400×2400 px)\nfig, ax = plt.subplots(figsize=(6, 6), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# 95% confidence band from order-statistic variance of the cumulative probabilities\nband_x = np.linspace(0, 1, 200)\nse = np.sqrt(band_x * (1 - band_x) / sample_size)\nax.fill_between(\n    band_x, band_x - 1.96 * se, band_x + 1.96 * se, color=INK_MUTED, alpha=0.16, zorder=0, label=\"95% confidence band\"\n)\n\n# Perfect-fit reference diagonal (neutral structural line)\nax.plot([0, 1], [0, 1], color=INK, linewidth=1.6, linestyle=\"--\", zorder=1, label=\"Perfect normal fit\")\n\n# Empirical vs. theoretical cumulative probabilities\nax.scatter(\n    theoretical_cdf,\n    empirical_cdf,\n    s=60,\n    color=BRAND,\n    alpha=0.8,\n    edgecolors=PAGE_BG,\n    linewidth=0.6,\n    zorder=3,\n    label=\"Sample (n=200)\",\n)\n\n# Call out the S-shape: the secondary-supplier batch lifts the upper tail\n# above the diagonal. Annotate the point of largest positive departure.\ndeparture = empirical_cdf - theoretical_cdf\ntail_idx = int(np.argmax(departure))\nax.annotate(\n    \"heavier upper tail\",\n    xy=(theoretical_cdf[tail_idx], empirical_cdf[tail_idx]),\n    xytext=(0.34, 0.84),\n    fontsize=8,\n    color=INK_SOFT,\n    ha=\"left\",\n    va=\"center\",\n    zorder=4,\n    arrowprops={\n        \"arrowstyle\": \"->\",\n        \"color\": INK_SOFT,\n        \"linewidth\": 0.9,\n        \"alpha\": 0.85,\n        \"connectionstyle\": \"arc3,rad=-0.2\",\n    },\n)\n\n# Style\nax.set_xlabel(\"Theoretical Cumulative Probability (Normal)\", fontsize=10, color=INK)\nax.set_ylabel(\"Empirical Cumulative Probability\", fontsize=10, color=INK)\nax.set_title(\"pp-basic · python · matplotlib · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, pad=10)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\nax.xaxis.set_major_locator(mticker.MultipleLocator(0.2))\nax.yaxis.set_major_locator(mticker.MultipleLocator(0.2))\nax.set_xlim(-0.02, 1.02)\nax.set_ylim(-0.02, 1.02)\nax.set_aspect(\"equal\")\n\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)\n    ax.spines[s].set_linewidth(0.8)\nax.grid(True, alpha=0.15, linewidth=0.6, color=INK)\n\n# Legend\nleg = ax.legend(fontsize=8, loc=\"lower right\", framealpha=0.95)\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nfor text in leg.get_texts():\n    text.set_color(INK_SOFT)\n\nfig.subplots_adjust(left=0.11, right=0.97, top=0.93, bottom=0.09)\n\n# Save\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}