{"spec_id":"qq-basic","library":"altair","language":"python","code":"\"\"\" anyplot.ai\nqq-basic: Basic Q-Q Plot\nLibrary: altair 6.2.2 | Python 3.13.14\nQuality: 90/100 | Updated: 2026-07-24\n\"\"\"\n\nimport os\nimport sys\n\n\n# The file is named altair.py; remove its own directory from sys.path so\n# `import altair` resolves to the library, not this script.\n_HERE = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if not p or os.path.abspath(p) != _HERE]\n\nimport altair as alt\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nfrom scipy.stats import norm\n\n\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\"  # Imprint palette, position 1\n\n# Bolt torque QC readings (N·m) from two assembly-line stations: most bolts\n# come from a well-calibrated station, a smaller batch from a drifting\n# station running hot, producing a bimodal mixture that departs from\n# normality in an S-curve.\nnp.random.seed(42)\nsample = np.concatenate([np.random.normal(45, 3, 80), np.random.normal(58, 2, 20)])\n\nn = len(sample)\nsorted_sample = np.sort(sample)\np = (np.arange(1, n + 1) - 0.5) / n\nsample_mean, sample_std = np.mean(sample), np.std(sample, ddof=1)\ntheoretical_scaled = norm.ppf(p) * sample_std + sample_mean\n\ndf = pd.DataFrame({\"Theoretical Quantiles\": theoretical_scaled, \"Sample Quantiles\": sorted_sample})\n\nline_min = min(theoretical_scaled.min(), sorted_sample.min())\nline_max = max(theoretical_scaled.max(), sorted_sample.max())\nline_df = pd.DataFrame({\"x\": [line_min, line_max], \"y\": [line_min, line_max]})\nlabel_df = pd.DataFrame({\"x\": [line_max], \"y\": [line_max], \"label\": [\"y = x\"]})\n\n# Distinctive Altair feature: a pointer-hover selection that enlarges the\n# nearest marker, giving overlapping mid-quantile points an interactive way\n# to be told apart (beyond the static tooltip already on the layer).\nhover = alt.selection_point(on=\"pointerover\", nearest=True, empty=False)\n\npoints = (\n    alt.Chart(df)\n    .mark_point(size=50, color=BRAND, filled=True, opacity=0.65, stroke=PAGE_BG, strokeWidth=0.5)\n    .encode(\n        x=alt.X(\"Theoretical Quantiles:Q\", title=\"Theoretical Quantiles\", scale=alt.Scale(zero=False)),\n        y=alt.Y(\"Sample Quantiles:Q\", title=\"Sample Quantiles\", scale=alt.Scale(zero=False)),\n        tooltip=[\"Theoretical Quantiles:Q\", \"Sample Quantiles:Q\"],\n        size=alt.condition(hover, alt.value(160), alt.value(50)),\n    )\n    .add_params(hover)\n)\n\nreference_line = alt.Chart(line_df).mark_line(color=INK_SOFT, strokeWidth=2, strokeDash=[8, 4]).encode(x=\"x:Q\", y=\"y:Q\")\n\nline_label = (\n    alt.Chart(label_df)\n    .mark_text(align=\"right\", baseline=\"bottom\", dx=-4, dy=-4, fontSize=11, color=INK_SOFT, fontStyle=\"italic\")\n    .encode(x=\"x:Q\", y=\"y:Q\", text=\"label:N\")\n)\n\nchart = (\n    (reference_line + line_label + points)\n    .properties(\n        background=PAGE_BG,\n        width=620,\n        height=320,\n        padding={\"left\": 0, \"right\": 0, \"top\": 0, \"bottom\": 0},\n        title=alt.Title(\"Bolt Torque QC · qq-basic · python · altair · anyplot.ai\", fontSize=16),\n    )\n    .configure_view(fill=PAGE_BG, strokeWidth=0)\n    .configure_axis(\n        domainColor=INK_SOFT,\n        tickColor=INK_SOFT,\n        gridColor=INK,\n        gridOpacity=0.10,\n        gridDash=[4, 4],\n        labelColor=INK_SOFT,\n        labelFontSize=10,\n        titleColor=INK,\n        titleFontSize=12,\n    )\n    .configure_title(color=INK)\n)\n\nchart.save(f\"plot-{THEME}.png\", scale_factor=4.0)\nchart.save(f\"plot-{THEME}.html\")\n\n# Canvas contract: pad (never crop) up to the exact 3200x1800 target — vl-convert's\n# title/axis-label padding lands short of the target more often than over it.\nTW, TH = 3200, 1800\n_img = Image.open(f\"plot-{THEME}.png\").convert(\"RGB\")\n_w, _h = _img.size\nif _w > TW or _h > TH:\n    raise SystemExit(\n        f\"altair vl-convert produced {_w}x{_h}, exceeds target {TW}x{TH}. \"\n        f\"Shrink chart .properties(width=, height=) values and re-render.\"\n    )\nif _w < TW or _h < TH:\n    _canvas = Image.new(\"RGB\", (TW, TH), PAGE_BG)\n    _canvas.paste(_img, ((TW - _w) // 2, (TH - _h) // 2))\n    _canvas.save(f\"plot-{THEME}.png\")\n"}